02 clipping

35
Clipping Comp 535

Upload: alex-solomon-a

Post on 18-Jul-2016

220 views

Category:

Documents


1 download

DESCRIPTION

clipping

TRANSCRIPT

ClippingComp 535

2

Line Clipping

What happens when one or both endpoints of

a line segment are not inside the specified

drawing area?

Drawing

Area

3

Line Clipping

• Strategies for clipping:

a) Check (in inner loop) if each point is inside

Works, but slow

b) Find intersection of line with boundary Correct

if (x ≥ xmin and x ≤ xmax and y ≥ ymin and y ≤ ymax)

drawPoint(x,y,c);

Clip line to

intersection

4

Line Clipping: Possible Configurations

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

• No clipping necessary

2.One endpoint in, one

out (line CD)

• Clip at intersection point

3.Both endpoints outside

the region:

a.No intersection (lines EF, GH)

b.Line intersects the region (line IJ)

• Clip line at both intersection points

A

B

C

D

F

E

I

J

G

H

5

Line Clipping: Cohen-Sutherland

• Basic algorithm:

• Accept lines that have both

endpoints inside the region.

F

E

Trivially reject

A

BTrivially accept

H

C

D

I

J

G

Clip and

retest

Clip the remaining lines at a

region boundary and repeat

the previous steps on the

clipped line segments.

Reject lines that have both

endpoints less than xmin or

ymin or greater than xmax or

ymax.

6

Cohen-Sutherland: Accept/Reject Tests

• Assign a 4-bit code to each

endpoint c0, c1 based on its

position:• 1st bit (1000): if y > ymax

• 2nd bit (0100): if y < ymin

• 3rd bit (0010): if x > xmax

• 4th bit (0001): if x < xmin

• Test using bitwise functionsif c0 | c1 = 0000

accept (draw)else if c0 & c1 0000

reject (don’t draw)else clip and retest

01000101 0110

10001001 1010

0001 00100000

7

• Accept/reject/redo all based on bit-wise

Boolean ops.

Cohen-Sutherland Accept/Reject

01000101 0110

10001001 1010

0001 00100000

8

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.

9

Cohen-Sutherland: Line Clipping

Intersection algorithm:

if c0 0000 then c = c0;else c = c1;

dx = x1 – x0; dy = y1 – y0if c & 1000 // ymax

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

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;

else // xmin

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

if c = c0

x0 = x; y0 = y;

elsex1 = x; y1 = y;

(x1, y1)

(x0, y0)

ymax

ymin

dx

dy

(x, y)

xmin xmax

10

Cohen-Sutherland: Line Clipping

Intersection algorithm:

if c0 0000 then c = c0;else c = c1;

dx = x1 – x0; dy = y1 – y0if c & 1000 // ymax

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

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;

else // xmin

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

if c = c0

x0 = x; y0 = y;

elsex1 = x; y1 = y;

c dx dy x y _

(x1, y1) =

(400, 300)

c1 = (1010)

(x0, y0) =

(150, 150)

c0 = (0000)

ymax=200

ymin=100

Xmin = 100 xmax = 300

11

Cohen-Sutherland: Line Clipping

Intersection algorithm:

if c0 0000 then c = c0;else c = c1;

dx = x1 – x0; dy = y1 – y0if c & 1000 // ymax

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

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;

else // xmin

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

if c = c0

x0 = x; y0 = y;

elsex1 = x; y1 = y;

c dx dy x y _

(x1, y1) =

(400, 300)

c1 = (1010)

(x0, y0) =

(150, 150)

c0 = (0000)

ymax=200

ymin=100

Xmin = 100 xmax = 300

12

Cohen-Sutherland: Line Clipping

Intersection algorithm:

if c0 0000 then c = c0;else c = c1;

dx = x1 – x0; dy = y1 – y0if c & 1000 // ymax

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

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;

else // xmin

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

if c = c0

x0 = x; y0 = y;

elsex1 = x; y1 = y;

c dx dy x y _

1010

(x1, y1) =

(400, 300)

c1 = (1010)

(x0, y0) =

(150, 150)

c0 = (0000)

ymax=200

ymin=100

Xmin = 100 xmax = 300

13

Cohen-Sutherland: Line Clipping

Intersection algorithm:

if c0 0000 then c = c0;else c = c1;

dx = x1 – x0; dy = y1 – y0if c & 1000 // ymax

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

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;

else // xmin

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

if c = c0

x0 = x; y0 = y;

elsex1 = x; y1 = y;

c dx dy x y _

1010 250 150

(x1, y1) =

(400, 300)

c1 = (1010)

(x0, y0) =

(150, 150)

c0 = (0000)

ymax=200

ymin=100

Xmin = 100 xmax = 300

14

Cohen-Sutherland: Line Clipping

Intersection algorithm:

if c0 0000 then c = c0;else c = c1;

dx = x1 – x0; dy = y1 – y0if c & 1000 // ymax

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

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;

else // xmin

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

if c = c0

x0 = x; y0 = y;

elsex1 = x; y1 = y;

c dx dy x y _

1010 250 150

(x1, y1) =

(400, 300)

c1 = (1010)

(x0, y0) =

(150, 150)

c0 = (0000)

ymax=200

ymin=100

Xmin = 100 xmax = 300

15

Cohen-Sutherland: Line Clipping

Intersection algorithm:

if c0 0000 then c = c0;else c = c1;

dx = x1 – x0; dy = y1 – y0if c & 1000 // ymax

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

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;

else // xmin

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

if c = c0

x0 = x; y0 = y;

elsex1 = x; y1 = y;

c dx dy x y _

1010 250 150 233 200

(x1, y1) =

(400, 300)

c1 = (1010)

(x0, y0) =

(150, 150)

c0 = (0000)

ymax=200

ymin=100

Xmin = 100 xmax = 300

16

Cohen-Sutherland: Line Clipping

Intersection algorithm:

if c0 0000 then c = c0;else c = c1;

dx = x1 – x0; dy = y1 – y0if c & 1000 // ymax

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

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;

else // xmin

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

if c = c0

x0 = x; y0 = y;

elsex1 = x; y1 = y;

c dx dy x y _

1010 250 150 233 200

(x1, y1) =

(400, 300)

c1 = (1010)

(x0, y0) =

(150, 150)

c0 = (0000)

ymax=200

ymin=100

Xmin = 100 xmax = 300

17

Cohen-Sutherland: Line Clipping

Intersection algorithm:

if c0 0000 then c = c0;else c = c1;

dx = x1 – x0; dy = y1 – y0if c & 1000 // ymax

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

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;

else // xmin

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

if c = c0

x0 = x; y0 = y;

elsex1 = x; y1 = y;

c dx dy x y _

1010 250 150 233 200

(x1, y1) =

(400, 300)

c1 = (1010)

(x0, y0) =

(150, 150)

c0 = (0000)

ymax=200

ymin=100

Xmin = 100 xmax = 300

18

Cohen-Sutherland Line Clip Examples

A

B

E

F

G

H

C

D

I

J

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

G 0000

H 1010

OR 1010

AND 0000

clip

I 0110

J 0010

OR 0110

AND 0010

reject

010001010110

10001001 1010

00010010

0000

if c0 | c1 = 0000

accept (draw)

else if c0 & c1 0000

reject (don’t draw)

else clip and retest

19

Cohen-Sutherland Line Clip Examples

A

B

G

H

C

D

A 0001

A’ 0001

OR 0001

AND 0001

reject

A’

G’

C’

C 0000

C’ 0000

OR 0000

AND 0000

accept

remove

C’D

G 0000

G’ 0000

OR 0000

AND 0000

accept

remove

G’H

010001010110

10001001 1010

00010010

0000

if c0 | c1 = 0000

accept (draw)

else if c0 & c1 0000

reject (don’t draw)

else clip and retest

remove

A’B

20

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.

21

Polygon Clipping

What about polygons?

22

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 n–1):• 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.

23

Polygon Clipping: Example

Clip first to ymin and ymax

ymax

ymin

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v0

Inside region: No

Add p0 to output list

Output vertex list:

p0

Line intersect

boundary: Yes

p0

24

Polygon Clipping: Example

Clip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v1

inside region: yes

add v1 to output list

Output vertex list:

p0

line intersect

boundary: no

, v1

ymax

ymin

p0

25

Polygon Clipping: Example

Clip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v2

inside region: yes

add v2, p1 to output list

Output vertex list:

p0

, v2, p1

line intersect

boundary: yes

p1

ymax

ymin

p0 , v1

26

Polygon Clipping: Example

Clip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v3

inside region: no

Output vertex list:

p0

p0, v1, v2, p1

line intersect

boundary: no

p1

ymax

ymin

27

Polygon Clipping: Example

Clip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v4

inside region: no

add p2 to output list

Output vertex list:

p0

, p2

p1

line intersect

boundary: yes

p2

ymax

ymin

p0, v1, v2, p1

28

Polygon Clipping: Example

Clip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v5

inside region: yes

add v5, p3 to output list

Output vertex list:

p0

p0, v1, v2, p1, p2

p1

p2

line intersect

boundary: yes

p3

ymax

ymin

, v5, p3

29

Polygon Clipping: Example

Clip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v6

inside region: no

Output vertex list:

p0

p0, v1, v2, p1, p2, v5, p3

p1

p2

line intersect

boundary: no

p3

ymax

ymin

30

Polygon Clipping: Example

Clip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v7

inside region: no

Output vertex list:

p0

p0, v1, v2, p1, p2, v5, p3

p1

p2

line intersect

boundary: no

p3

ymax

ymin

31

Polygon Clipping: Example

Clip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v8

inside region: no

add p4 to output list

Output vertex list:

p0

p0, v1, v2, p1, p2, v5, p3

p1

p2

p3

line intersect

boundary: yes

p4

ymax

ymin

, p4

32

Polygon Clipping: Example

Clip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v9

inside region: yes

add v9, p5 to output list

Output vertex list:

p0

p0, v1, v2, p1, p2, v5, p3, p4

p1

p2

p3p4

line intersect

boundary: yes

p5

ymax

ymin

, v9, p5

33

Polygon Clipping: Example

This gives us a new polygon

v1v2

v5

v9

with vertices:

p0

(p0, v1, v2, p1, p2, v5, p3, p4, v9, p5)

p1

p2

p3p4 p5

ymax

ymin

34

Polygon Clipping: Example (cont.)

Now clip to xmin and xmaxxmin xmax

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)

v1v2

v5

v9

p0

p1

p2

p3p4 p5

p6

p7

p8

p9

35

Polygon Clipping: Example (cont.)

Now post-processxmin xmax

v9

v3

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)

p8

p6

v2

p7

p0

p5 p3p4

p9

p1