CPSC 441 Computer Graphics: Clipping Lines Jinxiang Chai - PowerPoint PPT Presentation

1 / 127
About This Presentation
Title:

CPSC 441 Computer Graphics: Clipping Lines Jinxiang Chai

Description:

Given a point (x, y) and clipping window (xmin, ymin), (xmax, ... Big Optimization: trivial ... of u for which x and y both inside the viewing window. 72 /128 ... – PowerPoint PPT presentation

Number of Views:556
Avg rating:3.0/5.0
Slides: 128
Provided by: symo5
Category:

less

Transcript and Presenter's Notes

Title: CPSC 441 Computer Graphics: Clipping Lines Jinxiang Chai


1
CPSC 441 Computer GraphicsClipping
LinesJinxiang Chai
2
Why Clip?
  • We do not want to waste time drawing objects that
    are outside of viewing window (or clipping window)

3
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)
4
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)
xminltxltxmax?
(xmin,ymin)
yminltyltymax?
5
Clipping 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?
6
Clipping 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
7
Clipping 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
8
Clipping Lines
9
Clipping Lines
10
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)
11
Clipping Lines
12
Outline
  • Simple line clipping algorithm
  • Cohen-Sutherland
  • Liang-Barsky

13
Clipping 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

14
Clipping Lines Simple Algorithm
15
Clipping Lines Simple Algorithm
16
Intersecting Two Lines
17
Intersecting Two Lines
18
Intersecting Two Lines
19
Intersecting Two Lines
20
Intersecting Two Lines
21
Intersecting Two Lines
Substitute t or s back into equation to find
intersection
22
Clipping Lines Simple Algorithm
23
Clipping Lines Simple Algorithm
24
Clipping Lines Simple Algorithm
25
Clipping Lines Simple Algorithm
26
Clipping Lines Simple Algorithm
27
Clipping Lines Simple Algorithm
28
Clipping Lines Simple Algorithm
29
Clipping Lines Simple Algorithm
30
Clipping Lines Simple Algorithm
31
Clipping Lines Simple Algorithm
32
Clipping Lines Simple Algorithm
33
Clipping Lines Simple Algorithm
34
Clipping Lines Simple Algorithm
35
Clipping Lines Simple Algorithm
36
Clipping Lines Simple Algorithm
37
Clipping Lines Simple Algorithm
  • Lots of intersection tests makes algorithm
    expensive
  • Complicated tests to determine if intersecting
    rectangle
  • Is there a better way?

38
Trivial Accepts
  • Big Optimization trivial accepts/rejects
  • How can we quickly decide whether line segment is
    entirely inside window
  • Answer test both endpoints

39
Trivial Accepts
  • Big Optimization trivial accepts/rejects
  • How can we quickly decide whether line segment is
    entirely inside window
  • Answer test both endpoints

40
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

41
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

42
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

43
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 edges

bit 4
bit 3
bit 2
bit 1
left
right
bottom
top
44
Cohen-Sutherland Algorithm
bit 4
bit 3
bit 2
bit 1
left
right
bottom
top
top
right
left
Region code?
bottom
45
Cohen-Sutherland Algorithm
bit 4
bit 3
bit 2
bit 1
left
right
bottom
top
top
right
left
0010
bottom
?
46
Cohen-Sutherland Algorithm
bit 4
bit 3
bit 2
bit 1
left
right
bottom
top
top
right
left
0010
bottom
0100
47
Cohen-Sutherland Algorithm
48
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

49
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

50
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

Line is outside the window! reject
51
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

Line is inside the window! draw
52
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

53
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)

54
Example 1
55
Example 1
56
Example 1
57
Example 1
58
Example 2
59
Example 2
60
Example 2
61
Example 2
62
Example 2
63
Example 2
64
Example 2
65
Example 3
66
Example 3
67
Example 3
68
Example 3
69
Cohen-Sutherland Algorithm
  • Extends easily to 3D line clipping
  • 27 regions
  • 6 bits

70
Cohen-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

71
Liang-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

72
Liang-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.

73
Liang-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.

74
Liang-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

75
Liang-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

76
Liang-Barsky Algorithm
  • Current clipped line (u10,u21)

(2,1)
(-.5,-.5)
77
Liang-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
78
Liang-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
79
Liang-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
80
Liang-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
81
Liang-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
82
Liang-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)
83
Liang-Barsky Algorithm
  • Current clipped line (u10.2,u21)

(2,1)
  • u1ltu2?
  • yes continue
  • no the line is completely outside window

(-.5,-.5)
84
Liang-Barsky Algorithm
  • Current clipped line (u10.2,u21)

(2,1)
(-.5,-.5)
85
Liang-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
86
Liang-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)
87
Liang-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
88
Liang-Barsky Algorithm
  • Current clipped line (u10.2,u20.6)

(2,1)
  • u1ltu2?
  • yes continue
  • no the line is completely outside window

(-.5,-.5)
89
Liang-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
90
Liang-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)
91
Liang-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
92
Liang-Barsky Algorithm
  • Current clipped line (u10.333,u20.6)

(2,1)
(-.5,-.5)
93
Liang-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
94
Liang-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
95
Liang-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)
96
Liang-Barsky Algorithm
  • Check the left edge (u10.333,u20.6)

(2,1)
u1ltu2
(-.5,-.5)
xnew1x1?xu1
xnew2x2?xu2
ynew1y1?yu1
ynew2y2?yu2
97
Liang-Barsky Algorithm
98
Liang-Barsky Algorithm
99
Liang-Barsky Algorithm
100
Liang-Barsky Algorithm
101
Liang-Barsky Algorithm
102
Liang-Barsky Algorithm
103
Liang-Barsky Algorithm
104
Liang-Barsky Algorithm
105
Liang-Barsky Algorithm
106
Liang-Barsky Algorithm
107
Liang-Barsky Algorithm
108
Liang-Barsky Algorithm
109
Liang-Barsky Algorithm
110
Liang-Barsky Algorithm
111
Liang-Barsky Algorithm
112
Liang-Barsky Algorithm
113
Liang-Barsky Algorithm
u1gtu2!!
114
Liang-Barsky Algorithm
115
Liang-Barsky Algorithm
116
Liang-Barsky Algorithm
117
Liang-Barsky Algorithm
118
Liang-Barsky Algorithm
119
Liang-Barsky Algorithm
120
Liang-Barsky Algorithm
121
Liang-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

122
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 doesnt consider trivial
    accepts/rejects
  • Best when many lines must be clipped
  • Nicholl et al. Fastest, but doesnt do 3D

123
Curve Clipping
124
Curve Clipping
125
Curve Clipping
  • Approximate a curve using a set of straight-line
    segments
  • Apply line clipping for curve clipping

126
Next Lecture
  • Polygon fill-area clipping

127
Next Lecture
  • Polygon fill-area clipping
Write a Comment
User Comments (0)
About PowerShow.com