Lecture 8-Clipping in 2D
Lecture 8-Clipping in 2D
03/06/25 1
Windowing
A scene is made up of a collection of objects
specified in world coordinates
wymin
wxmin wxmax
03/06/25 World Coordinates 3
Windowing
Because drawing things to a display takes time
we clip everything outside the window
Window
wymax
wymin
wxmin wxmax
03/06/25 World Coordinates 4
Clipping
For the image below consider which lines and
points should be kept and which ones should be
clipped
P4
Window P2
wymax
P6
P3
P1
P7 P5
P9
P8
wymin
P10
Window P2
wymax
Clipped
P5
P1
P7 Points Within the Window
are Not Clipped
P9 P8
wymin
Clipped P10
Both end-points
Don’t know!
outside the window
Brute Force Line Clipping
Brute force line clipping can be performed as
follows:
– Don’t clip lines with both
end-points within the
window
– For lines with one end-
point inside the window
and one end-point
outside, calculate the
intersection point (using the equation of the line)
and clip from this point out
03/06/25 8
Brute Force Line Clipping (cont…)
– For lines with both end-
points outside the window
test the line for
intersection with all of the
window boundaries, and
clip appropriately
Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
wxmin wxmax
03/06/25 12
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] P14 [0110]
Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
03/06/25 15
Cohen-Sutherland: Other Lines (cont…)
– Otherwise, compare the remainder of the line against
the other window boundaries
– Continue until the line is either discarded or a
segment inside the window is found
We can use the region codes to determine which
window boundaries should be considered for
intersection
– To check if a line crosses a particular boundary we
compare the appropriate bits in the region codes of
its end-points
– If one of these is a 1 and the other is a 0 then the line
crosses the boundary
03/06/25 16
Cohen-Sutherland Examples
Consider the line P9 to P10 below
– Start at P10 Window
wy
– From the region codes max
P [0100]10
– Calculate the wx wx
intersection of the line with the bottom boundary to
min max
left boundary so
calculate the
intersection point to wxmin wxmax
generate P7’
03/06/25 19
Cohen-Sutherland Examples (cont…)
Window
wymax
wymin
wxmin wxmax
03/06/25 21
Calculating Line Intersections
Intersection points with the window boundaries
are calculated using the line-equation parameters
– Consider a line with the end-points (x1, y1) and (x2,
y2)
– The y-coordinate of an intersection with a vertical
window boundary can be calculated using:
y = y1 + m (xboundary - x1)
where xboundary can be set to either wxmin or wxmax
03/06/25 22
Calculating Line Intersections (cont…)
03/06/25 23
Compute the points of intersections of the
following clipping region with line AB
B(8,10)
4,8 10, 8
A(2,5)
4,3 10, 3
03/06/25 24
Area Clipping
Similarly to lines, areas
must be clipped to a
window boundary
Consideration must be
taken as to which portions
of the area must be clipped
03/06/25 25
Sutherland-Hodgman Area Clipping
Algorithm
Sutherland
A technique for clipping areas turns up
again. This
developed by Sutherland & time with
Gary Hodgman with
Hodgman whom he worked at
the first ever
Put simply the polygon is clipped graphics company
by comparing it against each Evans & Sutherland
boundary in turn
Original
03/06/25 Area Clip Left Clip Right Clip Top Clip Bottom
26
Sutherland-Hodgman Area Clipping
Algorithm (cont…)
To clip an area against an individual boundary:
– Consider each vertex in turn against the boundary
– Vertices inside the boundary are saved for clipping
against the next boundary
– Vertices outside the boundary are clipped
– If we proceed from a point inside the boundary to
one outside, the intersection of the line with the
boundary is saved
– If we cross from the outside to the inside intersection
point and the vertex are saved
03/06/25 27
Sutherland-Hodgman Example
Each example shows S
the point being
processed (P) and the P
I
S
S
No Points Saved Save Points I & P
03/06/25 28
Other Area Clipping Concerns
Clipping concave areas can be a little more tricky
as often superfluous lines must be removed
03/06/25 30
Parametric equation
• The variable t (or u for some authors) is called a
parameter and the relations between x, y and t
are called parametric equations.
03/06/25 31
Cont,
• Parametric equations are commonly used to
express the coordinates of the points that
make up a geometric object such as a curve or
surface, in which case the equations are
collectively called a parametric
representation or parameterization of the
object.
03/06/25 32
Liang Barsky
• With these intersections it knows which
portion of the line should be displayed
(drawn). This algorithm is significantly more
efficient than Cohen–Sutherland.
03/06/25 33
• The idea of the Liang-Barsky clipping
algorithm is to do as much testing as possible
before computing line intersections. Consider
first the usual parametric form of a straight
line:
03/06/25 34
A point is in the clip window, if
and
03/06/25 35
• Where
-Left
-Right
-Bottom
-Top
03/06/25 36
To compute the final line segment:
1. A line parallel to a clipping window edge has
Pk=0 for that boundary.
2. If for that k, qk <0, the line is completely
outside and can be eliminated.
3. When Pk<0 the line proceeds from outside to
inside the clip window and when Pk>0 , the
line proceeds from inside to outside.
03/06/25 37
4. For nonzero Pk, U=qk/pk gives the intersection
point.
5. For each line, calculate U1 and U2
6. For U1, look at the boundaries for which Pk<0
(outside-> in). Take U1 to be the largest among
(0,qk/pk)
7. For U2, look at the boundaries for which Pk>0
(inside-> out). Take U2 to be the minimum among
(1,qk/pk)
8. If U1>U2, the line is outside and therefore is
rejected
03/06/25 38
• Example 1
• (i)Describe Liang Barsky clipping algorithm
• (ii) Let P1(-1,-2) and P2(2,4) be end points of a
line to be clipped using Liang Barsky technique.
• Let the boundaries of viewport window be as
follows: Left boundary(XL) = 0, Right
boundary(XR) = 1, Bottom boundary
(YB) = 0 and Top boundary(YT) = 1.
Clip the line. Show computations.
03/06/25 39
P1(2,4)
P1(-1,-2)
03/06/25 40
• It is given P1 (-1, -2), P2 (2, 4)
• XL = 0, XR = 1, YB = 0, YT = 1
• dx = 2 - (-1) = 3 dy = 4 - (-2) = 6
• P1 = -dx = -3 q1 = x1 - XL = -1 - 0 = -1 q1 / P1 = 1/3
• P2 = dx = 3 q2 = XR - x1 = 1 - (-1) = 2 q2 / P2 = 2/3
• P3 = -dy = -6 q3 = y1 - YB = -2 q3 / P3 = 1/3
• P4 = dy = 6 q4 = YT - y1 = 3 q4 / P4 = 1/2
• for (Pi < 0) t1="MAX" ( 1 / 3, 1 / 3, 0 )="1" / 3
• for (Pi > 0) t2 = MIN ( 2 / 3, 1 / 2, 1 ) = 1 / 2
• Since t1 < t2 there is a visible section
• Notes: From the notes, U1==t1 AND U2==t2
03/06/25 41
• compute new endpoints
• t1 = 1 / 3 x1' = x1 + dx . t1 = -1 + (3 . 1 / 3) = 0
• y1' = y1 + dy . t1 = -2 + (6 . 1 / 3) = 0
• t2 = 1 / 2 x2' = x1 + dx . t2 = -1 + (3 . 1 / 2) = 1 /
2
• y2' = y1 + dy . t2 = -1 + (6 . 1 / 2) = 1
03/06/25 42
Example 2 - Line Passing Through Window
03/06/25 43
03/06/25 44
03/06/25 45
Example 3 - Line Not Passing
Through Window
03/06/25 46
03/06/25 47
03/06/25 48
Summary
Objects within a scene must be clipped to
display the scene in a window
Because there can be so many objects clipping
must be extremely efficient
The Cohen-Sutherland algorithm can be used for
line clipping
The Sutherland-Hodgman algorithm can be used
for area clipping
Liang Barsky is more efficient than Cohen-
Sutherland
03/06/25 49
Questions
03/06/25 50