270 likes | 280 Views
CS U540 Computer Graphics. Prof. Harriet Fell Spring 2007 Lectures 16 – February 14, 2007 17 – February 15, 2007 18 – February 21, 2007. 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
E N D
CS U540Computer Graphics Prof. Harriet Fell Spring 2007 Lectures 16 – February 14, 2007 17 – February 15, 2007 18 – February 21, 2007
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
1100 1000 1001 0001 0000 0100 0010 0011 0110 above left below right Cohen-Sutherland Clipping • Assign a 4 bit outcode to each endpoint. //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); }
1100 1000 1001 0001 0000 0100 0010 0011 0110 above left below right Cohen-Sutherland Clipping • Identify lines that are trivially accepted or trivially rejected. • if(outcode(P)|outcode(Q)==0) accept • else if(outcode(P)&outcode(Q))!=0) • reject • else test further
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
G G’ ACCEPT G’’ H’ H
Cohen-Sutherland Applet • Cohen-Sutherland Applet • By Patrick Min, CS Department, Princeton University
Liang-Barsky Clipping Clip window interior is defined by: xleft x xright ybottom y ytop
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)
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 decribe the interior of the clip window in terms of t.
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.
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 x = xleft p = - x V1 t V0 V1 t = (x0 – xleft)/(x0 – x1 ) is between 0 and 1. x = x1 – x0 < 0 so p > 0 replace V1 t V0
V0 p > 0 t > 1 V1 p < 0 so might replace V0 but t = (x0 – xleft)/(x0 – x1 ) > 1 so reject. V1 V0 t p > 0 so might replace V1 but t = (x0 – xleft)/(x0 – x1 ) < 0 so reject. V0 t V1 p = - x x = tleft V1 p < 0 so might replace V0 but t = (x0 – xleft)/(x0 – x1 ) < 0 so no change. V0 t
Liang-Barsky Rules • 0 < t < 1, p < 0 replace V0 • 0 < t < 1, p > 0 replace V1 • t < 0, p < 0 no change • t < 0, p > 0 reject • t > 1, p > 0 no change • t > 1, p < 0 reject
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.
Clip Left L2 L1 Sutherland-HodgemanClipping Example V1 New Vertex List V2 V2 V3 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 in, add Q. V6 V5 L1 V5 L2 V3 V1 V4
Clip Left L2 Clip Top Sutherland-HodgemanClipping Example V1 New Vertex List V2 V2 V3 V4 V5 L1 L1 V5 L2 V3 V1 V4
L2 Clip Top T2 T1 Sutherland-HodgemanClipping Example V1 New Vertex List V2 T1 V3 If P is out and Q is in, add intersection point with boundary and Q. V4 If P is in and Q is out, add the intersection point with boundary only. 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
L2 Clip Top T2 T1 Clip Right Sutherland-HodgemanClipping Example New Vertex List T1 V3 V4 V5 L1 L1 V5 L2 V3 T2 V4
L2 R1 T2 T1 R2 Clip Right Sutherland-HodgemanClipping Example New Vertex List R1 R2 V4 V5 L1 L1 V5 L2 V3 T2 T1 V4
L2 T2 T1 R1 B2 R2 B1 Clip Bottom Sutherland-HodgemanClipping Example New Vertex List R2 B1 B2 V5 L1 L1 V5 L2 T2 T1 R1 V4
L2 T1 T2 R1 B2 B1 R2 Sutherland-HodgemanClipping Example New Vertex List R2 B1 B2 V5 L1 L1 V5 L2 T2 T1 R1
Sutherland-Hodgeman Exercise 1 Vertex List initial after after after after left top right bottom A B C D A D B C
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 B R4 D F R3 E R2 H C
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 B R4 D F B4 B2 B3 K R3 B1 E R2 H C