150 likes | 329 Views
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
E N D
CS430 Computer Graphics Cohen-Sutherland Line Clipping Algorithm Chi-Cheng Lin, Winona State University
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 • BUT, as a CS major, you should know how it is done. Viewport Transformation … Model Clipping
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
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
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)?
Cohen-Sutherland Line Clipping Algorithm • Examples: • trivial accept? • trivial reject? L4 window L2 L3 L1 L5 L6
Cohen-Sutherland Line Clipping Algorithm • Use “region outcode”
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)
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
Cohen-Sutherland Line Clipping Algorithm • Examples: • outcodes? • trivial accept? • trivial reject? L4 window L2 L3 L1 L5 L6
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);
Cohen-Sutherland Line Clipping Algorithm • A segment that requires 4 clips
Cohen-Sutherland Line Clipping Algorithm • How do we chop against each boundary? Given P1 (outside) and P2, (A.x,A.y)=?
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?