Group a Assignment 2 Cohen-clipping-Algo

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Title: Cohen Southerland line clipping algorithm

Problem Statement: Write C++ program to implement Cohen Southerland line clipping
algorithm

Pre-requisite
1. Basic programming skills of C++
2. 64-bit Open source Linux
3. Open Source C++ Programming tool like G++/GCC

Objective :

1. To understand and implement Cohen Southerland line clipping algorithm

Theory:

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:

1. Visible
2. Not Visible
3. Clipping Case

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.
The center area is having the code, 0000, i.e., region 5 is considered a rectangle window.

Algorithm of Cohen Sutherland Line Clipping:

Step1:Calculate positions of both endpoints of the line

Step2:Perform OR operation on both of these end-points

Step3:If the OR operation gives 0000


Then
line is considered to be visible
else
Perform AND operation on both endpoints
If And ≠ 0000
then the line is invisible
else
And=0000
Line is considered the clipped case.

Step4:If a line is clipped case, find an intersection with boundaries of the window
m=(y2-y1 )(x2-x1)

(a) If bit 1 is "1" line intersects with left boundary of rectangle window
y3=y1+m(x-X1)
where X = Xwmin
where Xwminis the minimum value of X co-ordinate of window

(b) If bit 2 is "1" line intersect with right boundary


y3=y1+m(X-X1)
where X = Xwmax
where X more is maximum value of X co-ordinate of the window
(c) If bit 3 is "1" line intersects with bottom boundary
X3=X1+(y-y1)/m
where y = ywmin
ywmin is the minimum value of Y co-ordinate of the window

(d) If bit 4 is "1" line intersects with the top boundary


X3=X1+(y-y1)/m
where y = ywmax
ywmax is the maximum value of Y co-ordinate of the window

Conclusion: Thus we successfully studied Cohen Sutherland Line Clipping


Questions:
Solve example using cohen Sutherland line cliiping algorithm.
What are advantages and drawbacks of cohen Sutherland Line clipping algo?

You might also like