Friday 27 September 2013

Sutherland Hodgeman Polygon Clipping Aglorithm | Computer Graphics Lab Manual | IT department | Final Year




Aim : 
To write a C program to implement SUTHERLAND – HODGEMANN polygon 
clipping algorithm.


Algorithm: 
Step 1: Start the program.
Step 2: Input Coordinates of all vertices of the polygon
Step 3: Input coordinates of the clipping window 
Step 4: Consider the left edge of the window
Step 5: Compare the vertices of each edge of the polygon , individually with the clipping plane 
Step 6: Save the resulting intersections and vertices in the new list of vertices according to four 
possible relationships between the edge and the clipping boundary discussed 
earlier 
Step 7: Repeat the steps 4 and 5 for remaining edges of the clipping window. Each time the 
resultant list of vertices is successively passed to process the next edge of the 
clipping window
Step 8: Stop the Program. 



PROGRAM: 

#include  
#include  
#include  
#include  
#include  
#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 xwmin,xwmax,ywmin,ywmax; 
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)*(ywmax-y0)/(y1-y0); 
 y = ywmax; 
 } 
 else 
 if(outcodeOut & BOTTOM) 
 { 
 x = x0+(x1-x0)*(ywmin-y0)/(y1-y0); 
 y = ywmin; 
 } 
 else 
 if(outcodeOut & RIGHT) 
 { 
 y = y0+(y1-y0)*(xwmax-x0)/(x1-x0); 
 x = xwmax; 
 } 
 else 
 { 
 y = y0+(y1-y0)*(xwmin-x0)/(x1-x0); 
 x = xwmin; 
 } 
 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(xwmin,ywmin,xwmax,ywmax); 

outcode CompOutCode(float x,float y) 

 outcode code = 0; 
 if(y>ywmax) 
 code|=TOP; 
 else 
 if(y
 code|=BOTTOM; 
 if(x>xwmax) 
 code|=RIGHT; 
 else 
 if(x
 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 font="" i="" n="" nbsp="">

 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",&xwmin,&ywmin,&xwmax,&ywmax); 
/* initialize graphics and local variables */ 
initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); 
outtextxy(150,20,"POLYGON BEFORE CLIPPING"); 
drawpoly(n+1,poly); 
rectangle(xwmin,ywmin,xwmax,ywmax); 
getch( ); 
cleardevice( ); 
for(i=0;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 
POLYGON CLIPPING 
RESULT: 
Thus the above program has been executed and output is verified. 
After clipping 
Before clipping 




No comments:

Don't You Think this Awesome Post should be shared ??
| Sutherland Hodgeman Polygon Clipping Aglorithm | Computer Graphics Lab Manual | IT department | Final Year |
Back To Top Related Posts Plugin for WordPress, Blogger...