CS U540 Computer Graphics Prof. Harriet Fell Spring 2007 Lectures 16 – February 11, 2009 17 – February 12, 2009 18 – February 16, 2009 February 28, 2019
Clipping Lines G D C F A F’ G’ E B K H’ J H
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 February 28, 2019
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); } February 28, 2019
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 February 28, 2019
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 February 28, 2019
G G’ G’’ ACCEPT H’ H
Cohen-Sutherland Applet By Patrick Min, CS Department, Princeton University February 28, 2019
Liang-Barsky Clipping Clip window interior is defined by: xleft x xright ybottom y ytop February 28, 2019
Liang-Barsky continued V1 = (x1, y1) x = x0 + tx x = x1 - x0 y = y0 + ty y = y1 - y0 t = 0 at V0 t = 1 at V1 V0 = (x0, y0) February 28, 2019
Liang-Barsky continued Put the parametric equations into the inequalities: xleft x0 + tx xright ybottom y0 + ty ytop -tx x0 - xleft tx xright - x0 -ty y0 - ybottom ty ytop - y0 These describe the interior of the clip window in terms of t. February 28, 2019
Liang-Barsky continued -tx x0 - xleft tx xright - x0 -ty y0 - ybottom ty 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. February 28, 2019
x = xleft p = - x V1 t t (-x) (x0 – xleft) 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
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.
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 February 28, 2019
Polygon Clipping February 28, 2019
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. February 28, 2019
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 February 28, 2019
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 February 28, 2019
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 February 28, 2019
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 February 28, 2019
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 February 28, 2019
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 February 28, 2019
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 February 28, 2019
Sutherland-Hodgeman Exercise 1 Vertex List initial after after after after left top right bottom A B C D A D B C February 28, 2019
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 February 28, 2019
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 February 28, 2019