1 / 36

Computer Graphics

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 area? Draw just the portions of a line (or object) that fall within a given region/window/screen (usually rectangular). Drawing

aran
Download Presentation

Computer Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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: • Check (in inner loop) if each point is inside  Works, but slow • Clip invalid coordinate(s) to boundary  Incorrect results • 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; Clip x Output Input Clip y Clip line to intersection

  4. Line Clipping: Possible Configurations • Both endpoints are inside the region (line AB) • No clipping necessary • One endpoint in, one out (line CD) • Clip at intersection point • Both endpoints outside the region: • No intersection (lines EF, GH) • Line intersects the region (line IJ) • Clip line at both intersection points J D I A F C H B E G

  5. J D I Clip and retest A F C H B Trivially accept E G Trivially reject Line Clipping: Cohen-Sutherland • Basic algorithm: • Accept (and draw) lines that have both endpoints inside the region • Reject (and don’t draw) 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 steps 1 and 2 on the clipped line segments

  6. 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 1001 1000 1010 0001 0000 0010 0101 0100 0110

  7. Cohen-Sutherland: Line Clipping Intersection algorithm: if code0 0000 then code = code0 else code = code1 dx = x1 – x0; dy = y1 – y0 if codeAND 1000 then begin // ymax x = x0 + dx * (ymax – y0) / dy; y = ymax end else if codeAND 0100 then begin // ymin x = x0 + dx * (ymin – y0) / dy; y = ymin end else if codeAND 0010 then begin // xmax y = y0 + dy * (xmax – x0) / dx; x = xmax end else begin // xmin y = y0 + dy * (xmin – x0) / dx; x = xmin end if code = code0 then begin x0 = x; y0 = y; end else begin x1 = x; y1 = y; end (x1, y1) (x, y) ymax dy (x0, y0) dx ymin xmin xmax

  8. (x1, y1) (400, 300) Code (1010) ymax=200 (x0, y0) (150, 150) Code (0000) ymin=100 Xmin = 100 xmax = 300 Cohen-Sutherland: Line Clipping Example Intersection algorithm: if code0 0000 then code = code0 else code = code1 dx = x1 – x0; dy = y1 – y0 if codeAND 1000 then begin // ymax x = x0 + dx * (ymax – y0) / dy; y = ymax end else if codeAND 0100 then begin // ymin x = x0 + dx * (ymin – y0) / dy; y = ymin end else if codeAND 0010 then begin // xmax y = y0 + dy * (xmax – x0) / dx; x = xmax end else begin // xmin y = y0 + dy * (xmin – x0) / dx; x = xmin end if code = code0 then begin x0 = x; y0 = y; end else begin x1 = x; y1 = y; end Code dx dy x y

  9. (x1, y1) (400, 300) Code (1010) ymax=200 (x0, y0) (150, 150) Code (0000) ymin=100 Xmin = 100 xmax = 300 Cohen-Sutherland: Line Clipping Example Intersection algorithm: if code0 0000 then code = code0 else code = code1 dx = x1 – x0; dy = y1 – y0 if codeAND 1000 then begin // ymax x = x0 + dx * (ymax – y0) / dy; y = ymax end else if codeAND 0100 then begin // ymin x = x0 + dx * (ymin – y0) / dy; y = ymin end else if codeAND 0010 then begin // xmax y = y0 + dy * (xmax – x0) / dx; x = xmax end else begin // xmin y = y0 + dy * (xmin – x0) / dx; x = xmin end if code = code0 then begin x0 = x; y0 = y; end else begin x1 = x; y1 = y; end Code dx dy x y

  10. (x1, y1) (400, 300) Code (1010) ymax=200 (x0, y0) (150, 150) Code (0000) ymin=100 Xmin = 100 xmax = 300 Cohen-Sutherland: Line Clipping Example Intersection algorithm: if code0 0000 then code = code0 else code = code1 dx = x1 – x0; dy = y1 – y0 if codeAND 1000 then begin // ymax x = x0 + dx * (ymax – y0) / dy; y = ymax end else if codeAND 0100 then begin // ymin x = x0 + dx * (ymin – y0) / dy; y = ymin end else if codeAND 0010 then begin // xmax y = y0 + dy * (xmax – x0) / dx; x = xmax end else begin // xmin y = y0 + dy * (xmin – x0) / dx; x = xmin end if code = code0 then begin x0 = x; y0 = y; end else begin x1 = x; y1 = y; end Code 1010 dx dy x y

  11. (x1, y1) (400, 300) Code (1010) ymax=200 (x0, y0) (150, 150) Code (0000) ymin=100 Xmin = 100 xmax = 300 Cohen-Sutherland: Line Clipping Example Intersection algorithm: if code0 0000 then code = code0 else code = code1 dx = x1 – x0; dy = y1 – y0 if codeAND 1000 then begin // ymax x = x0 + dx * (ymax – y0) / dy; y = ymax end else if codeAND 0100 then begin // ymin x = x0 + dx * (ymin – y0) / dy; y = ymin end else if codeAND 0010 then begin // xmax y = y0 + dy * (xmax – x0) / dx; x = xmax end else begin // xmin y = y0 + dy * (xmin – x0) / dx; x = xmin end if code = code0 then begin x0 = x; y0 = y; end else begin x1 = x; y1 = y; end Code 1010 dx 250 dy 150 x y

  12. (x1, y1) (400, 300) Code (1010) ymax=200 (x0, y0) (150, 150) Code (0000) ymin=100 Xmin = 100 xmax = 300 Cohen-Sutherland: Line Clipping Example Intersection algorithm: if code0 0000 then code = code0 else code = code1 dx = x1 – x0; dy = y1 – y0 if codeAND 1000 then begin// ymax x = x0 + dx * (ymax – y0) / dy; y = ymax end else if codeAND 0100 then begin // ymin x = x0 + dx * (ymin – y0) / dy; y = ymin end else if codeAND 0010 then begin // xmax y = y0 + dy * (xmax – x0) / dx; x = xmax end else begin // xmin y = y0 + dy * (xmin – x0) / dx; x = xmin end if code = code0 then begin x0 = x; y0 = y; end else begin x1 = x; y1 = y; end Code 1010 dx 250 dy 150 x y

  13. (x1, y1) (400, 300) Code (1010) ymax=200 (x0, y0) (150, 150) Code (0000) ymin=100 Xmin = 100 xmax = 300 Cohen-Sutherland: Line Clipping Example Intersection algorithm: if code0 0000 then code = code0 else code = code1 dx = x1 – x0; dy = y1 – y0 if codeAND 1000 then begin// ymax x = x0 + dx * (ymax – y0) / dy; y = ymax end else if codeAND 0100 then begin // ymin x = x0 + dx * (ymin – y0) / dy; y = ymin end else if codeAND 0010 then begin // xmax y = y0 + dy * (xmax – x0) / dx; x = xmax end else begin // xmin y = y0 + dy * (xmin – x0) / dx; x = xmin end if code = code0 then begin x0 = x; y0 = y; end else begin x1 = x; y1 = y; end Code 1010 dx 250 dy 150 x 233 y 200

  14. (x1, y1) (400, 300) Code (1010) ymax=200 (x0, y0) (150, 150) Code (0000) ymin=100 Xmin = 100 xmax = 300 Cohen-Sutherland: Line Clipping Example Intersection algorithm: if code0 0000 then code = code0 else code = code1 dx = x1 – x0; dy = y1 – y0 if codeAND 1000 then begin// ymax x = x0 + dx * (ymax – y0) / dy; y = ymax end else if codeAND 0100 then begin // ymin x = x0 + dx * (ymin – y0) / dy; y = ymin end else if codeAND 0010 then begin // xmax y = y0 + dy * (xmax – x0) / dx; x = xmax end else begin // xmin y = y0 + dy * (xmin – x0) / dx; x = xmin end if code = code0 then begin x0 = x; y0 = y; end else begin x1 = x; y1 = y; end Code 1010 dx 250 dy 150 x 233 y 200

  15. (x1, y1) (400, 300) Code (1010) ymax=200 (x0, y0) (150, 150) Code (0000) ymin=100 Xmin = 100 xmax = 300 Cohen-Sutherland: Line Clipping Example Intersection algorithm: if code0 0000 then code = code0 else code = code1 dx = x1 – x0; dy = y1 – y0 if codeAND 1000 then begin// ymax x = x0 + dx * (ymax – y0) / dy; y = ymax end else if codeAND 0100 then begin // ymin x = x0 + dx * (ymin – y0) / dy; y = ymin end else if codeAND 0010 then begin // xmax y = y0 + dy * (xmax – x0) / dx; x = xmax end else begin // xmin y = y0 + dy * (xmin – x0) / dx; x = xmin end if code = code0 then begin x0 = x; y0 = y; end else begin x1 = x; y1 = y; end Code 1010 dx 250 dy 150 x 233 y 200

  16. Cohen-Sutherland: Line Clipping Summary • Choose an endpoint outside the clipping region • Using a consistent ordering (top to bottom, left to right) find a clipping border the line intersects • Discard the portion of the line from the endpoint to the intersection point • Set the new line to have as endpoints the new intersection point and the other original endpoint • 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 H 1000 1001 1010 F D G 0001 0000 C 0010 J A E 0101 I 0100 0110 B 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

  18. Cohen-Sutherland Line Clip Examples H 1000 1001 1010 G’ D G 0001 0000 C’ C 0010 A A’ 0101 0100 0110 B A 0001 A’ 0001 remove 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

  19. Cohen-Sutherland Line Clip Examples 1000 1001 1010 0001 0000 0010 A’ B’ 0101 0100 0110 B A’ 0001 B’ 0100 remove B’ 0100 B 0100 OR 0100 AND 0100 reject

  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 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 oflistout • If the line between viand vi+1 intersects specified boundaries  Add intersection point(s) to end oflistout • 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

  22. p0 line intersect boundary: yes Polygon Clipping: Example v6 v7 Clip first to ymin and ymax v0 v8 vertex:v0 ymin inside region: no v9 v5 v1 v2 add p0 to output list ymax v3 v4 Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) Output vertex list: (p0)

  23. p0 line intersect boundary: no Polygon Clipping: Example v6 v7 Clip first to ymin and ymax v0 v8 vertex:v1 ymin inside region: yes v9 v5 v1 v2 add v1 to output list ymax v3 v4 Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) Output vertex list: (p0, v1)

  24. p0 line intersect boundary: yes p1 Polygon Clipping: Example v6 v7 Clip first to ymin and ymax v0 v8 vertex:v2 ymin inside region: yes v9 v5 v1 v2 add v2, p1 to output list ymax v3 v4 Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) Output vertex list: (p0, v1, v2, p1)

  25. p0 line intersect boundary: no p1 Polygon Clipping: Example v6 v7 Clip first to ymin and ymax v0 v8 vertex:v3 ymin inside region: no v9 v5 v1 v2 ymax v3 v4 Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) Output vertex list: (p0, v1, v2, p1)

  26. p0 line intersect boundary: yes p1 p2 Polygon Clipping: Example v6 v7 Clip first to ymin and ymax v0 v8 vertex:v4 ymin inside region: no v9 v5 v1 v2 add p2 to output list ymax v3 v4 Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) Output vertex list: (p0, v1, v2, p1, p2)

  27. p3 p0 line intersect boundary: yes p1 p2 Polygon Clipping: Example v6 v7 Clip first to ymin and ymax v0 v8 vertex:v5 ymin inside region: yes v9 v5 v1 v2 add v5, p3 to output list ymax v3 v4 Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) Output vertex list: (p0, v1, v2, p1, p2, v5, p3)

  28. p3 p0 line intersect boundary: no p1 p2 Polygon Clipping: Example v6 v7 Clip first to ymin and ymax v0 v8 vertex:v6 ymin inside region: no v9 v5 v1 v2 ymax v3 v4 Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) Output vertex list: (p0, v1, v2, p1, p2, v5, p3)

  29. p3 p0 line intersect boundary: no p1 p2 Polygon Clipping: Example v6 v7 Clip first to ymin and ymax v0 v8 vertex:v7 ymin inside region: no v9 v5 v1 v2 ymax v3 v4 Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) Output vertex list: (p0, v1, v2, p1, p2, v5, p3)

  30. p4 p3 p0 line intersect boundary: yes p1 p2 Polygon Clipping: Example v6 v7 Clip first to ymin and ymax v0 v8 vertex:v8 ymin inside region: no v9 v5 v1 v2 add p4 to output list ymax v3 v4 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. p4 p3 p5 p0 line intersect boundary: yes p1 p2 Polygon Clipping: Example v6 v7 Clip first to ymin and ymax v0 v8 vertex:v9 ymin inside region: yes v9 v5 v1 v2 add v9, p5 to output list ymax v3 v4 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. p4 p3 p5 p0 p1 p2 Polygon Clipping: Example This gives us a new polygon ymin v9 v5 v1 v2 ymax with vertices: (p0, v1, v2, p1, p2, v5, p3, p4, v9, p5)

  33. p4 p3 p5 p0 p1 p2 Polygon Clipping: Example (cont.) Now clip to xmin and xmax xmin xmax p9 v9 p6 v5 p7 v1 v2 p8 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. Polygon Clipping: Example (cont.) Now post-process xmin xmax p4 p3 p5 p0 p9 v9 p6 p7 v2 p1 v3 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. Polygon Orientation C D R D C L B E R L A A E B positive Negative

  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

More Related