36
1 Computer Graphics Clipping Fall FCC 2006

1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Embed Size (px)

Citation preview

Page 1: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

1

Computer Graphics

Clipping

Fall FCC 2006

Page 2: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

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

Draw just the portions of a line (or object) that fall within a given region/window/screen (usually rectangular)

DrawingArea

Page 3: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Line Clipping• Strategies for clipping:

a) Check (in inner loop) if each point is inside Works, but slow

b) Clip invalid coordinate(s) to boundary Incorrect results

c) Find intersection of line with boundary Correct

if (x >= xmin && x <= xmax && y >= ymin && y <= ymax) drawPoint(x,y,c);

if (x < xmin) x = xmin;else if (x > xmax) x = xmax;if (y < ymin) y = ymin;else if (y > ymax) y = ymax; Input

OutputClip x

Clip y

Clip line tointersection

Page 4: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Line Clipping: Possible Configurations

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

2. One endpoint in, oneout (line CD) Clip at intersection point

3. Both endpoints outsidethe 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

Page 5: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Line Clipping: Cohen-Sutherland

• Basic algorithm: Accept (and draw) lines that

have both endpoints inside the region

F

E

Trivially reject

A

BTrivially accept

H

C

D

I

J

G

Clip andretest

Clip the remaining lines at a region boundary and repeat steps 1 and 2 on the clipped line segments

Reject (and don’t draw) lines that have both endpoints less than xmin or ymin or greater than xmax or ymax

Page 6: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Cohen-Sutherland: Accept/Reject Tests

• Assign 4-bit code to each endpoint corresponding to its position relative to region: First bit (1000): if y > ymax

Second bit (0100): if y < ymin

Third bit (0010): if x > xmax

Fourth bit (0001): if x < xmin

• Test:if code0 OR code1 = 0000

accept (draw)else if code0 AND code1 0000

reject (don’t draw)else clip and retest

01000101 0110

10001001 1010

0001 00100000

Page 7: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Cohen-Sutherland: Line Clipping

Intersection algorithm:if code0 0000 then code = code0

else code = code1

dx = x1 – x0; dy = y1 – y0

if code AND 1000 then begin // ymax

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

endelse if code AND 0100 then begin // ymin

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

endelse if code AND 0010 then begin // xmax

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

endelse begin // xmin

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

end

if code = code0 then begin x0 = x; y0 = y; endelse begin x1 = x; y1 = y; end

(x1, y1)

(x0, y0)

ymax

ymin

dx

dy

(x, y)

xmin xmax

Page 8: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Cohen-Sutherland: Line Clipping Example

Intersection algorithm:if code0 0000 then code = code0

else code = code1

dx = x1 – x0; dy = y1 – y0

if code AND 1000 then begin // ymax

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

endelse if code AND 0100 then begin // ymin

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

endelse if code AND 0010 then begin // xmax

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

endelse begin // xmin

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

end

if code = code0 then begin x0 = x; y0 = y; endelse begin x1 = x; y1 = y; end

Code dx yxdy

(x1, y1)(400, 300)Code (1010)

(x0, y0)(150, 150)Code (0000)

ymax=200

ymin=100

Xmin = 100 xmax = 300

Page 9: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Cohen-Sutherland: Line Clipping Example

Intersection algorithm:if code0 0000 then code = code0

else code = code1

dx = x1 – x0; dy = y1 – y0

if code AND 1000 then begin // ymax

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

endelse if code AND 0100 then begin // ymin

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

endelse if code AND 0010 then begin // xmax

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

endelse begin // xmin

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

end

if code = code0 then begin x0 = x; y0 = y; endelse begin x1 = x; y1 = y; end

Code dx yxdy

(x1, y1)(400, 300)Code (1010)

(x0, y0)(150, 150)Code (0000)

ymax=200

ymin=100

Xmin = 100 xmax = 300

Page 10: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Cohen-Sutherland: Line Clipping Example

Intersection algorithm:if code0 0000 then code = code0

else code = code1

dx = x1 – x0; dy = y1 – y0

if code AND 1000 then begin // ymax

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

endelse if code AND 0100 then begin // ymin

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

endelse if code AND 0010 then begin // xmax

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

endelse begin // xmin

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

end

if code = code0 then begin x0 = x; y0 = y; endelse begin x1 = x; y1 = y; end

Code1010

dx yxdy

(x1, y1)(400, 300)Code (1010)

(x0, y0)(150, 150)Code (0000)

ymax=200

ymin=100

Xmin = 100 xmax = 300

Page 11: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Cohen-Sutherland: Line Clipping Example

Intersection algorithm:if code0 0000 then code = code0

else code = code1

dx = x1 – x0; dy = y1 – y0

if code AND 1000 then begin // ymax

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

endelse if code AND 0100 then begin // ymin

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

endelse if code AND 0010 then begin // xmax

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

endelse begin // xmin

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

end

if code = code0 then begin x0 = x; y0 = y; endelse begin x1 = x; y1 = y; end

(x1, y1)(400, 300)Code (1010)

(x0, y0)(150, 150)Code (0000)

ymax=200

ymin=100

Xmin = 100 xmax = 300

Code1010

dx250

yxdy150

Page 12: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Cohen-Sutherland: Line Clipping Example

Intersection algorithm:if code0 0000 then code = code0

else code = code1

dx = x1 – x0; dy = y1 – y0

if code AND 1000 then begin // ymax

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

endelse if code AND 0100 then begin // ymin

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

endelse if code AND 0010 then begin // xmax

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

endelse begin // xmin

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

end

if code = code0 then begin x0 = x; y0 = y; endelse begin x1 = x; y1 = y; end

(x1, y1)(400, 300)Code (1010)

(x0, y0)(150, 150)Code (0000)

ymax=200

ymin=100

Xmin = 100 xmax = 300

Code1010

dx250

yxdy150

Page 13: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Cohen-Sutherland: Line Clipping Example

Intersection algorithm:if code0 0000 then code = code0

else code = code1

dx = x1 – x0; dy = y1 – y0

if code AND 1000 then begin // ymax

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

endelse if code AND 0100 then begin // ymin

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

endelse if code AND 0010 then begin // xmax

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

endelse begin // xmin

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

end

if code = code0 then begin x0 = x; y0 = y; endelse begin x1 = x; y1 = y; end

(x1, y1)(400, 300)Code (1010)

(x0, y0)(150, 150)Code (0000)

ymax=200

ymin=100

Xmin = 100 xmax = 300

Code1010

dx250

y200

x233

dy150

Page 14: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Cohen-Sutherland: Line Clipping Example

Intersection algorithm:if code0 0000 then code = code0

else code = code1

dx = x1 – x0; dy = y1 – y0

if code AND 1000 then begin // ymax

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

endelse if code AND 0100 then begin // ymin

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

endelse if code AND 0010 then begin // xmax

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

endelse begin // xmin

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

end

if code = code0 then begin x0 = x; y0 = y; endelse begin x1 = x; y1 = y; end

(x1, y1)(400, 300)Code (1010)

(x0, y0)(150, 150)Code (0000)

ymax=200

ymin=100

Xmin = 100 xmax = 300

Code1010

dx250

y200

x233

dy150

Page 15: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Cohen-Sutherland: Line Clipping Example

Intersection algorithm:if code0 0000 then code = code0

else code = code1

dx = x1 – x0; dy = y1 – y0

if code AND 1000 then begin // ymax

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

endelse if code AND 0100 then begin // ymin

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

endelse if code AND 0010 then begin // xmax

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

endelse begin // xmin

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

end

if code = code0 then begin x0 = x; y0 = y; endelse begin x1 = x; y1 = y; end

(x1, y1)(400, 300)Code (1010)

(x0, y0)(150, 150)Code (0000)

ymax=200

ymin=100

Xmin = 100 xmax = 300

Code1010

dx250

y200

x233

dy150

Page 16: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Cohen-Sutherland: Line Clipping Summary

1. Choose an endpoint outside the clipping region

2. Using a consistent ordering (top to bottom, left to right) find a clipping border the line intersects

3. Discard the portion of the line from the endpoint to the intersection point

4. Set the new line to have as endpoints the new intersection point and the other original endpoint

5. You may need to run this several times on a single line (e.g., a line that crosses multiple clip boundaries)

Page 17: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Cohen-Sutherland Line Clip Examples

A

B

E

F

G

H

C

D

I

J

A 0001B 0100OR 0101AND 0000subdivide

C 0000D 0010OR 0010AND 0000subdivide

E 0000F 0000OR 0000AND 0000accept

G 0000H 1010OR 1010AND 0000subdivide

I 0110J 0010OR 0110AND 0010reject

010001010110

10001001 1010

0001 00100000

Page 18: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Cohen-Sutherland Line Clip Examples

A

B

G

H

C

D

A 0001A’ 0001

remove

A’

G’

C’

A’ 0001B 0100OR 0101AND 0000subdivide

C 0000C’ 0000OR 0000AND 0000accept

C’ 0000D 1010

remove

G 0000G’ 0000OR 0000AND 0000accept

G’ 0000H 1010

remove

010001010110

10001001 1010

0001 00100000

Page 19: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Cohen-Sutherland Line Clip Examples

B’

B

A’ 0001B’ 0100

remove

A’

B’ 0100B 0100OR 0100AND 0100reject

010001010110

10001001 1010

0001 00100000

Page 20: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Polygon ClippingWhat about polygons?

For concave polygons, the intersectionwith the clipping region may be complex

Page 21: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Polygon Clipping: Algorithm

• Clip polygon to ymin and ymax: Create empty output vertex list (listout = empty)

Process input list (listin = (v0, v1, …, vn) where v0 = vn) in order

For each input vertex (vi where 0 i n–1):

- If vi is inside region Add vi to end of listout

- If the line between vi and vi+1 intersects specified boundaries Add intersection point(s) to end of listout

• Repeat: clipping to xmin and xmax

• Post-process: Find “degenerate” sections where both sides of polygon has

collapsed to region boundary Remove those sections Create new polygon

Page 22: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Polygon Clipping: Example

Clip first to ymin and ymax

ymin

ymax

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)

Page 23: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Polygon Clipping: Example

Clip first to ymin and ymax

ymin

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

(p0, v1)

Page 24: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Polygon Clipping: Example

Clip first to ymin and ymax

ymin

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

(p0, v1, v2, p1)

line intersect boundary: yes

p1

Page 25: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Polygon Clipping: Example

Clip first to ymin and ymax

ymin

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

Page 26: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Polygon Clipping: Example

Clip first to ymin and ymax

ymin

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

(p0, v1, v2, p1, p2)

p1

line intersect boundary: yes

p2

Page 27: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Polygon Clipping: Example

Clip first to ymin and ymax

ymin

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, v5, p3)

p1

p2

line intersect boundary: yes

p3

Page 28: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Polygon Clipping: Example

Clip first to ymin and ymax

ymin

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

Page 29: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Polygon Clipping: Example

Clip first to ymin and ymax

ymin

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

Page 30: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Polygon Clipping: Example

Clip first to ymin and ymax

ymin

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, p4)

p1

p2

p3

line intersect boundary: yes

p4

Page 31: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Polygon Clipping: Example

Clip first to ymin and ymax

ymin

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, v9, p5)

p1

p2

p3p4

line intersect boundary: yes

p5

Page 32: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Polygon Clipping: Example

This gives us a new polygon

ymin

ymax

v1v2

v5

v9

with vertices:

p0

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

p1

p2

p3p4 p5

Page 33: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

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

Page 34: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

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

Page 35: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Polygon Orientation

positive Negative

A

B

C

D

E

L

R

A

E

D

C

B

LR

Page 36: 1 Computer Graphics Clipping Fall FCC 2006. Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing

Left o RightLet A(x1,y1) and B(x2,y2) end points of a line.

A point P(x,y) will be to the left of the line segment if the expresion

C= (x2-x1)*(y-y1)-(y2-y1)*(x-x1)

is positive. We say the point is to the right if C is negative.

If P is to the right, it is outside the Polygon

If P is to the left, it is inside the Polygon