You are on page 1of 14

# 13 August 2001

Line Clipping in 2D

## Why would we clip?

We clip objects to our view before rasterization. Why?

## To avoid unnecessary work:

pixel coordinate calculations
parameter interpolation

13 August 2001

2D Viewing

Clipping window
(xwmin, ywmax)

(xwmax, ywmax)

(xwmin, ywmin)
(xwmax, ywmin)

13 August 2001

## What are the basic steps?

1. Determine if the line needs clipping
May be able to trivially accept or reject some lines

## 2. Find intersections of line with viewport

We can use y = mx + b to do this

## We want to determine which edges of the viewport to test lines

against and avoid unnecessary tests.

Cohen

Top
op--Left Top Top
op--Right

## Left Inside Right

Bottom
ottom--Left Bottom Bottom
ottom--Right

TBRL
14 September 2009 SCG 3023 6

13 August 2001

## Cohen--Sutherland line clipping

Cohen
Region codes T B R L
Bit 1 2 3 4
1001 1000 1010

## 0101 0100 0110

14 September 2009 SCG 3023 7

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

## Chris Weigle - Comp 136 4

13 August 2001

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.

## How to check for intersection?

if bit 1 = 1  there is intersection on TOP boundary.
if bit 2 = 1  .. .. .. .. BOTTOM ..
if bit 3 = 1  .. .. .. .. RIGHT ..
If bit 4 = 1  .. .. .. .. LEFT ..
How to find intersection point?
use line equation
intersection with LEFT or RIGHT boundary.
x = xwmin (LEFT) x = xwmax (RIGHT)

y = y1 + m(x –x1)
intersection with BOTTOM or TOP boundary.
y = ywmin (BOTTOM) y = ywmax (TOP)

x = x1 + (y –y1)/m

13 August 2001

B1
1001 1000 1010
D1
B2

C1 A2

C2

D2

## 14 September 2009 SCG 3023 11

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

## Chris Weigle - Comp 136 6

13 August 2001

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

## 14 September 2009 SCG 3023 13

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

13 August 2001

Example
D1 algorithm
1001 1000 1010
D1’
D1’’

A2

C1’

C2
D2’’

## 0101 0100 0110

D2’
D2
14 September 2009 SCG 3023 15

Example

(150, 100)

(10, 10)

## Diberi tetingkap ketipan seperti di atas. Sekiranya titik P1 ialah

(0, 120) dan titik P2(130, 5) . Dapatkan titik-titik persilangan
yang membentuk garisan selepas proses ketipan. Gunakan
algoritma Cohen-Sutherland
14 September 2009 SCG 3023 16

## Chris Weigle - Comp 136 8

13 August 2001

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

## The good and the bad

What’s the maximum number of clips for an accepted line?

## What’s the maximum number of clips for a rejected line?

Good:
Easy to implement
Early accept/reject tests

Slow for many clipped lines

13 August 2001

## Liang--Barsky Line Clipping

Liang
• Based on parametric equation of a line:
x = x1 + u.
u.x
0≤u≤1
y = y1 + u.
u.y

## • Similarly, the clipping window is represented by:

xwmin ≤ x1 + u.
u.x ≤ xwmax
ywmin ≤ y1 + u.
u.y ≤ ywmax
… or,
u. pk ≤ qk k = 1, 2, 3, 4
• where:
p1 = -  x , q1 = x1 – xwmin
p2 =  x , q2 = xwmax- x1
p3 = -  y , q3 = y1 – ywmin
p4 =  y , q4 = ywmax - y1

## 14 September 2009 SCG 3023 19

Liang--Barsky (continued)
Liang
• Clipped line will be:
x1’ = x1 + u1. x; u1 ≥ 0
y1’ = y1 + u1. y;

## x2’ = x1 + u2. x; u2 ≤ 1

y2’ = y1 + u2. y;

• Calculate uk

uk = qk/pk

## Chris Weigle - Comp 136 10

13 August 2001

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)

## • Consider our previous example where:

xwmin = 0, xwmax = 100
ywmin = 0, ywmax = 50

And the line we want to clip connects P1(10, 10) and P2(110, 40)

## 14 September 2009 SCG 3023 21

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

## Chris Weigle - Comp 136 11

13 August 2001

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

## 14 September 2009 SCG 3023 23

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

## Chris Weigle - Comp 136 12

13 August 2001

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

## 14 September 2009 SCG 3023 25

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

## Chris Weigle - Comp 136 13

13 August 2001

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

## x2’ = x1 + u2. x (u2 = 9/10)

= 10 + 0.9(100) = 100
y2’ = y2 + u2. y
= 10 + 0.9(30) = 37
**Homework: Use different values of xwmin, xwmax, ywmin ,
ywmax , P1 and P2 for exercise.
14 September 2009 SCG 3023 27

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