Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS U540 Computer Graphics Prof. Harriet Fell Spring 2007

Similar presentations


Presentation on theme: "CS U540 Computer Graphics Prof. Harriet Fell Spring 2007"— Presentation transcript:

1 CS U540 Computer Graphics Prof. Harriet Fell Spring 2007
Lectures 16 – February 14, 2007 17 – February 15, 2007 18 – February 21, 2007 August 1, 2019

2 Clipping Lines G D C F A F’ G’ E B K H’ J H

3 Intersections We know how to find the intersections of a line segment
P + t(Q-P) with the 4 boundaries x = xmin x = xmax y = ymin y = ymax Q P August 1, 2019

4 Cohen-Sutherland Clipping
Assign a 4 bit outcode to each endpoint. 0000 1000 1100 0010 above left below right 0100 0110 1001 0001 0011 //above left below right int outcode(x,y,xmin,xmax,ymin,ymax) { int code=0; if(x > xmax) code |= 1; //right if(y < ymin) code |= 2; //below if(x < xmin) code |= 4; //left if(y > ymax) code |= 8; //above return(code); } August 1, 2019

5 Cohen-Sutherland Clipping
Identify lines that are trivially accepted or trivially rejected. 0000 1000 1100 0010 above left below right 0100 0110 1001 0001 0011 if(outcode(P)|outcode(Q)==0) accept else if(outcode(P)&outcode(Q))!=0) reject else test further August 1, 2019

6 Cohen-Sutherland continued
Clip against one boundary at a time, top, left, bottom, right. Check for trivial accept or reject. If a line segment PQ falls into the “test further” category then if (outcode(P) & 1000  0) replace P with PQ intersect y = top else if (outcode(Q) & 1000  0) replace Q with PQ intersect y = top go on to next boundary August 1, 2019

7 G G’ G’’ ACCEPT H’ H

8 Cohen-Sutherland Applet
By Patrick Min, CS Department, Princeton University August 1, 2019

9 Liang-Barsky Clipping
Clip window interior is defined by: xleft  x  xright ybottom  y  ytop August 1, 2019

10 Liang-Barsky continued
V1 = (x1, y1) x = x0 + tx x = x1 - x0 y = y0 + ty y = y1 - y0 t = 0 at V0 t = 1 at V1 V0 = (x0, y0) August 1, 2019

11 Liang-Barsky continued
Put the parametric equations into the inequalities: xleft  x0 + tx  xright ybottom  y0 + ty  ytop -tx  x0 - xleft tx  xright - x0 -ty  y0 - ybottom ty  ytop - y0 These decribe the interior of the clip window in terms of t. August 1, 2019

12 Liang-Barsky continued
-tx  x0 - xleft tx  xright - x0 -ty  y0 - ybottom ty  ytop - y0 These are all of the form tp  q For each boundary, we decide whether to accept, reject, or which point to change depending on the sign of p and the value of t at the intersection of the line with the boundary. August 1, 2019

13 x = xleft p = - x V1 t  t  (x0 – xleft)/(-x) = q/p t = (x0 – xleft)/(x0 – x1 ) is between 0 and 1. x = x1 – x0 > 0 so p < 0  replace V0 V0 V1 t = (x0 – xleft)/(x0 – x1 ) is between 0 and 1. x = x1 – x0 < 0 so p > 0  replace V1  t V0

14 x = tleft p = - x V1 p < 0 so might replace V0 but t = (x0 – xleft)/(x0 – x1 ) < 0 so no change. V0 t  V1 V0 p > 0 t > 1 V1 V0 t  p < 0 so might replace V0 but t = (x0 – xleft)/(x0 – x1 ) > 1 so reject.  t V0 V1 p > 0 so might replace V1 but t = (x0 – xleft)/(x0 – x1 ) < 0 so reject.

15 Liang-Barsky Rules 0 < t < 1, p < 0 replace V0
t < 0, p < 0 no change t < 0, p > 0 reject t > 1, p > 0 no change t > 1, p < 0 reject August 1, 2019

16 Polygon Clipping August 1, 2019

17 Sutherland-Hodgeman Polygon Clipping Algorithm
Clip one boundary at a time: left, top, right, bottom. Check each adjacent pair of vertices (P,Q), in order to make a new vertex list. If P is out and Q is in, add intersection point with boundary and Q. If P and Q are in, add Q. If P is in and Q is out, add the intersection point with boundary only. If P and Q are both out, add nothing. August 1, 2019

18 Sutherland-Hodgeman Clipping Example
V1 New Vertex List V2 Clip Left V2 V3 L2 If P is out and Q is in, add intersection point with boundary and Q. If P is in and Q is out, add the intersection point with boundary only. V4 If P and Q are in, add Q. V6 L1 V5 L1 V5 L2 V3 V1 V4 August 1, 2019

19 Sutherland-Hodgeman Clipping Example
V1 New Vertex List V2 Clip Left V2 Clip Top V3 L2 V4 V5 L1 L1 V5 L2 V3 V1 V4 August 1, 2019

20 Sutherland-Hodgeman Clipping Example
V1 New Vertex List V2 Clip Top T2 T1 T1 V3 L2 If P is in and Q is out, add the intersection point with boundary only. If P is out and Q is in, add intersection point with boundary and Q. V4 If P and Q are both out, add nothing. If P and Q are in, add Q. V5 L1 L1 V5 L2 V3 T2 V4 August 1, 2019

21 Sutherland-Hodgeman Clipping Example
New Vertex List Clip Right Clip Top T2 T1 T1 V3 L2 V4 V5 L1 L1 V5 L2 V3 T2 V4 August 1, 2019

22 Sutherland-Hodgeman Clipping Example
New Vertex List Clip Right T2 T1 R1 R2 R1 L2 V4 V5 L1 L1 V5 L2 V3 T2 R2 T1 V4 August 1, 2019

23 Sutherland-Hodgeman Clipping Example
New Vertex List T2 T1 R2 B1 R1 L2 B2 V5 L1 L1 V5 L2 T2 R2 T1 Clip Bottom B2 B1 R1 V4 August 1, 2019

24 Sutherland-Hodgeman Clipping Example
New Vertex List T2 T1 R2 B1 R1 L2 B2 V5 L1 L1 V5 L2 T2 R2 T1 B2 B1 R1 August 1, 2019

25 Sutherland-Hodgeman Exercise 1
Vertex List initial after after after after left top right bottom A B C D A D B C August 1, 2019

26 Sutherland-Hodgeman Exercise 2
Vertex List initial after after after after left top right bottom H I I A A A B B B C C C D D D E E E F F F G G G H H I A G R1 I D R4 B F R3 E R2 H C August 1, 2019

27 Sutherland-Hodgeman Exercise 2
Vertex List initial after after after after left top right bottom H I I A A A R1 K B B B R2 B1 C C C D D D D D R3 B2 E E E R4 K F F F G R4 G G G H G H H I B3 I A B4 I A R1 A G R1 I D R4 B F B4 B3 B2 K R3 B1 E R2 H C August 1, 2019


Download ppt "CS U540 Computer Graphics Prof. Harriet Fell Spring 2007"

Similar presentations


Ads by Google