Clipping Lines - TAMU Computer Science Faculty Pages

advertisement
Clipping Lines
Dr. Scott Schaefer
Why Clip?

We do not want to waste time drawing
objects that are outside of viewing window
(or clipping window)
2/94
Clipping 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)
3/94
Clipping 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)
xmin<=x<=xmax?
ymin<=y<=ymax?
4/94
Clipping Points

Given a point (x, y) and clipping window
(xmin, ymin), (xmax, ymax), determine if the point
should be drawn
(xmax,ymax)
(x1,y1)
(xmin,ymin)
(x2,y2)
xmin<=x<=xmax?
ymin<=y<=ymax?
5/94
Clipping Points

Given a point (x, y) and clipping window
(xmin, ymin), (xmax, ymax), determine if the point
should be drawn
(xmax,ymax)
(x1,y1)
(xmin,ymin)
(x2,y2)
xmin<=x1<=xmax Yes
ymin<=y1<=ymax Yes
6/94
Clipping Points

Given a point (x, y) and clipping window
(xmin, ymin), (xmax, ymax), determine if the point
should be drawn
(xmax,ymax)
(x1,y1)
(xmin,ymin)
(x2,y2)
xmin<=x2<=xmax No
ymin<=y2<=ymax Yes
7/94
Clipping Lines
P6
P3
P4
P10
P8
P9
P5
P7
P2
P1
8/94
Clipping Lines
Pˆ6
Pˆ10
Pˆ9
P8
P5
P7
9/94
Clipping 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)
10/94
Clipping Lines
P6
P3
P4
P10
P8
P9
P5
P7
P2
P1
11/94
Clipping Lines – Simple Algorithm
If both end-points inside rectangle, draw line
 Otherwise,
intersect line with all edges of rectangle
clip that point and repeat test

12/94
Clipping Lines – Simple Algorithm
( xmax , ymax )
( x1 , y1 )
( x0 , y0 )
( xmin , ymin )
13/94
Clipping Lines – Simple Algorithm
( xmax , ymax )
( x1 , y1 )
( x0 , y0 )
( xmin , ymin )
14/94
Clipping Lines – Simple Algorithm
( xmax , ymax )
( x1 , y1 )
( xˆ0 , yˆ0 )
( xmin , ymin )
15/94
Clipping Lines – Simple Algorithm
( xmax , ymax )
( x1 , y1 )
( xˆ0 , yˆ0 )
( xmin , ymin )
16/94
Clipping Lines – Simple Algorithm
( xmax , ymax )
( x1 , y1 )
( xˆ0 , yˆ0 )
( xmin , ymin )
17/94
Clipping Lines – Simple Algorithm
( xmax , ymax )
( x1 , y1 )
( xˆ0 , yˆ0 )
( xmin , ymin )
18/94
Clipping Lines – Simple Algorithm
( xmax , ymax )
( x0 , y0 )
( xmin , ymin )
( x1 , y1 )
19/94
Clipping Lines – Simple Algorithm
( xmax , ymax )
( xˆ0 , yˆ0 )
( xmin , ymin )
( x1 , y1 )
20/94
Clipping Lines – Simple Algorithm
( xmax , ymax )
( xˆ0 , yˆ0 )
( xmin , ymin )
( x1 , y1 )
21/94
Clipping Lines – Simple Algorithm
( xmax , ymax )
( xˆ0 , yˆ0 )
( xmin , ymin )
( xˆ1 , yˆ1 )
22/94
Clipping Lines – Simple Algorithm
( xmax , ymax )
( xˆ0 , yˆ0 )
( xmin , ymin )
( xˆ1 , yˆ1 )
23/94
Window 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)
24/94
Clipping Lines – Simple Algorithm
Lots of intersection tests makes algorithm
expensive
 Complicated tests to determine if intersecting
rectangle


Is there a better way?
25/94
Trivial Accepts
Big Optimization: trivial accepts/rejects
 How can we quickly decide whether line
segment is entirely inside window
 Answer: test both endpoints

26/94
Trivial Accepts
Big Optimization: trivial accepts/rejects
 How can we quickly decide whether line
segment is entirely inside window
 Answer: test both endpoints

27/94
Trivial 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

28/94
Trivial 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

29/94
Cohen-Sutherland Algorithm
Classify p0, p1 using region codes c0, c1
 If c0  c1  0 , trivially reject
 If c0  c1  0 , trivially accept
 Otherwise reduce to trivial cases by splitting
into two segments

30/94
Cohen-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 edge

bit 4 bit 3 bit 2 bit 1
top
bottom right left
31/94
Cohen-Sutherland Algorithm
1001
1000
1010
0001
0000
0010
0101
0100
0110
32/94
Cohen-Sutherland Algorithm
Classify p0, p1 using region codes c0, c1
 If c0  c1  0 , trivially reject
 If c0  c1  0 , trivially accept
 Otherwise reduce to trivial cases by splitting
into two segments

33/94
Cohen-Sutherland Algorithm
Classify p0, p1 using region codes c0, c1
 If c0  c1  0 , trivially reject
 If c0  c1  0 , trivially accept
 Otherwise reduce to trivial cases by splitting
into two segments

34/94
Cohen-Sutherland Algorithm
Classify p0, p1 using region codes c0, c1
 If c0  c1  0 , trivially reject
 If c0  c1  0 , trivially accept
 Otherwise reduce to trivial cases by splitting
into two segments

1001
1000
1010
0001
0000
0010
0101
0100
0110
Line is outside the
window! reject
35/94
Cohen-Sutherland Algorithm
Classify p0, p1 using region codes c0, c1
 If c0  c1  0 , trivially reject
 If c0  c1  0 , trivially accept
 Otherwise reduce to trivial cases by splitting
into two segments

1001
1000
1010
0001
0000
0010
0101
0100
0110
Line is inside the
window! draw
36/94
Cohen-Sutherland Algorithm
Classify p0, p1 using region codes c0, c1
 If c0  c1  0 , trivially reject
 If c0  c1  0 , trivially accept
 Otherwise reduce to trivial cases by splitting
into two segments

1001
1000
1010
0001
0000
0010
0101
0100
0110
37/94
Cohen-Sutherland Algorithm
1001
1000
1010
0001
0000
0010
0101
0100
0110
38/94
Cohen-Sutherland Algorithm
1001
1000
1010
0001
0000
0010
0101
0100
0110
39/94
Cohen-Sutherland Algorithm
1001
1000
1010
0001
0000
0010
0101
0100
0110
40/94
Cohen-Sutherland Algorithm
1001
1000
1010
0001
0000
0010
0101
0100
0110
41/94
Cohen-Sutherland Algorithm
1001
1000
1010
0001
0000
0010
0101
0100
0110
42/94
Cohen-Sutherland Algorithm
1001
1000
1010
0001
0000
0010
0101
0100
0110
43/94
Cohen-Sutherland Algorithm
1001
1000
1010
0001
0000
0010
0101
0100
0110
44/94
Cohen-Sutherland Algorithm
1001
1000
1010
0001
0000
0010
0101
0100
0110
45/94
Cohen-Sutherland Algorithm
1001
1000
1010
0001
0000
0010
0101
0100
0110
46/94
Cohen-Sutherland Algorithm
1001
1000
1010
0001
0000
0010
0101
0100
0110
47/94
Cohen-Sutherland Algorithm
1001
1000
1010
0001
0000
0010
0101
0100
0110
48/94
Cohen-Sutherland Algorithm
1001
1000
1010
0001
0000
0010
0101
0100
0110
49/94
Cohen-Sutherland Algorithm
1001
1000
1010
0001
0000
0010
0101
0100
0110
50/94
Cohen-Sutherland Algorithm
1001
1000
1010
0001
0000
0010
0101
0100
0110
51/94
Cohen-Sutherland Algorithm
1001
1000
1010
0001
0000
0010
0101
0100
0110
52/94
Liang-Barsky Algorithm
Uses parametric form of line for clipping
 Lines are oriented
Classify lines as moving inside to out or
outside to in
 Don’t find actual intersection points
Find parameter values on line to draw

53/94
Liang-Barsky Algorithm
Initialize interval to [tmin, tmax]=[0,1]
 For each boundary
 Find parametric intersection t with
boundary
 If moving in to out, tmax = min(tmax, t)
 else tmin = max(tmin, t)
 If tmin>tmax, reject line

54/94
Intersecting Two Parametric Lines
( x3 , y3 )
( x1 , y1 )
( x0 , y0 )
( x2 , y2 )
55/94
Intersecting Two Parametric Lines
x(t )  x0  ( x1  x0 )t
y(t )  y0  ( y1  y0 )t
0  t 1
( x3 , y3 )
( x1 , y1 )
( x0 , y0 )
( x2 , y2 )
56/94
Intersecting Two Parametric Lines
x(t )  x0  ( x1  x0 )t
y(t )  y0  ( y1  y0 )t
x(0)  x0
y(0)  y0
x(1)  x1
y(1)  y1
( x3 , y3 )
( x1 , y1 )
( x0 , y0 )
( x2 , y2 )
57/94
Intersecting Two Parametric Lines
( x3 , y3 )
( x1 , y1 )
( x0 , y0 )
x0  ( x1  x0 )t  x2  ( x3  x2 )s
y0  ( y1  y0 )t  y2  ( y3  y2 )s
( x2 , y2 )
58/94
Intersecting Two Parametric Lines
( x3 , y3 )
( x1 , y1 )
( x0 , y0 )
 x1  x0

 y1  y0
x2  x3  t   x2  x0 
   

y2  y3  s   y2  y0 
( x2 , y2 )
59/94
Intersecting Two Parametric Lines
( x3 , y3 )
( x1 , y1 )
( x0 , y0 )
 x1  x0

 y1  y0
x2  x3  t   x2  x0 
   

y2  y3  s   y2  y0 
( x2 , y2 )
Substitute t or s back into equation to find intersection
60/94
Liang-Barsky: Classifying Lines
p(0)
p(t )
p(1)
x1  x0
movingout
61/94
Liang-Barsky: Classifying Lines
p(1)
p(t )
p(0)
x0  x1
movingin
62/94
Liang-Barsky: Classifying Lines
p(0)
p(t )
p(1)
x1  x0
movingin
63/94
Liang-Barsky: Classifying Lines
p(1)
p(t )
p(0)
x0  x1
movingout
64/94
Liang-Barsky: Classifying Lines
p(0)
p(t )
p(1)
y1  y0
movingin
65/94
Liang-Barsky: Classifying Lines
p(1)
p(t )
p(0)
y0  y1
movingout
66/94
Liang-Barsky: Classifying Lines
p(0)
p(t )
p(1)
y1  y0
movingout
67/94
Liang-Barsky: Classifying Lines
p(1)
p(t )
p(0)
y0  y1
movingin
68/94
Liang-Barsky Algorithm
p(0)
p(t )
p(1)
69/94
Liang-Barsky Algorithm
p(0)
p(t )
p(1)
70/94
Liang-Barsky Algorithm
p(0)
p(t )
p(1)
71/94
Liang-Barsky Algorithm
p(0)
p(t )
p (.8)
72/94
Liang-Barsky Algorithm
p(0)
p(1)
73/94
Liang-Barsky Algorithm
p(1)
p(0)
74/94
Liang-Barsky Algorithm
p(0)
p(1)
75/94
Liang-Barsky Algorithm
p(.7)
p(0)
p(1)
76/94
Liang-Barsky Algorithm
p(1)
p(0)
77/94
Liang-Barsky Algorithm
p(1)
p (.2)
78/94
Liang-Barsky Algorithm
p(1)
p(0)
79/94
Liang-Barsky Algorithm
p(1)
p(0)
80/94
Liang-Barsky Algorithm
p(1)
p (.1)
81/94
Liang-Barsky Algorithm
p(1)
p (.1)
82/94
Liang-Barsky Algorithm
p (.8)
p (.1)
83/94
Liang-Barsky Algorithm
p (.8)
p (.1)
84/94
Liang-Barsky Algorithm
p (.8)
p (.1)
85/94
Liang-Barsky Algorithm
p(0)
p(1)
86/94
Liang-Barsky Algorithm
p(0)
p(1)
87/94
Liang-Barsky Algorithm
p(0)
p(1)
88/94
Liang-Barsky Algorithm
p(0)
p (.6)
89/94
Liang-Barsky Algorithm
p(0)
p (.6)
90/94
Liang-Barsky Algorithm
p(0)
p (.6)
91/94
Liang-Barsky Algorithm
p (.2)
p (.6)
92/94
Comparison

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 doesn’t consider trivial accepts/rejects
 Best when many lines must be clipped

93/94
Line Clipping – Considerations
Just clipping end-points does not produce the
correct results inside the window
 Must also update sum in midpoint algorithm


Clipping against non-rectangular polygons is
also possible but seldom used
94/94
Download