You are on page 1of 14

CG (Line clipping: Cohen

Sutherland Line clipping)


Dr. Tathagata Ray
Assistant Professor, BITS Pilani, Hyderabad Campus
BITS Pilani
Hyderabad Campus rayt@hyderabad.bits-pilani.ac.in
Cohen-Sutherland Line
Clipping

• This algorithm is more efficient than the brute force


algorithm because it employs initial tests on a line to
determine whether intersection calculations can be
avoided.

• Trivial acceptance and rejection is done efficiently.

• More work will be required when a line segments is not


trivially accepted or rejected.

2
BITS Pilani, Hyderabad Campus
Cohen-Sutherland: World
Division
• World space is divided into regions based on the window
boundaries
– Each region has a unique four bit region code
– Region codes indicate the position of the regions with
respect to the window

1001 1000 1010


3 2 1 0
0000
above below right left 0001 0010
Window
Region Code Legend
0101 0100 0110

3
BITS Pilani, Hyderabad Campus
Cohen-Sutherland: Labelling

Every end-point is labelled with the appropriate region code

P11 [1010]
P4 [1000]
Window
wymax P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7
[0001 P9 [0000] P8 [0010]
wymin ]
P10 [0100]
P13 [0101] P2 [0110]
wxmin wxmax

First bit: above top edge y > wymax


Second bit: below bottom edge y < wymin
Third bit: to right of right edge x > wxmax
Fourth bit: to left of left edge x < wxmin 4
BITS Pilani, Hyderabad Campus
Cohen-Sutherland: Lines In
The Window
•Lines completely contained within the window boundaries
have region code [0000] for both end-points, so are not
clipped.

P11 [1010]
P4 [1000]

Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P2 [0110]

wxmin wxmax 5
BITS Pilani, Hyderabad Campus
Cohen-Sutherland: Lines
Outside The Window
•Any lines with a common set bit in the region codes of
both end-points can be clipped
– The logical AND of codes can efficiently check this. A
non-zero result would mean it is outside.

P11 [1010]
P4 [1000]

Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P2 [0110]

wxmin wxmax 6
BITS Pilani, Hyderabad Campus
Cohen-Sutherland: Other
Lines
• Lines that cannot be identified as completely inside or outside
the window may or may not cross the window interior.
• These lines are processed as follows:
– We find an end point that lies outside the clipping window.
– Then use the outcode bit that is set to determine a clip
edge; the edge chosen is the first in the above-to-below,
right-to-left order i.e. it is the leftmost bit that is set in the
outcode.
– Determine the corresponding intersection point.
– Clip off the line segment from the outside endpoint to the
intersection point.
– Replace the outside endpoint with the intersection point.
– Compute the outcode of this new endpoint to prepare for
the next iteration.
7
BITS Pilani, Hyderabad Campus
Non-trivial Examples

Consider the line P9 to P10 below


– Start at outside point P10 Window
– From the region code of P10, it wymax

is evident that it must be


intersecting bottom edge.
P9 [0000]
– Calculate the wymin
P10’ [0000]
intersection of the line with the
P10 [0100]
bottom boundary to generate
point P10’ wxmin wxmax

– The line P9 to P10’ is


completely inside the window
so is retained.
8
BITS Pilani, Hyderabad Campus
Non-trivial
• Start with the outside point
P1 (either end point can be P2 [1000]
X [1000]
selected). Window
wymax
• Outcode of P1 tells that P1 [0001]
there is an intersection with
the left edge.
• Compute the intersection X wymin
with left edge.
• Reject P1 to X.
wxmin wxmax
• Compute outcode of X as
1000.
• Finally XP2 is trivially
rejected. 9
BITS Pilani, Hyderabad Campus
• Start with the outside
point P1 (either end point P2 [1000]

can be selected). Window


wymax
X [0000]
• Outcode of P1 tells that
there is an intersection P1 [0001]

with the left edge.


• Compute the intersection wymin
X with left edge.
• Reject P1 to X.
wxmin wxmax
• Compute outcode of X as
0000.

10
BITS Pilani, Hyderabad Campus
• Start with the outside
point P2 (either end point P2 [1000]

can be selected). Window


wymax
X [0000] Y [0000]
• Outcode of P2 tells that
there is an intersection
with the top edge.
• Compute the intersection wymin
Y with top edge.
• Reject P2 to Y.
wxmin wxmax
• Compute outcode of Y as
0000.

11
BITS Pilani, Hyderabad Campus
• Trivially accept the
remaining clipped portion
XY. Window
wymax
X [0000] Y [0000]

wymin

wxmin wxmax

12
BITS Pilani, Hyderabad Campus
Cohen-Sutherland: Overview

• These lines are processed as follows:


– We find an end point that lies outside the clipping
window.
– Then use the outcode bit that is set to determine a
clip edge; the edge chosen is the first in the above-to-
below, right-to-left order i.e. it is the leftmost bit that is
set in the outcode.
– Determine the corresponding intersection point.
– Clip off the line segment from the outside endpoint to
the intersection point.
– Replace the outside endpoint with the intersection
point.
– Compute the outcode of this new endpoint to prepare
for the next iteration.

13
BITS Pilani, Hyderabad Campus
Pros and Cons

• Cohen-Sutherland
– Repeated clipping is expensive
– Best used when trivial acceptance and rejection is
possible for most lines

14
BITS Pilani, Hyderabad Campus

You might also like