CG Exp07 (A07)
CG Exp07 (A07)
LAB Manual
PART A
(PART A : TO BE REFFERED BY STUDENTS)
Experiment No.07
A.1 Aim:
Implement Sutherland Hodgeman polygon clipping algorithm.
A.2 Prerequisite:
1.C Language.
2.Geometric Concepts.
3.Concept of 2D basic Transformations.
4.Clipping Concepts.
A.3 Outcome:
After successful completion of this experiment students will be able
to,
The following figures show how this algorithm works at each edge, clipping
the polygon.
As the algorithm goes around the edges of the window, clipping the
polygon, it encounters four types of edges. All four edge types are
illustrated by the polygon in the following figure. For each edge type, zero,
one, or two vertices are added to the output list of vertices that define the
clipped polygon.
The four types of edges are:
Edges that are totally inside the clip window. - add the second inside vertex
point
Edges that are leaving the clip window. - add the intersection point as a
vertex
Edges that are entirely outside the clip window. - add nothing to the vertex
output list
Edges that are entering the clip window. - save the intersection and inside
points as vertices
Assume that we're clipping a polgon's edge with vertices at (x1,y1) and
(x2,y2) against a clip window with vertices at (xmin, ymin) and
(xmax,ymax).
The location (IX, IY) of the intersection of the edge with the left side of the
The location of the intersection of the edge with the right side of the window
is:
i) IX = xmax
ii) IY = slope*(xmax-x1) + y1, where the slope = (y2-y1)/(x2-x1)
The intersection of the polygon's edge with the top side of the window is:
Finally, the intersection of the edge with the bottom side of the window is:
Example:
A.5 Procedure:
Sutherland Hodgeman Algorithm
5. 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
6. 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
7. Stop
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <process.h>
outcode CompOutCode(float
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
y1)
outcode
outcode0,outcode1,outcodeOut; int
outcode0 = CompOutCode(x0,y0);
outcode1 = CompOutCode(x1,y1);
do
if(!(outcode0|outcode1))
accept = TRUE;
done = TRUE;
else
done = TRUE;
else
float x,y;
outcodeOut = outcode0?outcode0:outcode1;
x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
x = x0+(x1-x0)*(ymin-
y0)/(y1-y0); y = ymin;
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);
rectangle(xmin,ymin,xmax,ymax);
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( )
gmode, n,poly[14],i;
clrscr( );
for(i=0;i<2*n;i++)
scanf("%d",&poly[i]);
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
CLIPPING"); drawpoly(n+1,poly);
rectangle(xmin,ymin,xmax,ymax);
clip(poly[2*i],poly[(2*i)+1],poly[(2*i)
+2],poly[(2*i)+3]); getch( );
restorecrtmode( );
Output:
B.3 Observations and learning:
The Sutherland-Hodgman clipping algorithm finds the polygon that is the
intersection between an arbitrary polygon (the "subject polygon") and a
convex polygon (the "clip polygon"). It is used in computer graphics
(especially 2D graphics) to reduce the complexity of a scene being displayed
by eliminating parts of a polygon that do not need to be displayed.
B.4 Conclusion:
It is performed by processing the boundary of polygon against each window
corner or edge. First of all entire polygon is clipped against one edge, then
resulting polygon is considered, then the polygon is considered against the
second edge, so on for all four edges.
B.5 Question of Curiosity
Q.1] Implement Sutherland Hodgeman Polygon Clipping Algorithm.
ANS:
Advantages:
1. Simplicity to implement.
Disadvantages:
************************