Professional Documents
Culture Documents
Line and Polygon Clipping: Foley & Van Dam, Chapter 3
Line and Polygon Clipping: Foley & Van Dam, Chapter 3
x world x view
World and Viewing Clipping
Coordinates
1
Device Coordinates Normalized Device
Coordinates
Viewing Transformation in 2D
• Objects are given in world coordinates
• The world is viewed through a window
• The window is mapped onto a device viewport
A scene in World-Coordinates
WC
World-Coordinates to Viewing
Coordinates Clipping
VC
Viewing Coordinates to
Normalized Device Coordinates
NDC
DC
Normalized Device Coordinates
to Device Coordinates
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:
(xmin,ymin) and (xmax,ymax)
• A point (x,y) is in the window iff:
xmin ≤ x ≤ xmax and ymin ≤ y ≤ ymax
Brute Force Analytic Solution
S S S
W W W
0, 1, or 2 intersections between a line and a window
x = xmin
M NE
(xmin,Round(mxmin+B))
Q E
(xmin, mxmin+B)
y = ymin
Line Clipping
Midpoint Algorithm: Intersection with a horizontal edge
x = xmin
B A y = ymin
y = ymin-1/2
Cohen-Sutherland for Line Clipping
• Clipping is performed by computing intersections with four
boundary segments of the window:
Li, 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 half-plane that
does not contain W
Cohen-Sutherland for Line Clipping
bit 1 0
1 y < ymin y ≥ ymin
2 y > ymax y ≤ ymax
3 x > xmax x ≤ xmax
4 x < xmin x ≥ xmin
4 3
0101 0100 0110 2
ymax
0001 0000 0010
ymin 1
1001 1000 1010
xmin xmax
Cohen-Sutherland for Line Clipping
0101 0100 0110
D 4 3
I
C B
2
H
A G
1
F
E
Cyrus-Beck Line Clipping
N
V1
U
inside
V0
Inside/Outside Test:
• Assume WLOG that V=(V1-V0) is the border vector where
"inside" is to its right
• If V=(Vx,Vy), N is the normal to V, pointing outside,
defined by N=(-Vy,Vx)
• Vector U points "outside" if N·U > 0
• Otherwise U points "inside"
Cyrus-Beck Line Clipping
Q
N
P1
V(t)
inside
P0 L
PL PL
PL
p0 p1
PL
PE
PE
PE
p0
p0
Cyrus-Beck Line Clipping
• The intersection of p(t) with all four edges Li is
computed, resulting in up to four ti values
• If ti<0 or ti>1 , ti can be discarded
• Based on the sign of Ni·∆, 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
Sutherland-Hodgman
Polygon-Clipping Algorithm
Sutherland-Hodgman
Polygon-Clipping Algorithm
Idea: Clip a polygon by successively clipping against each
(infinite) clip edge
s i p s
p clip clip
clip clip
boundary boundary boundary boundary
p added to i added to no output i and p added to
output list output list output list
Sutherland-Hodgman
Polygon-Clipping Algorithm
V2
V'2
Window
V3
V”2 V’1
V’3
V1
Left Right Bottom Top
Clipping Clipping Clipping Clipping
V1 V1 V1 V’1
V2 V2 V2 V2
V3 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?
1D Example:
0 π 2π 3π 4π 5π 6π