Pages

Thursday, May 19, 2011

SUTHERLAND HODGEMAN POLYGON CLIPPING

The algorithm begins with an input list of all vertices in the subject polygon. Next, one side of the clip polygon is extended infinitely in both directions, and the path of the subject polygon is traversed. Vertices from the input list are inserted into an output list if they lie on the visible side of the extended clip polygon line, and new vertices are added to the output list where the subject polygon path crosses the extended clip polygon line.

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);
enum  {  TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
float xmin,xmax,ymin,ymax;
void clip(float x0,float y0,float x1,float y1)
{
outcode outcode0,outcode1,outcodeOut;
int accept = FALSE,done = FALSE;
outcode0 = CompOutCode(x0,y0);
outcode1 = CompOutCode(x1,y1);
do
{
if(!(outcode0|outcode1))
{
accept = TRUE;
done = TRUE;
}
else
if(outcode0 & outcode1)
done = TRUE;
else
{
float x,y;
 
outcodeOut = outcode0?outcode0:outcode1;
if(outcodeOut & TOP)
{
x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
}
else
if(outcodeOut & BOTTOM)
{
x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
}
else
if(outcodeOut & RIGHT)
{
y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
else
{
y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
}
if(outcodeOut==outcode0)
{
x0 = x;
y0 = y;
outcode0 = CompOutCode(x0,y0);
}
else
{
x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1);
}
}
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(150,20,"POLYGON AFTER CLIPPING");
 
rectangle(xmin,ymin,xmax,ymax);
}
outcode CompOutCode(float x,float y)
{
outcode code = 0;
if(y>ymax)
code|=TOP;
else
if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else
if(x<xmin)
code|=LEFT;
return code;
}
void main( )
{
float x1,y1,x2,y2;
/* request auto detection */
int gdriver = DETECT, gmode, n,poly[14],i;
clrscr( );
printf("Enter the no of sides of polygon:");
scanf("%d",&n);
printf("\nEnter the coordinates of polygon\n");
for(i=0;i<2*n;i++)
{
scanf("%d",&poly[i]);
}
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
printf("Enter the rectangular coordinates of clipping window\n");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
 
outtextxy(150,20,"POLYGON BEFORE CLIPPING");
drawpoly(n+1,poly);
rectangle(xmin,ymin,xmax,ymax);
getch( );
cleardevice( );
for(i=0;i<n;i++)
clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]);
getch( );
restorecrtmode( );
}

/*
OUTPUT:
Enter the no of sides of polygon:5
Enter the coordinates of polygon
50
50
200
100
350
350
80
200
40
80
Enter the rectangular coordinates of clipping window
150
150
300
300*/


Video lecture on Clipping Lines and Polygons...
Click Here to Watch lecture Now..

14 comments:

  1. thank you karish(vjay)

    ReplyDelete
  2. excellent.......but you can declare the variable name as simple as possible........

    ReplyDelete
  3. yea.. I tried my level best in working of this program and i decleared some complex names their ... i will definaltely try to use as simple names as possible.. thnxxx RAJA for ur suggestion..

    ReplyDelete
  4. thank you...! it works just fine... but what if the clipping rectangle is in the polygon? have you considered that case? because i get a wrong result... thanks again

    ReplyDelete
  5. @Hasapian very nice question,, right now i m busy with my exams, but later i will definatly answer this question , their r some more algorithms that i will try to explain. thnxx for putting this qusn...

    ReplyDelete
  6. thnx brother..ystrday oua sir tell us 2 do d pgm..@ dat tym ur coding mak so helpful...v r vry thnxful 2 u...nice...rly proud f u..

    ReplyDelete
  7. gud job ankit..m doing a project on it.. its helped me..thanks a lot :)

    ReplyDelete
  8. thanks, acha program hai helpful rha direct mil gya,jyada mhnet nhi krni padi.thanks dear

    ReplyDelete
  9. i can't run... can you send the code to me for email?

    ReplyDelete
  10. Indent your goddamn code.

    ReplyDelete
  11. thank you so much for this simple program of clipping

    ReplyDelete