Sunteți pe pagina 1din 35

Clipping

Comp 535

Line Clipping
What happens when one or both endpoints of
a line segment are not inside the specified
drawing area?

Drawing
Area

Line Clipping
Strategies for clipping:
a) Check (in inner loop) if each point is inside
Works, but slow
if (x xmin and x xmax and y ymin and y ymax)
drawPoint(x,y,c);

b) Find intersection of line with boundary Correct


Clip line to
intersection

Line Clipping: Possible Configurations


1.Both endpoints are inside the region (line AB)
J

No clipping necessary

2.One endpoint in, one


out (line CD)

Clip at intersection point

3.Both endpoints outside


the region:

H
B

a.No intersection (lines EF, GH)


b.Line intersects the region (line IJ)
Clip line at both intersection points
4

Line Clipping: Cohen-Sutherland


Basic algorithm:
Accept lines that have both
endpoints inside the region.

J
D

Reject lines that have both


endpoints less than xmin or
ymin or greater than xmax or
ymax.

Clip the remaining lines at a


region boundary and repeat
the previous steps on the
clipped line segments.

Clip and
retest

H
B

Trivially accept
E
G

Trivially reject

Cohen-Sutherland: Accept/Reject Tests


Assign a 4-bit code to each
endpoint c0, c1 based on its
position:

1st bit (1000):


2nd bit (0100):
3rd bit (0010):
4th bit (0001):

if y > ymax
if y < ymin
if x > xmax
if x < xmin

Test using bitwise functions


if c0 | c1 = 0000
accept (draw)
else if c0 & c1 0000
reject (dont draw)
else clip and retest
6

1001

1000

1010

0001

0000

0010

0101

0100

0110

Cohen-Sutherland Accept/Reject
Accept/reject/redo all based on bit-wise
Boolean ops.
1001

1000

1010

0001

0000

0010

0101

0100

0110

Cohen-Sutherland: Overview
1. Choose an endpoint outside the clipping
region.
2. Clip to a boundary using a consistent
ordering (top to bottom, left to right).
3. Set the new line to have as endpoints the
new intersection point and the other original
endpoint.
4. You may need to run this a few times on a
single line.
8

Cohen-Sutherland: Line Clipping


(x1, y1)

Intersection algorithm:
(x, y)

if c0 0000 then c = c0;


else
c = c1;

dy

dx = x1 x0; dy = y1 y0
if c & 1000
// ymax
x = x0 + dx * (ymax y0) / dy; y = ymax;
(x0, y0)

else if c & 0100


// ymin
x = x0 + dx * (ymin y0) / dy; y = ymin;

ymax

dx

else if c & 0010


// xmax
y = y0 + dy * (xmax x0) / dx; x = xmax;

ymin
xmin

else
// xmin
y = y0 + dy * (xmin x0) / dx; x = xmin;
if c = c0
x0 = x; y0 = y;
else
x1 = x; y1 = y;
9

xmax

Cohen-Sutherland: Line Clipping

(x1, y1) =
(400, 300)
c1 = (1010)

Intersection algorithm:
if c0 0000 then c = c0;
else
c = c1;
dx = x1 x0; dy = y1 y0
if c & 1000
// ymax
x = x0 + dx * (ymax y0) / dy; y = ymax;

ymax=200

(x0, y0) =
(150, 150)
c0 = (0000)

else if c & 0100


// ymin
x = x0 + dx * (ymin y0) / dy; y = ymin;

else if c & 0010


// xmax
y = y0 + dy * (xmax x0) / dx; x = xmax;

ymin=100

Xmin = 100

else
// xmin
y = y0 + dy * (xmin x0) / dx; x = xmin;
if c = c0

x0 = x; y0 = y;
else
x1 = x; y1 = y;
10

dx

xmax = 300

dy

y _

Cohen-Sutherland: Line Clipping

(x1, y1) =
(400, 300)
c1 = (1010)

Intersection algorithm:
if c0 0000 then c = c0;
else
c = c1;
dx = x1 x0; dy = y1 y0
if c & 1000
// ymax
x = x0 + dx * (ymax y0) / dy; y = ymax;

ymax=200

(x0, y0) =
(150, 150)
c0 = (0000)

else if c & 0100


// ymin
x = x0 + dx * (ymin y0) / dy; y = ymin;

else if c & 0010


// xmax
y = y0 + dy * (xmax x0) / dx; x = xmax;

ymin=100

Xmin = 100

else
// xmin
y = y0 + dy * (xmin x0) / dx; x = xmin;
if c = c0

x0 = x; y0 = y;
else
x1 = x; y1 = y;
11

dx

xmax = 300

dy

y _

Cohen-Sutherland: Line Clipping

(x1, y1) =
(400, 300)
c1 = (1010)

Intersection algorithm:
if c0 0000 then c = c0;
else
c = c1;
dx = x1 x0; dy = y1 y0
if c & 1000
// ymax
x = x0 + dx * (ymax y0) / dy; y = ymax;

ymax=200

(x0, y0) =
(150, 150)
c0 = (0000)

else if c & 0100


// ymin
x = x0 + dx * (ymin y0) / dy; y = ymin;

else if c & 0010


// xmax
y = y0 + dy * (xmax x0) / dx; x = xmax;

ymin=100

Xmin = 100

else
// xmin
y = y0 + dy * (xmin x0) / dx; x = xmin;
if c = c0

c
1010

x0 = x; y0 = y;
else
x1 = x; y1 = y;
12

dx

xmax = 300

dy

y _

Cohen-Sutherland: Line Clipping

(x1, y1) =
(400, 300)
c1 = (1010)

Intersection algorithm:
if c0 0000 then c = c0;
else
c = c1;
dx = x1 x0; dy = y1 y0
if c & 1000
// ymax
x = x0 + dx * (ymax y0) / dy; y = ymax;

ymax=200

(x0, y0) =
(150, 150)
c0 = (0000)

else if c & 0100


// ymin
x = x0 + dx * (ymin y0) / dy; y = ymin;

else if c & 0010


// xmax
y = y0 + dy * (xmax x0) / dx; x = xmax;

ymin=100

Xmin = 100

else
// xmin
y = y0 + dy * (xmin x0) / dx; x = xmin;
if c = c0

c
1010

x0 = x; y0 = y;
else
x1 = x; y1 = y;
13

dx
250

xmax = 300

dy
150

y _

Cohen-Sutherland: Line Clipping

(x1, y1) =
(400, 300)
c1 = (1010)

Intersection algorithm:
if c0 0000 then c = c0;
else
c = c1;
dx = x1 x0; dy = y1 y0
if c & 1000
// ymax
x = x0 + dx * (ymax y0) / dy; y = ymax;

ymax=200

(x0, y0) =
(150, 150)
c0 = (0000)

else if c & 0100


// ymin
x = x0 + dx * (ymin y0) / dy; y = ymin;

else if c & 0010


// xmax
y = y0 + dy * (xmax x0) / dx; x = xmax;

ymin=100

Xmin = 100

else
// xmin
y = y0 + dy * (xmin x0) / dx; x = xmin;
if c = c0

c
1010

x0 = x; y0 = y;
else
x1 = x; y1 = y;
14

dx
250

xmax = 300

dy
150

y _

Cohen-Sutherland: Line Clipping

(x1, y1) =
(400, 300)
c1 = (1010)

Intersection algorithm:
if c0 0000 then c = c0;
else
c = c1;
dx = x1 x0; dy = y1 y0
if c & 1000
// ymax
x = x0 + dx * (ymax y0) / dy; y = ymax;

ymax=200

(x0, y0) =
(150, 150)
c0 = (0000)

else if c & 0100


// ymin
x = x0 + dx * (ymin y0) / dy; y = ymin;

else if c & 0010


// xmax
y = y0 + dy * (xmax x0) / dx; x = xmax;

ymin=100

Xmin = 100

else
// xmin
y = y0 + dy * (xmin x0) / dx; x = xmin;
if c = c0

c
1010

x0 = x; y0 = y;
else
x1 = x; y1 = y;
15

dx
250

xmax = 300

dy
150

x
233

y _
200

Cohen-Sutherland: Line Clipping

(x1, y1) =
(400, 300)
c1 = (1010)

Intersection algorithm:
if c0 0000 then c = c0;
else
c = c1;
dx = x1 x0; dy = y1 y0
if c & 1000
// ymax
x = x0 + dx * (ymax y0) / dy; y = ymax;

ymax=200

(x0, y0) =
(150, 150)
c0 = (0000)

else if c & 0100


// ymin
x = x0 + dx * (ymin y0) / dy; y = ymin;

else if c & 0010


// xmax
y = y0 + dy * (xmax x0) / dx; x = xmax;

ymin=100

Xmin = 100

else
// xmin
y = y0 + dy * (xmin x0) / dx; x = xmin;
if c = c0

c
1010

x0 = x; y0 = y;
else
x1 = x; y1 = y;
16

dx
250

xmax = 300

dy
150

x
233

y _
200

Cohen-Sutherland: Line Clipping

(x1, y1) =
(400, 300)
c1 = (1010)

Intersection algorithm:
if c0 0000 then c = c0;
else
c = c1;
dx = x1 x0; dy = y1 y0
if c & 1000
// ymax
x = x0 + dx * (ymax y0) / dy; y = ymax;

ymax=200

(x0, y0) =
(150, 150)
c0 = (0000)

else if c & 0100


// ymin
x = x0 + dx * (ymin y0) / dy; y = ymin;

else if c & 0010


// xmax
y = y0 + dy * (xmax x0) / dx; x = xmax;

ymin=100

Xmin = 100

else
// xmin
y = y0 + dy * (xmin x0) / dx; x = xmin;
if c = c0

c
1010

x0 = x; y0 = y;
else
x1 = x; y1 = y;
17

dx
250

xmax = 300

dy
150

x
233

y _
200

Cohen-Sutherland Line Clip Examples


H

1000

1001

1010

if c0 | c1 = 0000
accept (draw)
else if c0 & c1 0000
reject (dont draw) 0001
else clip and retest A

0000

0010

0101

0100

0110

B
A 0001
B 0100
OR 0101
AND 0000
clip

C
0000
D
0010
OR 0010
AND 0000
clip

E
0000
F
0000
OR 0000
AND 0000
accept
18

G
0000
H
1010
OR 1010
AND 0000
clip

I
0110
J
0010
OR 0110
AND 0010
reject

Cohen-Sutherland Line Clip Examples


H

1000

1001

1010

if c0 | c1 = 0000
accept (draw)
else if c0 & c1 0000
reject (dont draw) 0001
else clip and retest A

0000

0010

0101

0100
B

remove
AB

A 0001
A 0001
OR 0001
AND 0001
reject

remove
CD

C 0000
C 0000
OR 0000
AND 0000
accept
19

remove
GH

0110
G
0000
G 0000
OR 0000
AND 0000
accept

Cohen-Sutherland: Summary
1. Choose an endpoint outside the clipping
region.
2. Clip to a boundary using a consistent
ordering (top to bottom, left to right).
3. Set the new line to have as endpoints the
new intersection point and the other original
endpoint.
4. You may need to run this a few times on a
single line.
20

Polygon Clipping
What about polygons?

21

Polygon Clipping: Algorithm


Clip polygon to ymin and ymax:
Create empty output vertex list
Process input list (v0, v1, , vn) where v0 = vn
For each input vertex (vi where 0 i n1):
If vi is inside region Add vi to output list.
If the line between vi and vi+1 intersects clipping
boundaries Add intersection point(s) to output list.

Repeat: Clip to xmin and xmax


Post-process:
Remove degenerate sections that have collapsed
to region boundary.
22

Polygon Clipping: Example


Clip first to ymin and ymax

v0

v8

vertex: v0

v6

v7

ymax

p0

Inside region: No
v9

Line intersect
boundary: Yes

v2

Add p0 to output list


ymin

v3

Input vertex list: (v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list: p0


23

v5

v1

v4

Polygon Clipping: Example


Clip first to ymin and ymax

v0

v8

vertex: v1

v6

v7

ymax

p0

inside region: yes


v9

line intersect
boundary: no

v2

add v1 to output list


ymin

v3

Input vertex list: (v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list: p0 , v1


24

v5

v1

v4

Polygon Clipping: Example


Clip first to ymin and ymax

v0

v8

vertex: v2

v6

v7

ymax

p0

inside region: yes


v9

line intersect
boundary: yes

v2

add v2, p1 to output list

v5

v1
p1

ymin

v3

Input vertex list: (v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list: p0 , v1 , v2, p1


25

v4

Polygon Clipping: Example


Clip first to ymin and ymax

v0

v8

vertex: v3

v6

v7

ymax

p0

inside region: no
v9

line intersect
boundary: no

v5

v1

v2
p1

ymin

v3

Input vertex list: (v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list: p0, v1, v2, p1


26

v4

Polygon Clipping: Example


Clip first to ymin and ymax

v0

v8

vertex: v4

v6

v7

ymax

p0

inside region: no
v9

line intersect
boundary: yes

v2

add p2 to output list

v5

v1
p1

ymin

v3

Input vertex list: (v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list: p0, v1, v2, p1 , p2


27

p2
v4

Polygon Clipping: Example


Clip first to ymin and ymax

v0

v8

vertex: v5

v6

v7

ymax

p3

p0

inside region: yes


v9

line intersect
boundary: yes

v2

add v5, p3 to output list

v5

v1
p1

ymin

v3

Input vertex list: (v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list: p0, v1, v2, p1, p2 , v5, p3


28

p2
v4

Polygon Clipping: Example


Clip first to ymin and ymax

v0

v8

vertex: v6

v6

v7

ymax

p3

p0

inside region: no
v9

line intersect
boundary: no

v5

v1

v2
p1

ymin

v3

Input vertex list: (v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list: p0, v1, v2, p1, p2, v5, p3


29

p2
v4

Polygon Clipping: Example


Clip first to ymin and ymax

v0

v8

vertex: v7

v6

v7

ymax

p3

p0

inside region: no
v9

line intersect
boundary: no

v5

v1

v2
p1

ymin

v3

Input vertex list: (v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list: p0, v1, v2, p1, p2, v5, p3


30

p2
v4

Polygon Clipping: Example


Clip first to ymin and ymax
v8

vertex: v8

v6

v7
v0

p4

ymax

p3

p0

inside region: no
v9

line intersect
boundary: yes

v2

add p4 to output list

v5

v1
p1

ymin

v3

Input vertex list: (v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list: p0, v1, v2, p1, p2, v5, p3, p4
31

p2
v4

Polygon Clipping: Example


Clip first to ymin and ymax
v8

vertex: v9

v6

v7

p4

p5

ymax

v0

p3

p0

inside region: yes


v9

line intersect
boundary: yes

v2

add v9, p5 to output list

v5

v1
p1

ymin

v3

Input vertex list: (v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list: p0, v1, v2, p1, p2, v5, p3, p4 , v9, p5
32

p2
v4

Polygon Clipping: Example


This gives us a new polygon
p4

p3

p5

ymax

p0

v9

v2
p1

ymin

with vertices: (p0, v1, v2, p1, p2, v5, p3, p4, v9, p5)
33

v5

v1

p2

Polygon Clipping: Example (cont.)


Now clip to xmin and xmax

xmin

xmax
p4

p3

p5
p0

p9

v9
p6
v2

p7
p1

Input vertex list: = (p0, v1, v2, p1, p2, v5, p3, p4, v9, p5)

Output vertex list: (p0, p6, p7, v2, p1, p8, p9, p3, p4, v9, p5)
34

v5

v1

p8

p2

Polygon Clipping: Example (cont.)


Now post-process
xmin

xmax
p4

p3

p5
p0

p9

v9

p6
v2

p7
pv13

p8

Output vertex list: (p0, p6, p7, v2, p1, p8, p9, p3, p4, v9, p5)

Post-process: (p0, p6, p9, p3,) and (p7, v2, p1, p8) and (v4, v9, p5)
35

S-ar putea să vă placă și