Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chi-Cheng Lin, Winona State University CS430 Computer Graphics Cohen-Sutherland Line Clipping Algorithm.

Similar presentations


Presentation on theme: "Chi-Cheng Lin, Winona State University CS430 Computer Graphics Cohen-Sutherland Line Clipping Algorithm."— Presentation transcript:

1 Chi-Cheng Lin, Winona State University CS430 Computer Graphics Cohen-Sutherland Line Clipping Algorithm

2 2 Topics l Clipping l Cohen-Sutherland Line Clipping Algorithm

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

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

5 5 Line Clipping l Example zLine RetVal Output AB BC CD DE EA o P1 o P2 o P3 o P4o P1 o P2 o P3 o P4

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

7 7 Cohen-Sutherland Line Clipping Algorithm l Examples: ztrivial accept? ztrivial reject? window L1 L2 L3 L4 L5 L6

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

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

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

11 11 Cohen-Sutherland Line Clipping Algorithm l Examples: zoutcodes? ztrivial accept? ztrivial reject? window L1 L2 L3 L4 L5 L6

12 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 13 Cohen-Sutherland Line Clipping Algorithm l A segment that requires 4 clips

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

15 15 Cohen-Sutherland Line Clipping Algorithm l Let dx = p1.x - p2.xdy = 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?


Download ppt "Chi-Cheng Lin, Winona State University CS430 Computer Graphics Cohen-Sutherland Line Clipping Algorithm."

Similar presentations


Ads by Google