0% found this document useful (0 votes)
30 views50 pages

Lecture 8-Clipping in 2D

The document discusses 2D clipping techniques for rendering scenes within specified window boundaries, focusing on point and line clipping methods. It introduces the Cohen-Sutherland algorithm for efficient line clipping, which uses region codes to determine line intersections with window boundaries, and the Sutherland-Hodgman algorithm for area clipping. Additionally, it covers the Liang-Barsky algorithm, which is more efficient than Cohen-Sutherland by utilizing parametric equations to determine line intersections with clipping windows.

Uploaded by

joylineselim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views50 pages

Lecture 8-Clipping in 2D

The document discusses 2D clipping techniques for rendering scenes within specified window boundaries, focusing on point and line clipping methods. It introduces the Cohen-Sutherland algorithm for efficient line clipping, which uses region codes to determine line intersections with window boundaries, and the Sutherland-Hodgman algorithm for area clipping. Additionally, it covers the Liang-Barsky algorithm, which is more efficient than Cohen-Sutherland by utilizing parametric equations to determine line intersections with clipping windows.

Uploaded by

joylineselim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 50

Clipping in 2D

03/06/25 1
Windowing
A scene is made up of a collection of objects
specified in world coordinates

03/06/25 World Coordinates 2


Windowing
When we display a scene only those objects
within a particular window are displayed
Window
wymax

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

03/06/25 wxmin wxmax 5


Point Clipping
Easy - a point (x,y) is not clipped if:
wxmin ≤ x ≤ wxmax AND wymin ≤ y ≤ wymax
otherwise it is clipped
P4 Clipped
Clipped

Window P2
wymax
Clipped
P5
P1
P7 Points Within the Window
are Not Clipped
P9 P8
wymin
Clipped P10

03/06/25 wxmin wxmax 6


Line Clipping
Harder - examine the end-points of each line to
see if they are in the window or not
Situation Solution Example

Both end-points inside


Don’t clip
the window

One end-point inside


the window, one Must clip
outside

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

However, calculating line intersections is


computationally expensive
Because a scene can contain so many lines,
the brute force approach to clipping is much
too slow
03/06/25 9
Cohen-Sutherland Clipping Algorithm
An efficient line clipping
algorithm
The key advantage of the
algorithm is that it vastly
reduces the number of line
intersections that must be Dr. Ivan E. Sutherland
co-developed the Cohen-
calculated Sutherland clipping
algorithm. Sutherland is
a graphics giant and
includes amongst his
Cohen is something of a mystery – can achievements the
anybody find out who he was? invention of the head
03/06/25
mounted display. 10
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


03/06/25 11
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] 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]

03/06/25 wxmin wxmax 13


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 AND operation can efficiently check this
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]

03/06/25 wxmin wxmax 14


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:
– Compare an end-point outside the window to a
boundary (choose any order in which to consider
boundaries e.g. left, right, bottom, top) and
determine how much can be discarded
– If the remainder of the line is entirely inside or
outside the window, retain it or clip it respectively

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

of the two end-points we


know the line doesn’t P [0000]
cross the left or right
9
wymin
P ’ [0000]
boundary
10

P [0100]10

– Calculate the wx wx
intersection of the line with the bottom boundary to
min max

generate point P10’


– The line P9 to P10’ is completely inside the window so
03/06/25 is retained 17
Cohen-Sutherland Examples (cont…)

Consider the line P3 to P4 below P4’ [1001]


P4 [1000]
Window
– Start at P4 wymax

– From the region codes P [0001]


3

of the two end-points


we know the line
wymin
crosses the left
boundary so calculate
the intersection point to
wxmin wxmax
generate P4’
– The line P3 to P4’ is completely outside the window so
is clipped
03/06/25 18
Cohen-Sutherland Examples (cont…)

Consider the line P7 to P8 below


– Start at P7 Window
wymax
– From the two region
codes of the two P7’ [0000]
end-points we know P7 [0001] P8 [0010]

the line crosses the wymin


P8’ [0000]

left boundary so
calculate the
intersection point to wxmin wxmax
generate P7’

03/06/25 19
Cohen-Sutherland Examples (cont…)

Consider the line P7’ to P8


– Start at P8 Window
wymax
– Calculate the
intersection with the P7’ [0000]
P7 [0001] P8 [0010]
right boundary to P8’ [0000]
wymin
generate P8’
– P7’ to P8’ is inside
wxmin wxmax
the window so is
retained
03/06/25 20
Cohen-Sutherland Worked Example

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…)

– The x-coordinate of an intersection with a


horizontal window boundary can be calculated
using:
x = x1 + (yboundary - y1) / m
where yboundary can be set to either wymin or wymax
– m is the slope of the line in question and can be
calculated as m = (y2 - y1) / (x2 - x1)

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

previous point (S) P


Saved points define Save Point P Save Point I

area clipped to the P S


boundary in question I P

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

Window Window Window Window

Clipping curves requires more work


– For circles we must find the two intersection
points on the window boundary
03/06/25 29
Liang–Barsky algorithm

• Liang–Barsky algorithm (named after You-


Dong Liang and Brian A. Barsky) is a line
clipping algorithm.
• The Liang–Barsky algorithm uses the
parametric equation of a line and inequalities
describing the range of the clipping window to
determine the intersections between the line
and the clipping window.

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

which can be expressed as the 4 inequalities:

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

You might also like