You are on page 1of 5

Title: Write C++ program to implement Cohen Southerland line clipping algorithm.

Objective: Identify different clipping algorithms of computer graphics

Outcomes: Implement computer graphics programs in C++ using polygon clipping


techniques.

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)

Fig. Types of lines

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.

xmin, xmax are coordinates of the window.

ymin, ymax are also coordinates of the window.


x1>xmax
x2>xmax
y1>ymax
y2>ymax
x1<xmin
x2<xmin
y1<ymin
y2<ymin

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.

Advantage of Cohen Sutherland Line Clipping:


1. It calculates end-points very quickly and rejects and accepts lines quickly.
2. It can clip pictures much large than screen size.
3. Easy to understand
4. Simple to implement
5. Best suitable for the lines fully inside or outside
6. Easily be extended for 3D line clipping
7. Repeated clipping is expensive
8. Only applicable to rectangular clipping window. It cannot handle any other shape
9. It can be improved using more regions

Algorithm of Cohen Sutherland Line Clipping:

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

Input line co-ordinates from user

Draw a window for clipping

V=0 V=1
Visibility

Draw a line as per given coordinates V=2 Display line is out of range

Calculate new coordinates for line as it is partially visible

checking

Line coordinates Line coordinates


are greater than are greater than
x coordinate of y coordinate of
clipping region clipping region

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

You might also like