3. INTRODUCTION
In computer graphics, line clipping is the process of removing lines or portions
of lines outside an area of interest. Typically, any line or part thereof which is outside
of the viewing area is removed.
There are two common algorithms for line clipping: Cohen–Sutherland and Liang–
Barsky.
4. CLIPPING LINES
• If a line is partially in the viewport, then we need to
recalculate its endpoints.
• In general, there are four possible cases.
• The line is entirely within the viewport.
• The line is entirely outside the viewport.
• One endpoint is in and the other is out.
• Both endpoints are out, but the middle part is in.
5. CLIPPING LINE
• Clipping rectangle:
• xmin to xmax
• ymin to ymax
• A point (x,y) lies within a clip
rectangle and thus displayed if
following conditions are hold
• xmin <= x <= xmax
• ymin <= y <= ymax
6. CASES
Here are a few cases, where the black rectangle represents the screen, in red are the
old endpoints, and in blue the ones after clipping:
11. COHEN-SUTHERLAND LINE CLIPPING
ALGORITHM
1. End points are checked to for trivial acceptance
• If both endpoints are inside the clip rectangle boundary
2. If not trivially accepted, region check is done for trivial rejection
• Both endpoints have x co-ordinate less than xmin
region to the left edge of clip rectangle
• Both endpoints have x co-ordinate greater than xmax
region to the right edge of clip rectangle
• Both endpoints have y co-ordinate less than ymin
region below the bottom edge of clip rectangle
• Both endpoints have y co-ordinate greater than ymax
region above the top edge of clip rectangle
12. COHEN-SUTHERLAND LINE CLIPPING
ALGORITHM
3. If neither trivially accepted, nor trivially rejected
• Divided into two segments at the intersection point of a clip edge, such that
• one segment can be trivially
rejected/accepted
• Another segment is subdivided
iteratively until cannot be trivially
rejected or accepted.
13. COHEN-SUTHERLAND LINE CLIPPING
ALGORITHM• Extend the edges of the clip rectangle
• Divide the plane into 9 regions
• Each region is assigned a 4 bit code
called outcode
• First bit is 1 if the region is
above the top edge, 0 otherwise
• Second bit is 1 if the region is
below the bottom edge, 0 otherwise
• Third bit is 1 if the region is
right to the right edge, 0 otherwise
• Fourth bit is 1 if the region is
left to the left edge, 0 otherwise
14. COHEN-SUTHERLAND LINE CLIPPING
ALGORITHM• Each endpoint of the line segment is assigned the outcode
• If both endcodes are 0000 then
it completely lies inside the
clip rectangle
trivial acceptance
A:0000
B:0000
• Otherwise perform the
logical AND of the outcodes.
If results in non zero
trivial rejection
C:1000
D:1010
A
B
C
D
AND gives 1000 above the top edge
15. COHEN-SUTHERLAND LINE CLIPPING
ALGORITHM• If neither trivially accepted nor rejected, subdivide
it based on a intersecting edge
• Select the outcode of the endpoint
that lies outside
• Choose a set bit in that outcode
for selecting the edge for
subdivision
Let us choose:
topbottomrightleft order
leftmost set bit in the outcode is used
for selecting the edge for subdivision
16. THE COHEN-SUTHERLAND CLIPPING
ALGORITHM
• Let the x-coordinates of the window boundaries be xmin and xmax and let the y-
coordinates be ymin and ymax.
xmin xmax
ymin
ymax
17. THE COHEN-SUTHERLAND CLIPPING
ALGORITHM
• For each endpoint p of a line segment we will define a
codeword c3c2c1c0 consisting of 4 true/false values.
• c3 = true, if p is left of the window.
• c2 = true, if p is above the window.
• c1 = true, if p is right of the window.
• c0 = true, if p is below the window.
18. THE COHEN-SUTHERLAND CLIPPING
ALGORITHM
• Consider the vertical edge x = xmin. (You can do the other edges.)
• Compare the x-coordinate of p to xmin.
• If it is less, then “set” bit 3 of the codeword.
if (p.x < xmin)
c = 1 << 3;
19. THE COHEN-SUTHERLAND CLIPPING
ALGORITHM
• After the codewords for both endpoints are computed, we divide the possibilities
into three cases:
• Trivial accept – both codewords are FFFF.
• Trivial reject – both codewords have T in the same position.
• Indeterminate so far – Investigate further.
26. THE COHEN-SUTHERLAND CLIPPING
ALGORITHM
• Consider again the vertical edge x = xmin. (You can do the other edges.)
• Compute codeword1 ^ codeword2.
• This shows where they disagree.
• Why?
• “And” this with (1 << 3).
• If result = 1, then clip.
• Else do not clip.
27. THE COHEN-SUTHERLAND CLIPPING
ALGORITHM
• If we clip, then the new point will be on the line x = xmin.
• So its x-coordinate will be xmin.
• We must calculate its y-coordinate.
28. • In this algorithm it divides lines & edges into 2 cases.
1) Trivially Accept and
2) Trivially Reject.
29. CONDITIONS OF TRIVIALLY ACCEPT
Xmin ≤ X ≤ Xmax
Ymin ≤ Y ≤ Ymax
Lines fulfill this conditions then we will mark those lines as
trivially accept.