Title: Dr' Scott Schaefer
1Clipping Lines
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 Yes
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
12Clipping 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
13Clipping Lines Simple Algorithm
14Clipping Lines Simple Algorithm
15Clipping Lines Simple Algorithm
16Clipping Lines Simple Algorithm
17Clipping Lines Simple Algorithm
18Clipping Lines Simple Algorithm
19Clipping Lines Simple Algorithm
20Clipping Lines Simple Algorithm
21Clipping Lines Simple Algorithm
22Clipping Lines Simple Algorithm
23Clipping Lines Simple Algorithm
24Clipping Lines Simple Algorithm
25Clipping Lines Simple Algorithm
26Window 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)
27Clipping Lines Simple Algorithm
- Lots of intersection tests makes algorithm
expensive - Complicated tests to determine if intersecting
rectangle - Is there a better way?
28Trivial Accepts
- Big Optimization trivial accepts/rejects
- How can we quickly decide whether line segment is
entirely inside window - Answer test both endpoints
29Trivial Accepts
- Big Optimization trivial accepts/rejects
- How can we quickly decide whether line segment is
entirely inside window - Answer test both endpoints
30Trivial 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
31Trivial 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
32Cohen-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
33Cohen-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
34Cohen-Sutherland Algorithm
35Cohen-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
36Cohen-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
37Cohen-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
38Cohen-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
39Cohen-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
40Cohen-Sutherland Algorithm
41Cohen-Sutherland Algorithm
42Cohen-Sutherland Algorithm
43Cohen-Sutherland Algorithm
44Cohen-Sutherland Algorithm
45Cohen-Sutherland Algorithm
46Cohen-Sutherland Algorithm
47Cohen-Sutherland Algorithm
48Cohen-Sutherland Algorithm
49Cohen-Sutherland Algorithm
50Cohen-Sutherland Algorithm
51Cohen-Sutherland Algorithm
52Cohen-Sutherland Algorithm
53Cohen-Sutherland Algorithm
54Cohen-Sutherland Algorithm
55Liang-Barsky Algorithm
- Uses parametric form of line for clipping
- Lines are oriented
- Classify lines as moving inside to out or
outside to in - Dont find actual intersection points
- Find parameter values on line to draw
56Intersecting Two Parametric Lines
57Intersecting Two Parametric Lines
58Intersecting Two Parametric Lines
59Intersecting Two Parametric Lines
60Intersecting Two Parametric Lines
61Intersecting Two Parametric Lines
Substitute t or s back into equation to find
intersection
62Liang-Barsky Algorithm
63Liang-Barsky Algorithm
64Liang-Barsky Algorithm
65Liang-Barsky Algorithm
66Liang-Barsky Algorithm
67Liang-Barsky Algorithm
68Liang-Barsky Algorithm
69Liang-Barsky Algorithm
70Liang-Barsky Algorithm
71Liang-Barsky Algorithm
72Liang-Barsky Algorithm
73Liang-Barsky Algorithm
74Liang-Barsky Algorithm
75Liang-Barsky Algorithm
76Liang-Barsky Algorithm
77Liang-Barsky Algorithm
78Liang-Barsky Algorithm
79Liang-Barsky Algorithm
80Liang-Barsky Algorithm
81Liang-Barsky Algorithm
82Liang-Barsky Algorithm
83Liang-Barsky Algorithm
84Liang-Barsky Algorithm
85Liang-Barsky Algorithm
86Comparison
- 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
87Line 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