CGL Practical 8
CGL Practical 8
Problem Statement: Implement Cohen Sutherland line clipping method to clip the
line with respect to the view-port and window. Use mouse click, keyboard interface.
Objective;
i) To study what the clipping is?
ii) To study and implement the Cohen Sutherland line clipping algorithm.
Theory:
In computer graphics, 'line clipping' is the process of removing lines or portions of lines
outside of an area of interest. Typically, any line or part thereof which is outside of the
viewing area is removed.
The algorithm was developed in 1967 during flight simulator work by Danny Cohen and
Ivan Sutherland
The design stage includes, excludes or partially includes the line based on where:
● Both endpoints are in the viewport region (bitwise OR of endpoints == 0): trivial
accept.
● Both endpoints share at least one non-visible region which implies that the line
does not cross the visible region. (bitwise AND of endpoints != 0): trivial reject.
● Both endpoints are in different regions: In case of this nontrivial situation the
algorithm finds one of the two points that is outside the viewport region (there
will be at least one point outside). The intersection of the outpoint and extended
viewport border is then calculated (i.e. with the parametric equation for the line)
and this new point replaces the outpoint. The algorithm repeats until a trivial
accept or reject occurs.
The numbers in the figure below are called outcodes. The outcode is computed for each
of the two points in the line. The outcode will have four bits for two-dimensional clipping,
or six bits in the three-dimensional case. The first bit is set to 1 if the point is above the
viewport. The bits in the 2D outcode represent: Top, Bottom, Right, Left. For example the
outcode 1010 represents a point that is top-right of the viewport. Note that the outcodes
for endpoints must be recalculated on each iteration after the clipping occurs.
Algorithm
Steps
3) if OR = 0000,
else
i) if AND != 0000,
then the line is invisible and not inside the window. Also,
it can’t be considered for clipping.
ii) else
4) After confirming that the line is partially inside the window, then we find
the intersection with the boundary of the window. By using the following
formula:-
Slope:- m= (y2-y1)/(x2-x1)
a) If the line passes through the top or the line intersects with the top
boundary of the window.
x = x + (y_wmax – y)/m
y = y_wmax
b) If the line passes through the bottom or the line intersects with the
bottom boundary of the window.
x = x + (y_wmin – y)/m
y = y_wmin
c) If the line passes through the left region or the line intersects with the
left boundary of the window.
y = y+ (x_wmin – x)*m
x = x_wmin
d) If the line passes through the right region or the line intersects with the
right boundary of the window.
y = y + (x_wmax -x)*m
x = x_wmax
5) Now, overwrite the endpoints with a new one and update it.
6) Repeat the 4th step till your line doesn’t get completely clipped
Given a set of lines and a rectangular area of interest, the task is to remove
lines that are outside the area of interest and clip the lines which are partially
inside the area.
Example:
Conclusion: We have studied and implemented the Cohen Sutherland Line Clipping
Algorithm.