Upload
randall-weaver
View
216
Download
2
Tags:
Embed Size (px)
Citation preview
Dr. Scott Schaefer
Clipping Lines
2/94
Why Clip?
We do not want to waste time drawing objects that are outside of viewing window (or clipping window)
3/94
Clipping Points
Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn
(xmin,ymin)
(xmax,ymax)
(x,y)
4/94
Clipping Points
Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn
(xmin,ymin)
(xmax,ymax)
(x,y)
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
(x1,y1)(x2,y2)
(xmin,ymin)
(xmax,ymax)
xmin<=x<=xmax?ymin<=y<=ymax?
6/94
Clipping Points
Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn
(xmin,ymin)
(xmax,ymax)
xmin<=x1<=xmax Yes
ymin<=y1<=ymax Yes
(x1,y1)(x2,y2)
7/94
Clipping Points
Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn
(xmin,ymin)
(xmax,ymax)
xmin<=x2<=xmax Noymin<=y2<=ymax Yes
(x1,y1)(x2,y2)
8/94
Clipping Lines
1P
2P
3P4P
5P
6P
7P
8P9P
10P
9/94
Clipping Lines
5P
6̂P
7P
8P
9̂P
10P̂
10/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.
(x0, y0)
(x1, y1)
(xmin,ymin)
(xmax,ymax)
11/94
Clipping Lines
1P
2P
3P4P
5P
6P
7P
8P9P
10P
12/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
13/94
Clipping Lines – Simple Algorithm
),( 00 yx
),( minmin yx
),( maxmax yx
),( 11 yx
14/94
Clipping Lines – Simple Algorithm
),( 00 yx
),( minmin yx
),( maxmax yx
),( 11 yx
15/94
Clipping Lines – Simple Algorithm
)ˆ,ˆ( 00 yx
),( minmin yx
),( maxmax yx
),( 11 yx
16/94
Clipping Lines – Simple Algorithm
)ˆ,ˆ( 00 yx
),( minmin yx
),( maxmax yx
),( 11 yx
17/94
Clipping Lines – Simple Algorithm
)ˆ,ˆ( 00 yx
),( minmin yx
),( maxmax yx
),( 11 yx
18/94
Clipping Lines – Simple Algorithm
)ˆ,ˆ( 00 yx
),( minmin yx
),( maxmax yx
),( 11 yx
19/94
Clipping Lines – Simple Algorithm
),( 00 yx
),( minmin yx
),( maxmax yx
),( 11 yx
20/94
Clipping Lines – Simple Algorithm
)ˆ,ˆ( 00 yx
),( minmin yx
),( maxmax yx
),( 11 yx
21/94
Clipping Lines – Simple Algorithm
)ˆ,ˆ( 00 yx
),( minmin yx
),( maxmax yx
),( 11 yx
22/94
Clipping Lines – Simple Algorithm
)ˆ,ˆ( 00 yx
),( minmin yx
),( maxmax yx
)ˆ,ˆ( 11 yx
23/94
Clipping Lines – Simple Algorithm
)ˆ,ˆ( 00 yx
),( minmin yx
),( maxmax yx
)ˆ,ˆ( 11 yx
24/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)
25/94
Clipping Lines – Simple Algorithm
Lots of intersection tests makes algorithm expensive
Complicated tests to determine if intersecting rectangle
Is there a better way?
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 Accepts
Big Optimization: trivial accepts/rejects How can we quickly decide whether line
segment is entirely inside window Answer: test both endpoints
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
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
30/94
Cohen-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
010 cc
010 cc
31/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
leftrightbottomtop
32/94
Cohen-Sutherland Algorithm
00000001
0101
1001 1000 1010
0010
01100100
33/94
Cohen-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
010 cc
010 cc
34/94
Cohen-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
010 cc
010 cc
35/94
Cohen-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
010 cc
010 cc
00000001
0101
1001 1000 1010
0010
01100100
Line is outside the window! reject
36/94
Cohen-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
010 cc
010 cc
00000001
0101
1001 1000 1010
0010
01100100
Line is inside the window! draw
37/94
Cohen-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
010 cc
010 cc
00000001
0101
1001 1000 1010
0010
01100100
38/94
Cohen-Sutherland Algorithm
00000001
0101
1001 1000 1010
0010
01100100
39/94
Cohen-Sutherland Algorithm
00000001
0101
1001 1000 1010
0010
01100100
40/94
Cohen-Sutherland Algorithm
00000001
0101
1001 1000 1010
0010
01100100
41/94
Cohen-Sutherland Algorithm
00000001
0101
1001 1000 1010
0010
01100100
42/94
Cohen-Sutherland Algorithm
00000001
0101
1001 1000 1010
0010
01100100
43/94
Cohen-Sutherland Algorithm
00000001
0101
1001 1000 1010
0010
01100100
44/94
Cohen-Sutherland Algorithm
00000001
0101
1001 1000 1010
0010
01100100
45/94
Cohen-Sutherland Algorithm
00000001
0101
1001 1000 1010
0010
01100100
46/94
Cohen-Sutherland Algorithm
00000001
0101
1001 1000 1010
0010
01100100
47/94
Cohen-Sutherland Algorithm
00000001
0101
1001 1000 1010
0010
01100100
48/94
Cohen-Sutherland Algorithm
00000001
0101
1001 1000 1010
0010
01100100
49/94
Cohen-Sutherland Algorithm
00000001
0101
1001 1000 1010
0010
01100100
50/94
Cohen-Sutherland Algorithm
00000001
0101
1001 1000 1010
0010
01100100
51/94
Cohen-Sutherland Algorithm
00000001
0101
1001 1000 1010
0010
01100100
52/94
Cohen-Sutherland Algorithm
00000001
0101
1001 1000 1010
0010
01100100
53/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
54/94
Liang-Barsky Algorithm
Initialize interval to [tmin, tmax]=[0,1]
For each boundaryFind parametric intersection t with
boundary If moving in to out, tmax = min(tmax, t)
else tmin = max(tmin, t)
If tmin>tmax, reject line
55/94
Intersecting Two Parametric Lines
),( 00 yx
),( 22 yx
),( 11 yx
),( 33 yx
56/94
Intersecting Two Parametric Lines
),( 00 yx
),( 22 yx
),( 11 yxtxxxtx )()( 010 ),( 33 yx
tyyyty )()( 010
10 t
57/94
Intersecting Two Parametric Lines
),( 00 yx
),( 22 yx
),( 11 yxtxxxtx )()( 010 ),( 33 yx
tyyyty )()( 010
0)0( xx 0)0( yy
1)1( xx 1)1( yy
58/94
Intersecting Two Parametric Lines
),( 00 yx
),( 22 yx
),( 11 yx
sxxxtxxx )()( 232010
),( 33 yx
syyytyyy )()( 232010
59/94
Intersecting Two Parametric Lines
),( 00 yx
),( 22 yx
),( 11 yx
02
02
3201
3201
yy
xx
s
t
yyyy
xxxx
),( 33 yx
60/94
Intersecting Two Parametric Lines
),( 00 yx
),( 22 yx
),( 11 yx
02
02
3201
3201
yy
xx
s
t
yyyy
xxxx
),( 33 yx
Substitute t or s back into equation to find intersection
61/94
Liang-Barsky: Classifying Lines
)(tp
)0(p
)1(p
outmoving01 xx
62/94
Liang-Barsky: Classifying Lines
)(tp
)1(p
)0(p
inmoving10 xx
63/94
Liang-Barsky: Classifying Lines
)(tp
)0(p
)1(p
inmoving01 xx
64/94
Liang-Barsky: Classifying Lines
)(tp
)1(p
)0(p
outmoving10 xx
65/94
Liang-Barsky: Classifying Lines
)(tp
)0(p
)1(p
inmoving01 yy
66/94
Liang-Barsky: Classifying Lines
)(tp
)1(p
)0(p
outmoving10 yy
67/94
Liang-Barsky: Classifying Lines
)(tp
)0(p
)1(p
outmoving01 yy
68/94
Liang-Barsky: Classifying Lines
)(tp
)1(p
)0(p
inmoving10 yy
69/94
Liang-Barsky Algorithm
)(tp
)0(p
)1(p
70/94
Liang-Barsky Algorithm
)(tp
)0(p
)1(p
71/94
Liang-Barsky Algorithm
)(tp
)0(p
)1(p
72/94
Liang-Barsky Algorithm
)(tp
)0(p
)8(.p
73/94
Liang-Barsky Algorithm
)0(p
)1(p
74/94
Liang-Barsky Algorithm
)0(p
)1(p
75/94
Liang-Barsky Algorithm
)0(p
)1(p
76/94
Liang-Barsky Algorithm
)0(p
)1(p
)7.(p
77/94
Liang-Barsky Algorithm
)1(p
)0(p
78/94
Liang-Barsky Algorithm
)1(p
)2(.p
79/94
Liang-Barsky Algorithm
)1(p
)0(p
80/94
Liang-Barsky Algorithm
)1(p
)0(p
81/94
Liang-Barsky Algorithm
)1(p
)1(.p
82/94
Liang-Barsky Algorithm
)1(p
)1(.p
83/94
Liang-Barsky Algorithm
)8(.p
)1(.p
84/94
Liang-Barsky Algorithm
)8(.p
)1(.p
85/94
Liang-Barsky Algorithm
)8(.p
)1(.p
86/94
Liang-Barsky Algorithm
)1(p
)0(p
87/94
Liang-Barsky Algorithm
)1(p
)0(p
88/94
Liang-Barsky Algorithm
)1(p
)0(p
89/94
Liang-Barsky Algorithm
)6(.p
)0(p
90/94
Liang-Barsky Algorithm
)6(.p
)0(p
91/94
Liang-Barsky Algorithm
)6(.p
)0(p
92/94
Liang-Barsky Algorithm
)6(.p
)2(.p
93/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
94/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