1 / 15

CS430 Computer Graphics

CS430 Computer Graphics. Cohen-Sutherland Line Clipping Algorithm. Topics. Clipping Cohen-Sutherland Line Clipping Algorithm. Clipping. Why clipping? Not everything defined in the world coordinates is inside the world window Where does clipping take place? OpenGL does it for you

chapa
Download Presentation

CS430 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. CS430 Computer Graphics Cohen-Sutherland Line Clipping Algorithm Chi-Cheng Lin, Winona State University

  2. Topics • Clipping • Cohen-Sutherland Line Clipping Algorithm

  3. Clipping • Why clipping? • Not everything defined in the world coordinates is inside the world window • Where does clipping take place? • OpenGL does it for you • BUT, as a CS major, you should know how it is done. Viewport Transformation … Model Clipping

  4. Line Clipping • int clipSegment(p1, p2, window) • Input parameters: p1, p2, window p1, p2: 2D endpoints that define a line window: aligned rectangle • Returned value: 1, if part of the line is inside the window 0, otherwise • Output parameters: p1, p2 p1 and/or p2’s value might be changed so that both p1 and p2 are inside the window

  5. o P4 o P4 o P1 o P1 o P3 o P3 o P2 o P2 Line Clipping • Example • Line RetVal Output AB BC CD DE EA

  6. Cohen-Sutherland Line Clipping Algorithm • Trivial accept and trivial reject • If both endpoints within window  trivial accept • If both endpoints outside of same boundary of window  trivial reject • Otherwise • Clip against each edge in turn Throw away “clipped off” part of line each time • How can we do it efficiently (elegantly)?

  7. Cohen-Sutherland Line Clipping Algorithm • Examples: • trivial accept? • trivial reject? L4 window L2 L3 L1 L5 L6

  8. Cohen-Sutherland Line Clipping Algorithm • Use “region outcode”

  9. Cohen-Sutherland Line Clipping Algorithm • outcode[1]  (x < Window.left) outcode[2]  (y > Window.top) outcode[3]  (x > Window.right) outcode[4]  (y < Window.bottom)

  10. Cohen-Sutherland Line Clipping Algorithm • Both outcodes are FFFF • Trivial accept • Logical AND of two outcodes  FFFF • Trivial reject • Logical AND of two outcodes = FFFF • Can’t tell • Clip against each edge in turn Throw away “clipped off” part of line each time

  11. Cohen-Sutherland Line Clipping Algorithm • Examples: • outcodes? • trivial accept? • trivial reject? L4 window L2 L3 L1 L5 L6

  12. Cohen-Sutherland Line Clipping Algorithm int clipSegment(Point2& p1, Point2& p2, RealRect W) do if(trivial accept) return 1; else if(trivial reject) return 0; else if(p1 is inside) swap(p1, p2) if(p1 is to the left) chop against the left else if(p1 is to the right) chop against the right else if(p1 is below) chop against the bottom else if(p1 is above) chop against the top while(1);

  13. Cohen-Sutherland Line Clipping Algorithm • A segment that requires 4 clips

  14. Cohen-Sutherland Line Clipping Algorithm • How do we chop against each boundary?  Given P1 (outside) and P2, (A.x,A.y)=?

  15. Cohen-Sutherland Line Clipping Algorithm • Let dx = p1.x - p2.x dy = p1.y - p2.y A.x = w.r d = p1.y - A.y e = p1.x - w.r d/dy = e/dx  p1.y - A.y = (dy/dx)(p1.x - w.r)  A.y = p1.y - (dy/dx)(p1.x - w.r) = p1.y + (dy/dx)(w.r - p1.x) As A is the new P1  p1.y += (dy/dx)(w.r - p1.x) p1.x = w.r Q: Will we have divided-by-zero problem?

More Related