Escolar Documentos
Profissional Documentos
Cultura Documentos
Line Clipping in 2D
2D Viewing
Clipping window
(xwmin, ywmax)
(xwmax, ywmax)
(xwmin, ywmin)
(xwmax, ywmin)
Top
op--Left Top Top
op--Right
Bottom
ottom--Left Bottom Bottom
ottom--Right
TBRL
14 September 2009 SCG 3023 6
Region coding
How would you decide which region an endpoint is in?
e.g
if (x<xwmin)&&(y>ymax) the point is at the Top-
Top-Left
algorithm
1. Assign a region code for each endpoints.
2. If both endpoints have a region code 0000 ---
--- trivially accept
these line.
3. Else, perform the logical AND operation for both region codes.
3.1 if the result is not 0000 - trivially reject the line.
3.2 else – (result = 0000, need clipping)
3.2.1. Choose an endpoint of the line that is outside the window.
3.2.2. Find the intersection point at the window boundary (base on
region code).
3.2.3. Replace endpoint with the intersection point and update the
region code.
3.2.4. Repeat step 2 until we find a clipped line either trivially accepted
or trivially rejected.
4. Repeat step 1 for other lines.
y = y1 + m(x –x1)
intersection with BOTTOM or TOP boundary.
y = ywmin (BOTTOM) y = ywmax (TOP)
x = x1 + (y –y1)/m
C1 A2
C2
Example
algorithm
1001 1000 1010 1. A1=0000,A2=0000
2. (both 0000) – Yes
-> accept &
draw
A2 3.
3.1
0001 A1 0000 0010 3.2
3.2.1
3.2.2
3.2.3
3.2.4
Example
B1 algorithm
1001 1000 1010 1. B1=1001,B2=1010
B2 2. (both 0000) – No
3. AND Operation
B1 1001
A2 B2 1010
Result 1000
0001 A1 0000 0010
3.1 (not 0000) – Yes
reject
3.2
3.2.1
3.2.2
3.2.3
0101 0100 0110 3.2.4
Example
algorithm
1001 1000 1010 1.1.C1=0001,C2=0000
C1=0001,C2=0000
2.2.(both
(both0000)
0000)––No
Yes
3.->AND
acceptOperation
&
C1 draw 0001
A2 3. 0000
3.1result 0000
0001 A1 0000 0010 3.1(not
3.2 0000) - No
C1’ 3.2.3.2.1
(0000)-Yes
3.2.1.
3.2.2 choose C1
C2 3.2.2.
3.2.3 Intersection
point,
3.2.4 C1’ at
LEFT
0101 0100 0110 3.2.3 C1 <- C1’
C1 = 0000
3.2.4 repeat 2
14 September 2009 SCG 3023 14
Example
D1 algorithm
1001 1000 1010
D1’
D1’’
A2
C2
D2’’
Example
(150, 100)
(10, 10)
answer
1. P1=1001, P2=0100
2. (both 0000) – yes No ACCEPT & DRAW
3. AND Operation
Endpoints
B1 after
1001 clipping
1000
0000
P1’’ B2
= (22,
0100 P2’ = 124, 10)
100)
Result 0000
3.1 (not 0000) – no
3.2 (0000) yes
3.2.1choose P2 P1
P1’
3.2.2 intersection with TOP LEFTboundary
BOTTOM boundary
boundary
m = (5-
(5-120)/(130
120)/(130--0) = -0.8846
• y == x1
x y1++(ym(x –x1) where
–y1)/m wherey =x 10;
100;
= 10;
• y = 120
x 10 ++(100-
130 (100
-(10-
(10
0.8846(10-
0.8846(10
-5)/
111)/
-0.8846
-0.8846
-0) ==111.15
124.35
= 22.44
==111
=124
22
• P1’’
P1’ == (124,
P2’ (10,
(22, 111)
100)
10)
3.2.3 update region code P2’ P1’ ==0000
P1’’ 1000
0000(TOP)
3.2.4 repeat step 2
14 September 2009 SCG 3023 17
Good:
Easy to implement
Early accept/reject tests
Bad:
Slow for many clipped lines
Liang--Barsky (continued)
Liang
• Clipped line will be:
x1’ = x1 + u1. x; u1 ≥ 0
y1’ = y1 + u1. y;
uk = qk/pk
Liang--Barsky (continued)
Liang
• u1 : maximum value between 0 and u (for pk < 0), where
starting value for u1 is 0 (u1 =0)
• u2 : minimum value between u and 1 (for pk > 0), where
starting value for u2 is 1 (u2 = 1)
And the line we want to clip connects P1(10, 10) and P2(110, 40)
Liang--Barsky (example)
Liang
• Lets construct a table:
k pk qk uk
- x x1 – xwmin
1 = -(110
(110--10) = 10-
10-0 = 10
= -100
x xwmax- x1
2
=110--10=100
=110 = 100 – 10 = 90
- y y1 – ywmin
3 = -(40-
(40-10) = 10–
10–0 = 10
=-30
y ywmax - y1
4
= 40-
40-10=30 = 50 – 10 = 40
Liang--Barsky (example)
Liang
• Lets construct a table:
k pk qk uk
- x x1 – xwmin
u1 1 = -(110
(110--10) = 10-
10-0 = 10
= -100
x xwmax- x1 Since
2 pk < 0
=110--10=100
=110 = 100 – 10 = 90
- y y1 – ywmin
u1 3 = -(40-
(40-10) = 10–
10–0 = 10
=-30
y ywmax - y1
4
= 40-
40-10=30 = 50 – 10 = 40
Liang--Barsky (example)
Liang
• u1 : maximum value between 0 and u (for pk < 0)!
k pk qk uk
- x x1 – xwmin u=10/(-100)
u=10/(-
u1 1 = -(110
(110--10) = 10-
10-0 = 10 =-1/10 We opt
= -100 u1 =0,
x xwmax- x1
2
=110--10=100
=110 = 100 – 10 = 90
- y y1 – ywmin u=10/(-30)
u=10/(-
u1 3 = -(40-
(40-10) = 10–
10–0 = 10 =-1/3
=-30
y ywmax - y1
4
= 40-
40-10=30 = 50 – 10 = 40
Liang--Barsky (example)
Liang
• u2 : minimum value between u (for pk > 0) and 1
k pk qk uk
- x x1 – xwmin u=10/(-100)
u=10/(-
1 = -(110
(110--10) = 10-
10-0 = 10 =-1/10 We opt
= -100 u1 =0,
x xwmax- x1
u2 2
=110--10=100
=110 = 100 – 10 = 90
- y y1 – ywmin u=10/(-30)
u=10/(-
3 = -(40-
(40-10) = 10–
10–0 = 10 =-1/3 Since
=-30 pk > 0
y ywmax - y1
u2 4
= 40-
40-10=30 = 50 – 10 = 40
Liang--Barsky (example)
Liang
• u2 : minimum value between u (for pk > 0) and 1
k pk qk uk
- x x1 – xwmin u=10/(-100)
u=10/(-
= 10-
10-0 = 10 =-1/10 We opt
1 = -(110
(110--10)
u1 =0,
= -100
x xwmax- x1 u=90/100
2 We opt
u2 =110--10=100
=110 = 100 – 10 = 90 =9/10 u2 = 0.9
- y y1 – ywmin u=10/(-30)
u=10/(-
3 = -(40-
(40-10) = 10–
10–0 = 10 =-1/3
=-30
y ywmax - y1 u=40/30)
u2 4 =4/3
= 40-
40-10=30 = 50 – 10 = 40
Liang--Barsky (example)
Liang
• If u1 > u2 then reject line (completely outside clipping
window!)
• Clipped line will be:
x1’ = x1 + u1. x (u1 = 0)
= 10 + 0.(100) = 10
y1’ = y1 + u1. y
= 10 + 0.(30) = 10
algorithm
• 1. Initial value : u1 = 0, u2 = 1
• 2. For k = 1, 2, 3, 4;
– 2.1 calculate Pk dan qk
– 2.2 calculate rk = qk/ Pk
– 2.2 if (Pk <0) –find u1 ( if (rk>u1), u1=rk )
– 2.3 if (Pk > 0)–
0)– find u2 ( if (rk<u2), u2=rk )
– 2.4 if (Pk = 0) and (qk< 0) ; reject the line; goto step 6
• 3. If (u1> u2) ; reject the line; goto step 6
• 5. Find the clipped line
•
• x1’ = x1 + u1. ∆x
• y1’ = y1 + u1. ∆y
•
• x2’ = x1 + u2. ∆x
• y2’ = y1 + u2. ∆y
•
• 6. Repeat step 1 –5 for other lines.
•14 September 2009 SCG 3023 28