Title: CPSC 441 Computer Graphics: Clipping Lines Jinxiang Chai
1CPSC 441 Computer GraphicsClipping
LinesJinxiang Chai
2Why Clip?
- We do not want to waste time drawing objects that
are outside of viewing window (or clipping window)
3Clipping Points
- Given a point (x, y) and clipping window (xmin,
ymin), (xmax, ymax), determine if the point
should be drawn
(xmax,ymax)
(x,y)
(xmin,ymin)
4Clipping Points
- Given a point (x, y) and clipping window (xmin,
ymin), (xmax, ymax), determine if the point
should be drawn
(xmax,ymax)
(x,y)
xminltxltxmax?
(xmin,ymin)
yminltyltymax?
5Clipping Points
- Given a point (x, y) and clipping window (xmin,
ymin), (xmax, ymax), determine if the point
should be drawn
(xmax,ymax)
(x2,y2)
(x1,y1)
xminltxltxmax?
(xmin,ymin)
yminltyltymax?
6Clipping Points
- Given a point (x, y) and clipping window (xmin,
ymin), (xmax, ymax), determine if the point
should be drawn
(xmax,ymax)
(x2,y2)
(x1,y1)
xminltx1ltxmax Yes
(xmin,ymin)
yminlty1ltymax Yes
7Clipping Points
- Given a point (x, y) and clipping window (xmin,
ymin), (xmax, ymax), determine if the point
should be drawn
(xmax,ymax)
(x2,y2)
(x1,y1)
xminltx2ltxmax No
(xmin,ymin)
yminlty2ltymax No
8Clipping Lines
9Clipping Lines
10Clipping Lines
- Given a line with end-points (x0, y0), (x1, y1)
- and clipping window (xmin, ymin), (xmax, ymax),
- determine if line should be drawn and clipped
end-points of line to draw.
(xmax,ymax)
(x1, y1)
(x0, y0)
(xmin,ymin)
11Clipping Lines
12Outline
- Simple line clipping algorithm
- Cohen-Sutherland
- Liang-Barsky
13Clipping Lines Simple Algorithm
- If both end-points inside rectangle, draw line
- If one end-point outside,
- intersect line with all edges of rectangle
- clip that point and repeat test
14Clipping Lines Simple Algorithm
15Clipping Lines Simple Algorithm
16Intersecting Two Lines
17Intersecting Two Lines
18Intersecting Two Lines
19Intersecting Two Lines
20Intersecting Two Lines
21Intersecting Two Lines
Substitute t or s back into equation to find
intersection
22Clipping Lines Simple Algorithm
23Clipping Lines Simple Algorithm
24Clipping Lines Simple Algorithm
25Clipping Lines Simple Algorithm
26Clipping Lines Simple Algorithm
27Clipping Lines Simple Algorithm
28Clipping Lines Simple Algorithm
29Clipping Lines Simple Algorithm
30Clipping Lines Simple Algorithm
31Clipping Lines Simple Algorithm
32Clipping Lines Simple Algorithm
33Clipping Lines Simple Algorithm
34Clipping Lines Simple Algorithm
35Clipping Lines Simple Algorithm
36Clipping Lines Simple Algorithm
37Clipping Lines Simple Algorithm
- Lots of intersection tests makes algorithm
expensive - Complicated tests to determine if intersecting
rectangle - Is there a better way?
38Trivial Accepts
- Big Optimization trivial accepts/rejects
- How can we quickly decide whether line segment is
entirely inside window - Answer test both endpoints
39Trivial Accepts
- Big Optimization trivial accepts/rejects
- How can we quickly decide whether line segment is
entirely inside window - Answer test both endpoints
40Trivial Rejects
- How can we know a line is outside of the window
- Answer both endpoints on wrong side of same
edge, can trivially reject the line
41Trivial Rejects
- How can we know a line is outside of the window
- Answer both endpoints on wrong side of same
edge, can trivially reject the line
42Cohen-Sutherland Algorithm
- Classify p0, p1 using region codes c0, c1
- If , trivially reject
- If , trivially accept
- Otherwise reduce to trivial cases by splitting
into two segments
43Cohen-Sutherland Algorithm
- Every end point is assigned to a four-digit
binary value, i.e. Region code - Each bit position indicates whether the point
is inside or outside of a specific window edges
bit 4
bit 3
bit 2
bit 1
left
right
bottom
top
44Cohen-Sutherland Algorithm
bit 4
bit 3
bit 2
bit 1
left
right
bottom
top
top
right
left
Region code?
bottom
45Cohen-Sutherland Algorithm
bit 4
bit 3
bit 2
bit 1
left
right
bottom
top
top
right
left
0010
bottom
?
46Cohen-Sutherland Algorithm
bit 4
bit 3
bit 2
bit 1
left
right
bottom
top
top
right
left
0010
bottom
0100
47Cohen-Sutherland Algorithm
48Cohen-Sutherland Algorithm
- Classify p0, p1 using region codes c0, c1
- If , trivially reject
- If , trivially accept
- Otherwise reduce to trivial cases by splitting
into two segments
49Cohen-Sutherland Algorithm
- Classify p0, p1 using region codes c0, c1
- If , trivially reject
- If , trivially accept
- Otherwise reduce to trivial cases by splitting
into two segments
50Cohen-Sutherland Algorithm
- Classify p0, p1 using region codes c0, c1
- If , trivially reject
- If , trivially accept
- Otherwise reduce to trivial cases by splitting
into two segments
Line is outside the window! reject
51Cohen-Sutherland Algorithm
- Classify p0, p1 using region codes c0, c1
- If , trivially reject
- If , trivially accept
- Otherwise reduce to trivial cases by splitting
into two segments
Line is inside the window! draw
52Cohen-Sutherland Algorithm
- Classify p0, p1 using region codes c0, c1
- If , trivially reject
- If , trivially accept
- Otherwise reduce to trivial cases by splitting
into two segments
53Window Intersection
- (x1, y1), (x2, y2) intersect with vertical edge
at xright - yintersect y1 m(xright x1)
- where m(y2-y1)/(x2-x1)
- (x1, y1), (x2, y2) intersect with horizontal edge
at ybottom - xintersect x1 (ybottom y1)/m
- where m(y2-y1)/(x2-x1)
54Example 1
55Example 1
56Example 1
57Example 1
58Example 2
59Example 2
60Example 2
61Example 2
62Example 2
63Example 2
64Example 2
65Example 3
66Example 3
67Example 3
68Example 3
69Cohen-Sutherland Algorithm
- Extends easily to 3D line clipping
- 27 regions
- 6 bits
70Cohen-Sutherland Algorithm
- Use region codes to quickly eliminate/include
lines - Best algorithm when trivial accepts/rejects are
common - Must compute viewing window clipping of remaining
lines - Non-trivial clipping cost
- Redundant clipping of some lines
- More efficient algorithms exist
71Liang-Barsky Algorithm
- Parametric definition of a line
- x x1 u?x
- y y1 u?y
- ?x (x2-x1), ?y (y2-y1), 0ltult1
- Lines are oriented classify lines as moving
inside to out or outside to in - Goal find range of u for which x and y both
inside the viewing window
72Liang-Barsky Algorithm
- For lines starting outside of boundary, update
its starting point (u1) - For lines starting inside of boundary, update end
point (u2) - For lines paralleling the boundaries and outside
window, reject it.
73Liang-Barsky Algorithm
- For lines starting outside of boundary, update
its starting point (u1) - For lines starting inside of boundary, update end
point (u2) - For lines paralleling the boundaries and outside
window, reject it.
74Liang-Barsky Algorithm
- Mathematically
- xmin lt x1 u?x lt xmax
- ymin lt y1 u?y lt ymax
- Rearranged
- 1 u(-?x) lt (x1 xmin)
- 2 u(?x) lt (xmax x1)
- 3 u(-?y) lt (y1 ymin)
- 4 u(?y) lt (ymax y1)
- gen u(pk) lt (qk), k1,2,3,4
75Liang-Barsky Algorithm
- Rules
- pk 0 the line is parallel to boundaries
- If for that same k, qk lt 0, its outside
- Otherwise its inside
- pk lt 0 the line starts outside this boundary
- rk qk/pk
- u1 max(0, rk, u1) /update starting point/
- pk gt 0 the line starts inside the boundary
- rk qk/pk
- u2 min(1, rk, u2) / update end point/
- If u1 gt u2, the line is completely outside
76Liang-Barsky Algorithm
- Current clipped line (u10,u21)
(2,1)
(-.5,-.5)
77Liang-Barsky Algorithm
- Current clipped line (u10,u21)
(2,1)
?x (x2-x1)2.5
?y (y2-y1)1.5
p1 -?x -2.5lt0
(-.5,-.5)
q1 (x1-xmin)-.5
r1 q1/p10.2
78Liang-Barsky Algorithm
- Current clipped line (u10,u21)
(2,1)
?x (x2-x1)2.5
?y (y2-y1)1.5
p1 -?x -2.5lt0
(-.5,-.5)
q1 (x1-xmin)-.5
line starts outside this boundary
r1 q1/p10.2
79Liang-Barsky Algorithm
- Current clipped line (u10,u21)
(2,1)
?x (x2-x1)2.5
?y (y2-y1)1.5
p1 -?x -2.5lt0
(-.5,-.5)
q1 (x1-xmin)-.5
r1 q1/p10.2
80Liang-Barsky Algorithm
- Current clipped line (u10,u21)
(2,1)
?x (x2-x1)2.5
?y (y2-y1)1.5
p1 -?x -2.5lt0
(-.5,-.5)
q1 (x1-xmin)-.5
r1 q1/p10.2
81Liang-Barsky Algorithm
- Current clipped line (u10,u21)
(2,1)
?x (x2-x1)2.5
?y (y2-y1)1.5
p1 -?x -2.5lt0
(-.5,-.5)
q1 (x1-xmin)-.5
r1 q1/p10.2
82Liang-Barsky Algorithm
- Current clipped line (u10,u21)
(2,1)
?x (x2-x1)2.5
?y (y2-y1)1.5
p1 -?x -2.5lt0
(-.5,-.5)
q1 (x1-xmin)-.5
r1 q1/p10.2
u1 max(0, r1, u1)
83Liang-Barsky Algorithm
- Current clipped line (u10.2,u21)
(2,1)
- u1ltu2?
- yes continue
- no the line is completely outside window
(-.5,-.5)
84Liang-Barsky Algorithm
- Current clipped line (u10.2,u21)
(2,1)
(-.5,-.5)
85Liang-Barsky Algorithm
- Current clipped line (u10.2,u21)
(2,1)
?x (x2-x1)2.5
?y (y2-y1)1.5
p2 ?x 2.5gt0
(-.5,-.5)
q2 (xmax-x1)1.5
r2 q2/p20.6
86Liang-Barsky Algorithm
- Current clipped line (u10.2,u21)
(2,1)
?x (x2-x1)2.5
?y (y2-y1)1.5
p2 ?x 2.5gt0
(-.5,-.5)
q2 (xmax-x1)1.5
r2 q2/p20.6
u2 min(1, r2, u2)
87Liang-Barsky Algorithm
- Current clipped line (u10.2,u20.6)
(2,1)
?x (x2-x1)2.5
?y (y2-y1)1.5
p2 ?x 2.5gt0
(-.5,-.5)
q2 (xmax-x1)1.5
r2 q2/p20.6
88Liang-Barsky Algorithm
- Current clipped line (u10.2,u20.6)
(2,1)
- u1ltu2?
- yes continue
- no the line is completely outside window
(-.5,-.5)
89Liang-Barsky Algorithm
- Current clipped line (u10.2,u20.6)
(2,1)
?x (x2-x1)2.5
?y (y2-y1)1.5
p3 -?y -1.5lt0
(-.5,-.5)
q3 (y1-ymin)0.5
r3 q3/p30.333
90Liang-Barsky Algorithm
- Current clipped line (u10.2,u20.6)
(2,1)
?x (x2-x1)2.5
?y (y2-y1)1.5
p3 -?y -1.5lt0
(-.5,-.5)
q3 (y1-ymin)0.5
r3 q3/p30.333
u1 max(0, r3, u1)
91Liang-Barsky Algorithm
- Current clipped line (u10.333,u20.6)
(2,1)
?x (x2-x1)2.5
?y (y2-y1)1.5
p3 -?y -1.5lt0
(-.5,-.5)
q3 (y1-ymin)0.5
r3 q3/p30.333
92Liang-Barsky Algorithm
- Current clipped line (u10.333,u20.6)
(2,1)
(-.5,-.5)
93Liang-Barsky Algorithm
- Current clipped line (u10.333,u20.6)
(2,1)
?x (x2-x1)2.5
?y (y2-y1)1.5
p3 -?y -1.5lt0
(-.5,-.5)
q3 (y1-ymin)0.5
r3 q3/p30.333
94Liang-Barsky Algorithm
- Current clipped line (u10.333,u20.6)
(2,1)
?x (x2-x1)2.5
?y (y2-y1)1.5
p4 ?y 1.5gt0
(-.5,-.5)
q4 (ymax-y1)1.5
r4 q4/p41
95Liang-Barsky Algorithm
- Current clipped line (u10.333,u20.6)
(2,1)
?x (x2-x1)2.5
?y (y2-y1)1.5
p4 ?y 1.5gt0
(-.5,-.5)
q4 (ymax-y1)1.5
r4 q4/p41
u2 min(1, r4, u2)
96Liang-Barsky Algorithm
- Check the left edge (u10.333,u20.6)
(2,1)
u1ltu2
(-.5,-.5)
xnew1x1?xu1
xnew2x2?xu2
ynew1y1?yu1
ynew2y2?yu2
97Liang-Barsky Algorithm
98Liang-Barsky Algorithm
99Liang-Barsky Algorithm
100Liang-Barsky Algorithm
101Liang-Barsky Algorithm
102Liang-Barsky Algorithm
103Liang-Barsky Algorithm
104Liang-Barsky Algorithm
105Liang-Barsky Algorithm
106Liang-Barsky Algorithm
107Liang-Barsky Algorithm
108Liang-Barsky Algorithm
109Liang-Barsky Algorithm
110Liang-Barsky Algorithm
111Liang-Barsky Algorithm
112Liang-Barsky Algorithm
113Liang-Barsky Algorithm
u1gtu2!!
114Liang-Barsky Algorithm
115Liang-Barsky Algorithm
116Liang-Barsky Algorithm
117Liang-Barsky Algorithm
118Liang-Barsky Algorithm
119Liang-Barsky Algorithm
120Liang-Barsky Algorithm
121Liang-Barsky Algorithm
- Faster than Cohen-Sutherland
- Extension to 3D is easy
- - Parametric representation for 3D lines
- - Compute u1,u2 based on the intersection
between line and plane
122Comparison
- Cohen-Sutherland
- Repeated clipping is expensive
- Best used when trivial acceptance and rejection
is possible for most lines - Liang-Barsky
- Computation of t-intersections is cheap (only one
division) - Computation of (x,y) clip points is only done
once - Algorithm doesnt consider trivial
accepts/rejects - Best when many lines must be clipped
- Nicholl et al. Fastest, but doesnt do 3D
123Curve Clipping
124Curve Clipping
125Curve Clipping
- Approximate a curve using a set of straight-line
segments - Apply line clipping for curve clipping
126Next Lecture
- Polygon fill-area clipping
127Next Lecture
- Polygon fill-area clipping