Professional Documents
Culture Documents
01KP102021010310020
01KP102021010310020
Theory:
Introduction:
Cohen Sutherland Line drawing algorithm is a 2D line clipping algorithm
Main advantage- vastly reduces the number of line intersections that must be
calculated in scan conversion approach Operates in 2 phases
• Region Code Generation
• Clipping
In the algorithm, first of all, it is detected whether line lies inside the screen or it is
outside the screen. All lines come under any one of the following categories:
• Visible(Completely inside)
• Not Visible(Completely outside)
• Clipping Case(Partially inside)
1. Visible: If a line lies within the window, i.e., both endpoints of the line lies within
the window. A line is visible and will be displayed as it is.
2. Not Visible: If a line lies outside the window it will be invisible and rejected. Such
lines will not display. If any one of the following inequalities is satisfied, then the line
is considered invisible. Let A (x1,y2) and B (x2,y2) are endpoints of line.
3. Clipping Case: If the line is neither visible case nor invisible case. It is considered
to be clipped case. First of all, the category of a line is found based on nine regions
given below. All nine regions are assigned codes. Each code is of 4 bits. If both
endpoints of the line have end bits zero, then the line is considered to be visible.
Step 1: read (xmin, ymin) and (xmax, ymax) lower left and top right corner of
clipping window
Step 2: read A(x1, y1) and B(x2, y2) endpoints of line
Step 3: compute outcode of A and B
if y1>ymax then outcode_A(1)=1 else 0
if y1<ymin then outcode_A(2)=1 else 0
if x1>xmax then outcode_A(3)=1 else 0
if x1<xmin then outcode_A(4)=1 else 0
if y2>ymax then outcode_B(1)=1 else 0
if y2<ymin then outcode_B(2)=1 else 0
if x2>xmax then outcode_B(3)=1 else 0
if x2<xmin then outcode_B(4)=1 else 0
Step 4:
if outcode_A OR outcode_B==0000
then display entire line and goto step 5
else if outcode_A AND outcode_B!=0000
then reject the entire line
else
compute intersection point with window boundaries repeat Step 4
Step 5: Stop
Conclusion: It helps us to efficiently determine the lines and portions of lines that are
visible in the central region of interest (the viewport).
Flowchart:
Start
V=0 V=1
Visibility
Draw a line as per given coordinates V=2 Display line is out of range
checking
Reset end point using formula: Reset end point using formula:
x_new=(p1.x+(m*(x-p1.y))) y_new=p1.x+(y-p1.y)/m;
where p1 is prev. x coordinates where p1 is prev. y coordinates
m is slope of line m is slope of line
Stop