Download presentation
Presentation is loading. Please wait.
Published byGabriel Hamilton Modified over 9 years ago
1
1 Computer Graphics Clipping Fall FCC 2006
2
Line Clipping What 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) Drawing Area
3
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 Output Clip x Clip y 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 (and draw) lines that have both endpoints inside the region F E Trivially reject A B Trivially accept H C D I J G Clip and retest 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 x min or y min or greater than x max or y max
6
Cohen-Sutherland: Accept/Reject Tests Assign 4-bit code to each endpoint corresponding to its position relative to region: Firstbit (1000) : if y > y max Secondbit (0100) : if y < y min Thirdbit (0010) : if x > x max Fourthbit (0001) : if x < x min Test: if code 0 OR code 1 = 0000 accept (draw) else if code 0 AND code 1 0000 reject (don’t draw) else clip and retest 010001010110 100010011010 000100100000
7
Cohen-Sutherland: Line Clipping Intersection algorithm: if code 0 0000 then code = code 0 else code = code 1 dx = x 1 – x 0 ; dy = y 1 – y 0 if code AND 1000 then begin // y max x = x 0 + dx * (y max – y 0 ) / dy; y = y max end else if code AND 0100 then begin // y min x = x 0 + dx * (y min – y 0 ) / dy; y = y min end else if code AND 0010 then begin // x max y = y 0 + dy * (x max – x 0 ) / dx; x = x max end else begin // x min y = y 0 + dy * (x min – x 0 ) / dx; x = x min end if code = code 0 thenbegin x 0 = x; y 0 = y; end elsebegin x 1 = x; y 1 = y; end (x 1, y 1 ) (x 0, y 0 ) y max y min dx dy (x, y) x min x max
8
Cohen-Sutherland: Line Clipping Example Intersection algorithm: if code 0 0000 then code = code 0 else code = code 1 dx = x 1 – x 0 ; dy = y 1 – y 0 if code AND 1000 then begin // y max x = x 0 + dx * (y max – y 0 ) / dy; y = y max end else if code AND 0100 then begin // y min x = x 0 + dx * (y min – y 0 ) / dy; y = y min end else if code AND 0010 then begin // x max y = y 0 + dy * (x max – x 0 ) / dx; x = x max end else begin // x min y = y 0 + dy * (x min – x 0 ) / dx; x = x min end if code = code 0 thenbegin x 0 = x; y 0 = y; end elsebegin x 1 = x; y 1 = y; end Codedxyxdy (x 1, y 1 ) (400, 300) Code (1010) (x 0, y 0 ) (150, 150) Code (0000) y max =200 y min =100 X min = 100x max = 300
9
Cohen-Sutherland: Line Clipping Example Intersection algorithm: if code 0 0000 then code = code 0 else code = code 1 dx = x 1 – x 0 ; dy = y 1 – y 0 if code AND 1000 then begin // y max x = x 0 + dx * (y max – y 0 ) / dy; y = y max end else if code AND 0100 then begin // y min x = x 0 + dx * (y min – y 0 ) / dy; y = y min end else if code AND 0010 then begin // x max y = y 0 + dy * (x max – x 0 ) / dx; x = x max end else begin // x min y = y 0 + dy * (x min – x 0 ) / dx; x = x min end if code = code 0 thenbegin x 0 = x; y 0 = y; end elsebegin x 1 = x; y 1 = y; end Codedxyxdy (x 1, y 1 ) (400, 300) Code (1010) (x 0, y 0 ) (150, 150) Code (0000) y max =200 y min =100 X min = 100x max = 300
10
Cohen-Sutherland: Line Clipping Example Intersection algorithm: if code 0 0000 then code = code 0 else code = code 1 dx = x 1 – x 0 ; dy = y 1 – y 0 if code AND 1000 then begin // y max x = x 0 + dx * (y max – y 0 ) / dy; y = y max end else if code AND 0100 then begin // y min x = x 0 + dx * (y min – y 0 ) / dy; y = y min end else if code AND 0010 then begin // x max y = y 0 + dy * (x max – x 0 ) / dx; x = x max end else begin // x min y = y 0 + dy * (x min – x 0 ) / dx; x = x min end if code = code 0 thenbegin x 0 = x; y 0 = y; end elsebegin x 1 = x; y 1 = y; end Code 1010 dxyxdy (x 1, y 1 ) (400, 300) Code (1010) (x 0, y 0 ) (150, 150) Code (0000) y max =200 y min =100 X min = 100x max = 300
11
Cohen-Sutherland: Line Clipping Example Intersection algorithm: if code 0 0000 then code = code 0 else code = code 1 dx = x 1 – x 0 ; dy = y 1 – y 0 if code AND 1000 then begin // y max x = x 0 + dx * (y max – y 0 ) / dy; y = y max end else if code AND 0100 then begin // y min x = x 0 + dx * (y min – y 0 ) / dy; y = y min end else if code AND 0010 then begin // x max y = y 0 + dy * (x max – x 0 ) / dx; x = x max end else begin // x min y = y 0 + dy * (x min – x 0 ) / dx; x = x min end if code = code 0 thenbegin x 0 = x; y 0 = y; end elsebegin x 1 = x; y 1 = y; end (x 1, y 1 ) (400, 300) Code (1010) (x 0, y 0 ) (150, 150) Code (0000) y max =200 y min =100 X min = 100x max = 300 Code 1010 dx 250 yxdy 150
12
Cohen-Sutherland: Line Clipping Example Intersection algorithm: if code 0 0000 then code = code 0 else code = code 1 dx = x 1 – x 0 ; dy = y 1 – y 0 if code AND 1000 then begin // y max x = x 0 + dx * (y max – y 0 ) / dy; y = y max end else if code AND 0100 then begin // y min x = x 0 + dx * (y min – y 0 ) / dy; y = y min end else if code AND 0010 then begin // x max y = y 0 + dy * (x max – x 0 ) / dx; x = x max end else begin // x min y = y 0 + dy * (x min – x 0 ) / dx; x = x min end if code = code 0 thenbegin x 0 = x; y 0 = y; end elsebegin x 1 = x; y 1 = y; end (x 1, y 1 ) (400, 300) Code (1010) (x 0, y 0 ) (150, 150) Code (0000) y max =200 y min =100 X min = 100x max = 300 Code 1010 dx 250 yxdy 150
13
Cohen-Sutherland: Line Clipping Example Intersection algorithm: if code 0 0000 then code = code 0 else code = code 1 dx = x 1 – x 0 ; dy = y 1 – y 0 if code AND 1000 then begin // y max x = x 0 + dx * (y max – y 0 ) / dy; y = y max end else if code AND 0100 then begin // y min x = x 0 + dx * (y min – y 0 ) / dy; y = y min end else if code AND 0010 then begin // x max y = y 0 + dy * (x max – x 0 ) / dx; x = x max end else begin // x min y = y 0 + dy * (x min – x 0 ) / dx; x = x min end if code = code 0 thenbegin x 0 = x; y 0 = y; end elsebegin x 1 = x; y 1 = y; end (x 1, y 1 ) (400, 300) Code (1010) (x 0, y 0 ) (150, 150) Code (0000) y max =200 y min =100 X min = 100x max = 300 Code 1010 dx 250 y 200 x 233 dy 150
14
Cohen-Sutherland: Line Clipping Example Intersection algorithm: if code 0 0000 then code = code 0 else code = code 1 dx = x 1 – x 0 ; dy = y 1 – y 0 if code AND 1000 then begin // y max x = x 0 + dx * (y max – y 0 ) / dy; y = y max end else if code AND 0100 then begin // y min x = x 0 + dx * (y min – y 0 ) / dy; y = y min end else if code AND 0010 then begin // x max y = y 0 + dy * (x max – x 0 ) / dx; x = x max end else begin // x min y = y 0 + dy * (x min – x 0 ) / dx; x = x min end if code = code 0 thenbegin x 0 = x; y 0 = y; end elsebegin x 1 = x; y 1 = y; end (x 1, y 1 ) (400, 300) Code (1010) (x 0, y 0 ) (150, 150) Code (0000) y max =200 y min =100 X min = 100x max = 300 Code 1010 dx 250 y 200 x 233 dy 150
15
Cohen-Sutherland: Line Clipping Example Intersection algorithm: if code 0 0000 then code = code 0 else code = code 1 dx = x 1 – x 0 ; dy = y 1 – y 0 if code AND 1000 then begin // y max x = x 0 + dx * (y max – y 0 ) / dy; y = y max end else if code AND 0100 then begin // y min x = x 0 + dx * (y min – y 0 ) / dy; y = y min end else if code AND 0010 then begin // x max y = y 0 + dy * (x max – x 0 ) / dx; x = x max end else begin // x min y = y 0 + dy * (x min – x 0 ) / dx; x = x min end if code = code 0 thenbegin x 0 = x; y 0 = y; end elsebegin x 1 = x; y 1 = y; end (x 1, y 1 ) (400, 300) Code (1010) (x 0, y 0 ) (150, 150) Code (0000) y max =200 y min =100 X min = 100x max = 300 Code 1010 dx 250 y 200 x 233 dy 150
16
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)
17
Cohen-Sutherland Line Clip Examples A B E F G H C D I J A 0001 B 0100 OR 0101 AND 0000 subdivide C 0000 D 0010 OR 0010 AND 0000 subdivide E 0000 F 0000 OR 0000 AND 0000 accept G 0000 H 1010 OR 1010 AND 0000 subdivide I 0110 J 0010 OR 0110 AND 0010 reject 0100 0101 0110 1000 10011010 0001 0010 0000
18
Cohen-Sutherland Line Clip Examples A B G H C D A 0001 A’ 0001 remove A’ G’ C’ A’ 0001 B 0100 OR 0101 AND 0000 subdivide C 0000 C’ 0000 OR 0000 AND 0000 accept C’ 0000 D 1010 remove G 0000 G’ 0000 OR 0000 AND 0000 accept G’ 0000 H 1010 remove 0100 0101 0110 1000 10011010 0001 0010 0000
19
Cohen-Sutherland Line Clip Examples B’ B A’ 0001 B’ 0100 remove A’ B’ 0100 B 0100 OR 0100 AND 0100 reject 0100 0101 0110 1000 10011010 0001 0010 0000
20
Polygon Clipping What about polygons? For concave polygons, the intersection with the clipping region may be complex
21
Polygon Clipping: Algorithm Clip polygon to y min and y max : Create empty output vertex list (list out = empty) Process input list (list in = (v 0, v 1, …, v n ) where v 0 = v n ) in order For each input vertex (v i where 0 i n–1) : - If v i is inside region Add v i to end of list out - If the line between v i and v i+1 intersects specified boundaries Add intersection point(s) to end of list out Repeat: clipping to x min and x max Post-process: Find “degenerate” sections where both sides of polygon has collapsed to region boundary Remove those sections Create new polygon
22
Polygon Clipping: Example Clip first to y min and y max y min y max v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 v9v9 Input vertex list: (v 0, v 1, v 2, v 3, v 4, v 5, v 6, v 7, v 8, v 9 ) vertex: v 0 inside region: no add p 0 to output list Output vertex list: p0p0 line intersect boundary: yes (p0)(p0)
23
Polygon Clipping: Example Clip first to y min and y max y min y max v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 v9v9 Input vertex list: (v 0, v 1, v 2, v 3, v 4, v 5, v 6, v 7, v 8, v 9 ) vertex: v 1 inside region: yes add v 1 to output list Output vertex list: p0p0 line intersect boundary: no (p 0, v 1 )
24
Polygon Clipping: Example Clip first to y min and y max y min y max v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 v9v9 Input vertex list: (v 0, v 1, v 2, v 3, v 4, v 5, v 6, v 7, v 8, v 9 ) vertex: v 2 inside region: yes add v 2, p 1 to output list Output vertex list: p0p0 (p 0, v 1, v 2, p 1 ) line intersect boundary: yes p1p1
25
Polygon Clipping: Example Clip first to y min and y max y min y max v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 v9v9 Input vertex list: (v 0, v 1, v 2, v 3, v 4, v 5, v 6, v 7, v 8, v 9 ) vertex: v 3 inside region: no Output vertex list: p0p0 (p 0, v 1, v 2, p 1 ) line intersect boundary: no p1p1
26
Polygon Clipping: Example Clip first to y min and y max y min y max v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 v9v9 Input vertex list: (v 0, v 1, v 2, v 3, v 4, v 5, v 6, v 7, v 8, v 9 ) vertex: v 4 inside region: no add p 2 to output list Output vertex list: p0p0 (p 0, v 1, v 2, p 1, p 2 ) p1p1 line intersect boundary: yes p2p2
27
Polygon Clipping: Example Clip first to y min and y max y min y max v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 v9v9 Input vertex list: (v 0, v 1, v 2, v 3, v 4, v 5, v 6, v 7, v 8, v 9 ) vertex: v 5 inside region: yes add v 5, p 3 to output list Output vertex list: p0p0 (p 0, v 1, v 2, p 1, p 2, v 5, p 3 ) p1p1 p2p2 line intersect boundary: yes p3p3
28
Polygon Clipping: Example Clip first to y min and y max y min y max v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 v9v9 Input vertex list: (v 0, v 1, v 2, v 3, v 4, v 5, v 6, v 7, v 8, v 9 ) vertex: v 6 inside region: no Output vertex list: p0p0 (p 0, v 1, v 2, p 1, p 2, v 5, p 3 ) p1p1 p2p2 line intersect boundary: no p3p3
29
Polygon Clipping: Example Clip first to y min and y max y min y max v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 v9v9 Input vertex list: (v 0, v 1, v 2, v 3, v 4, v 5, v 6, v 7, v 8, v 9 ) vertex: v 7 inside region: no Output vertex list: p0p0 (p 0, v 1, v 2, p 1, p 2, v 5, p 3 ) p1p1 p2p2 line intersect boundary: no p3p3
30
Polygon Clipping: Example Clip first to y min and y max y min y max v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 v9v9 Input vertex list: (v 0, v 1, v 2, v 3, v 4, v 5, v 6, v 7, v 8, v 9 ) vertex: v 8 inside region: no add p 4 to output list Output vertex list: p0p0 (p 0, v 1, v 2, p 1, p 2, v 5, p 3, p 4 ) p1p1 p2p2 p3p3 line intersect boundary: yes p4p4
31
Polygon Clipping: Example Clip first to y min and y max y min y max v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 v9v9 Input vertex list: (v 0, v 1, v 2, v 3, v 4, v 5, v 6, v 7, v 8, v 9 ) vertex: v 9 inside region: yes add v 9, p 5 to output list Output vertex list: p0p0 (p 0, v 1, v 2, p 1, p 2, v 5, p 3, p 4, v 9, p 5 ) p1p1 p2p2 p3p3 p4p4 line intersect boundary: yes p5p5
32
Polygon Clipping: Example This gives us a new polygon y min y max v1v1 v2v2 v5v5 v9v9 with vertices: p0p0 (p 0, v 1, v 2, p 1, p 2, v 5, p 3, p 4, v 9, p 5 ) p1p1 p2p2 p3p3 p4p4 p5p5
33
Polygon Clipping: Example (cont.) Now clip to x min and x max x min x max Input vertex list: = (p 0, v 1, v 2, p 1, p 2, v 5, p 3, p 4, v 9, p 5 ) Output vertex list: (p 0, p 6, p 7, v 2, p 1, p 8, p 9, p 3, p 4, v 9, p 5 ) v1v1 v2v2 v5v5 v9v9 p0p0 p1p1 p2p2 p3p3 p4p4 p5p5 p6p6 p7p7 p8p8 p9p9
34
Polygon Clipping: Example (cont.) Now post-process x min x max v9v9 v3v3 Output vertex list: (p 0, p 6, p 7, v 2, p 1, p 8, p 9, p 3, p 4, v 9, p 5 ) Post-process: (p 0, p 6, p 9, p 3,) and (p 7, v 2, p 1, p 8 ) and (v 4, v 9, p 5 ) p8p8 p6p6 v2v2 p7p7 p0p0 p5p5 p3p3 p4p4 p9p9 p1p1
35
Polygon Orientation positive Negative A B C D E L R A E D C B L R
36
Left o Right Let 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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.