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 (x min, y min ), (x max, y max ), determine if the point should be drawn (x min, y min ) (x max, y max ) (x,y)
4/94 Clipping Points Given a point (x, y)and clipping window (x min, y min ), (x max, y max ), determine if the point should be drawn (x min, y min ) (x max, y max ) (x,y) x min <=x<=x max ? y min <=y<=y max ?
5/94 Clipping Points Given a point (x, y)and clipping window (x min, y min ), (x max, y max ), determine if the point should be drawn (x 1, y 1 ) (x 2, y 2 ) (x min, y min ) (x max, y max ) x min <=x<=x max ? y min <=y<=y max ?
6/94 Clipping Points Given a point (x, y)and clipping window (x min, y min ), (x max, y max ), determine if the point should be drawn (x min, y min ) (x max, y max ) x min <=x 1 <=x max Yes y min <=y 1 <=y max Yes (x 1, y 1 ) (x 2, y 2 )
7/94 Clipping Points Given a point (x, y)and clipping window (x min, y min ), (x max, y max ), determine if the point should be drawn (x min, y min ) (x max, y max ) x min <=x 2 <=x max No y min <=y 2 <=y max Yes (x 1, y 1 ) (x 2, y 2 )
8/94 Clipping Lines
9/94 Clipping Lines
10/94 Clipping Lines Given a line with end-points (x 0, y 0 ), (x 1, y 1 ) and clipping window (x min, y min ), (x max, y max ), determine if line should be drawn and clipped end-points of line to draw. (x 0, y 0 ) (x 1, y 1 ) (x min, y min ) (x max, y max )
11/94 Clipping Lines
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
14/94 Clipping Lines – Simple Algorithm
15/94 Clipping Lines – Simple Algorithm
16/94 Clipping Lines – Simple Algorithm
17/94 Clipping Lines – Simple Algorithm
18/94 Clipping Lines – Simple Algorithm
19/94 Clipping Lines – Simple Algorithm
20/94 Clipping Lines – Simple Algorithm
21/94 Clipping Lines – Simple Algorithm
22/94 Clipping Lines – Simple Algorithm
23/94 Clipping Lines – Simple Algorithm
24/94 Window Intersection (x 1, y 1 ), (x 2, y 2 ) intersect with vertical edge at x right y intersect = y 1 + m(x right – x 1 ) where m=(y 2 -y 1 )/(x 2 -x 1 ) (x 1, y 1 ), (x 2, y 2 ) intersect with horizontal edge at y bottom x intersect = x 1 + (y bottom – y 1 )/m where m=(y 2 -y 1 )/(x 2 -x 1 )
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 p 0, p 1 using region codes c 0, c 1 If, trivially reject If, trivially accept Otherwise reduce to trivial cases by splitting into two segments
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 4bit 3bit 2bit 1 leftrightbottomtop
32/94 Cohen-Sutherland Algorithm
33/94 Cohen-Sutherland Algorithm Classify p 0, p 1 using region codes c 0, c 1 If, trivially reject If, trivially accept Otherwise reduce to trivial cases by splitting into two segments
34/94 Cohen-Sutherland Algorithm Classify p 0, p 1 using region codes c 0, c 1 If, trivially reject If, trivially accept Otherwise reduce to trivial cases by splitting into two segments
35/94 Cohen-Sutherland Algorithm Classify p 0, p 1 using region codes c 0, c 1 If, trivially reject If, trivially accept Otherwise reduce to trivial cases by splitting into two segments Line is outside the window! reject
36/94 Cohen-Sutherland Algorithm Classify p 0, p 1 using region codes c 0, c 1 If, trivially reject If, trivially accept Otherwise reduce to trivial cases by splitting into two segments Line is inside the window! draw
37/94 Cohen-Sutherland Algorithm Classify p 0, p 1 using region codes c 0, c 1 If, trivially reject If, trivially accept Otherwise reduce to trivial cases by splitting into two segments
38/94 Cohen-Sutherland Algorithm
39/94 Cohen-Sutherland Algorithm
40/94 Cohen-Sutherland Algorithm
41/94 Cohen-Sutherland Algorithm
42/94 Cohen-Sutherland Algorithm
43/94 Cohen-Sutherland Algorithm
44/94 Cohen-Sutherland Algorithm
45/94 Cohen-Sutherland Algorithm
46/94 Cohen-Sutherland Algorithm
47/94 Cohen-Sutherland Algorithm
48/94 Cohen-Sutherland Algorithm
49/94 Cohen-Sutherland Algorithm
50/94 Cohen-Sutherland Algorithm
51/94 Cohen-Sutherland Algorithm
52/94 Cohen-Sutherland Algorithm
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 [t min, t max ]=[0,1] For each boundary Find parametric intersection t with boundary If moving in to out, t max = min(t max, t) else t min = max(t min, t) If t min >t max, reject line
55/94 Intersecting Two Parametric Lines
56/94 Intersecting Two Parametric Lines
57/94 Intersecting Two Parametric Lines
58/94 Intersecting Two Parametric Lines
59/94 Intersecting Two Parametric Lines
60/94 Intersecting Two Parametric Lines Substitute t or s back into equation to find intersection
61/94 Liang-Barsky: Classifying Lines
62/94 Liang-Barsky: Classifying Lines
63/94 Liang-Barsky: Classifying Lines
64/94 Liang-Barsky: Classifying Lines
65/94 Liang-Barsky: Classifying Lines
66/94 Liang-Barsky: Classifying Lines
67/94 Liang-Barsky: Classifying Lines
68/94 Liang-Barsky: Classifying Lines
69/94 Liang-Barsky Algorithm
70/94 Liang-Barsky Algorithm
71/94 Liang-Barsky Algorithm
72/94 Liang-Barsky Algorithm
73/94 Liang-Barsky Algorithm
74/94 Liang-Barsky Algorithm
75/94 Liang-Barsky Algorithm
76/94 Liang-Barsky Algorithm
77/94 Liang-Barsky Algorithm
78/94 Liang-Barsky Algorithm
79/94 Liang-Barsky Algorithm
80/94 Liang-Barsky Algorithm
81/94 Liang-Barsky Algorithm
82/94 Liang-Barsky Algorithm
83/94 Liang-Barsky Algorithm
84/94 Liang-Barsky Algorithm
85/94 Liang-Barsky Algorithm
86/94 Liang-Barsky Algorithm
87/94 Liang-Barsky Algorithm
88/94 Liang-Barsky Algorithm
89/94 Liang-Barsky Algorithm
90/94 Liang-Barsky Algorithm
91/94 Liang-Barsky Algorithm
92/94 Liang-Barsky Algorithm
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