Line and Polygon Clipping
Foley & Van Dam, Chapter 3
Topics
• Viewing Transformation Pipeline in 2D • Line and polygon clipping • Brute force analytic solution • CohenSutherland Line Clipping Algorithm • CyrusBeck Line Clipping Algorithm • SutherlandHodgman Polygon Clipping • Sampling Theorem (Nyquist Frequency)
^{y} ^{v}^{i}^{e}^{w}
^{x} ^{v}^{i}^{e}^{w}
y world
y view
x world
World and Viewing Coordinates
Clipping
x view
Device Coordinates
1
1
Normalized Device
Coordinates
• Objects are given in world coordinates • The world is viewed through a window • The window is mapped onto a device viewport
A scene in WorldCoordinates


WC

WorldCoordinates to Viewing Coordinates


VC

Viewing Coordinates to Normalized Device Coordinates


NDC

Normalized Device Coordinates to Device Coordinates

DC
Line and Polygon Clipping
The problem:
Given a set of 2D lines or polygons and a window, clip the lines or polygons to their regions that are inside the window
Motivations
• Efficiency • Display in portion of a screen • Occlusions
clip rectangle
Line Clipping
• We will deal only with lines (segments)
• Our window can be described by two extreme points:
(x _{m}_{i}_{n} ,y _{m}_{i}_{n} ) and (x _{m}_{a}_{x} ,y _{m}_{a}_{x} )
•
A point (x,y) is in the window iff:
x _{m}_{i}_{n} ≤ x ≤ x _{m}_{a}_{x}
and
y _{m}_{i}_{n} ≤ y ≤ y _{m}_{a}_{x}
Brute Force Analytic Solution
S
S
S
WWW
0, 1, or 2 intersections between a line and a window
• The intersection of convex regions is always convex
• Since both W and S are convex, their intersection is convex, i.e a single connected segment of S
Question: Can the boundary of two convex shapes intersect more than twice?
Pseudo Code for Midpoint Line Drawing
Line(x _{0} ,y _{0} ,x _{1} ,y _{1} ) begin
int dx, dy, x, y, d, ∆ _{E} , ∆ _{Ν}_{E} ;
x:= x _{0} ;
y=y _{0} ;

dx := x _{1} x _{0} ; d := 2*dydx;

dy := y _{1} y _{0} ;

∆ _{E} := 2*dy;

∆ _{Ν}_{Ε} := 2*(dydx);

PlotPixel(x,y); while(x < x _{1} ) do
if (d < 0) then d:=d+ _{∆} _{E} ;
x:=x+1;
end; else d:=d+ _{∆} _{Ν}_{E} ;
x:=x+1;
Assume x _{1} >x _{0} and
y:=y+1;
end;
PlotPixel(x,y);
end;
end;
0 < slope ≤ 1
Line Clipping
Midpoint Algorithm: Intersection with a vertical edge
x = x min
M
NE
(x min ,Round(mx min +B))
Q
E
(x min , mx min +B)
y = y min
Line Clipping
Midpoint Algorithm: Intersection with a horizontal edge
x = x min
B
A
y = y min
y = y min 1/2
CohenSutherland for Line Clipping
• Clipping is performed by computing intersections with four boundary segments of the window:
L _{i} ,
i=1,2,3,4
• Purpose: Fast treatment of lines that are trivially inside/outside the window • Let P=(x,y) be a point to be classified against window W
• Idea: Assign P a binary code consisting of a bit for each edge of W. The bit is 1 if the pixel is in the halfplane that does not contain W
CohenSutherland for Line Clipping
bit

1

0

1

y < y min

y ≥ y min

2

y > y max

y ≤ y max

3

x > x max

x ≤ x max

4

^{x} ^{<} ^{x} min

x ≥ x min

^{y} max
^{y} min

4


3

0101

0100

0110

2

0001

0000

0010

1001

1000

1010

1



^{x} max

CohenSutherland for Line Clipping
Given a line segment S from p _{0} =(x _{0} ,y _{0} ) to p _{1} =(x _{1} ,y _{1} ) to be clipped against a window W
If code(p _{0} ) AND code(p _{1} ) is not zero, then S is trivially rejected
If code(p _{0} ) OR code(p _{1} ) is zero, then S is trivially accepted
CohenSutherland for Line Clipping
Otherwise: let assume w.l.o.g. that p _{0} is outside W • Find the intersection of S with the edge corresponding to the MSB in code( p _{0} ) that is equal to 1. Call the intersection point p _{2} . • Run the procedure for the new segment ( p _{1} , p _{2} ).
D
4
3
I
C
B
2
H
A
G
1
F
E
CyrusBeck Line Clipping
^{Q}
N
V(t)
L
^{P} 0
The parametric line P(t)=P _{0} +(P _{1} P _{0} )t The parametric vector V(t)=P(t)Q The segment P _{0} P _{1} intersects the line L at t _{0} satisfying V(t _{0} )·N=0 The intersection point is P(t _{0} ) ∆=P _{1} P _{0} points inside if (P _{1} P _{0} )·N<0. Otherwise it points outside If L is vertical, intersection can be computed using the explicit equation
CyrusBeck Line Clipping
• Denote
p 0
Ni
p(t)=p _{0} +(p _{1} p _{0} )t
t∈[0 1] ..
•

Let Q _{i} be a point on the edge L _{i} with outside pointing normal N _{i}

•

V(t) = p(t)Q _{i} is a parameterized vector from Q _{i} to the segment P(t)

•

N _{i} · V(t) = 0

iff

V(t) ⊥ N _{i}

CyrusBeck Line Clipping
0 = N _{i} · V(t) = N _{i} · (p(t)Q _{i} ) = N _{i} · (p _{0} +(p _{1} p _{0} )tQ _{i} ) = N _{i} · (p _{0} Q _{i} ) + N _{i} · (p _{1} p _{0} )t
Solving for t we get:
t = N _{i} ·(p _{0} Q _{i} ) N _{i} ·(p _{1} p _{0} ) where ∆=(p _{1} p _{0} )
=
N _{i} ·(p _{0} Q _{i} )
N _{i} ·∆
Comment: If N _{i} ·∆=0,
t
has no solution (V(t) ⊥ N _{i} )
CyrusBeck Line Clipping
• The intersection of p(t) with all four edges L _{i} is computed, resulting in up to four t _{i} values
•
If
t _{i} <0
or
t _{i} >1 ,
t _{i} can be discarded
• Based on the sign of N _{i} ·∆, each intersection point is classified as PE (potentially entering) or PL (potentially leaving)
• PE with the largest t and PL with the smallest t provide the domain of p(t) inside W
• The domain, if inverted, signals that p(t) is totally outside
SutherlandHodgman PolygonClipping Algorithm
SutherlandHodgman PolygonClipping Algorithm
Idea: Clip a polygon by successively clipping against each (infinite) clip edge
After each clipping a new set of vertices is produced.
right clip boundary
left clip boundary
bottom clip boundary
top clip boundary
SutherlandHodgman PolygonClipping Algorithm
For each clip edge  scan the polygon and consider the relation between successive vertices of the polygon
Each iteration adds 0, 1 or 2 new vertices
Assume vertex s has been dealt with, vertex p follows:
inside
outside
s
p
clip
boundary
p added to output list
inside
outside
s
i
p
clip
boundary
i added to
output list
_{n}_{o} _{o}_{u}_{t}_{p}_{u}_{t}
i and p added to output list
SutherlandHodgman PolygonClipping Algorithm
V
2
V' 2
Window
V
3
V” 2
V’ 1
V’ 3
V
1
Left


Right


Bottom


Top

Clipping

Clipping

Clipping

Clipping










V



V’ _{2}

V’ _{2}

V’ _{2}

V’ _{3}

V’ _{3}

V” _{2}

Sampling Theorem
Question: How dense should be the pixel grid in order to draw properly a drawn object?
Given a sampling at intervals equal to d then one may recover frequencies of wavelength > 2d
Aliasing: If the sampling interval is more than 1/2 the wavelength, erroneous frequencies may be produced
Sampling Theorem
1D Example:
0
π
2π
3π
4π
5π
6π
Rule of Thumb: To observe details of size d one must sample at d/2 intervals
To observe details at frequency f (=1/d) one must sample at frequency 2f. The Frequency 2f is the NYQUIST frequency
Sampling Theorem
2D Example: Moire’ Effect