Computer Graphics : Viewing In 2D UNIT-3 Contents Windowing Concepts Clipping Introduction Brute Force Cohen-Sutherland Clipping Algorithm Area Clipping Sutherland-Hodgman Area Clipping Algorithm Windowing I A scene is made up of a collection of objects specified in world coordinates World Coordinates Windowing II When we display a scene only those objects within a particular window are displayed Window wymax wymin wxmax wxmin World Coordinates Windowing III Because drawing things to a display takes time we clip everything outside the window Window wymax wymin wxmax wxmin World Coordinates Clipping For the image below consider which lines and points should be kept and which ones should be clipped P4 Window wymax P2 P6 P3 P1 P5 P7 P9 P8 wymin P10 wxmin wxmax Point Clipping Easy - a point (x,y) is not clipped if: wxmin ≤ x ≤ wxmax AND wymin ≤ y ≤ wymax otherwise it is clipped P4 Clipped Clipped Window wymax Clipped P7 P5 P2 P1 Points Within the Window are Not Clipped P9 P8 wymin Clipped wxmin P10 wxmax Line Clipping Harder - examine the end-points of each line to see if they are in the window or not Situation Solution Both end-points inside Don’t clip the window One end-point inside the window, one outside Must clip Both end-points outside the window Don’t know! Example Cohen-Sutherland Algorithm Idea: eliminate as many cases as possible without computing intersections Start with four lines that determine the sides of the clipping window y = ymax x = xmin x = xmax y = ymin 9 The Cases Case 1: both endpoints of line segment inside all four lines Draw (accept) line segment as is y = ymax x = xmin x = xmax y = ymin Case 2: both endpoints outside all lines and on same side of a line Discard (reject) the line segment 10 The Cases Case 3: One endpoint inside, one outside Must do at least one intersection Case 4: Both outside May have part inside Must do at least one intersection y = ymax x = xmin x = xmax 11 Defining Outcodes For each endpoint, define an outcode b0b1b2b3 b0 = 1 if y > ymax, 0 otherwise b1 = 1 if y < ymin, 0 otherwise b2 = 1 if x > xmax, 0 otherwise b3 = 1 if x < xmin, 0 otherwise Outcodes divide space into 9 regions Computation of outcode requires at most 4 subtractions 12 Using Outcodes Consider the 5 cases below AB: outcode(A) = outcode(B) = 0 Accept line segment 13 Using Outcodes CD: outcode (C) = 0, outcode(D) 0 Compute intersection Location of 1 in outcode(D) determines which edge to intersect with Note if there were a segment from A to a point in a region with 2 ones in outcode, we might have to do two interesections 14 Using Outcodes EF: outcode(E) logically ANDed with outcode(F) (bitwise) 0 Both outcodes have a 1 bit in the same place Line segment is outside of corresponding side of clipping window reject 15 Using Outcodes GH and IJ: same outcodes, neither zero but logical AND yields zero Shorten line segment by intersecting with one of sides of window Compute outcode of intersection (new endpoint of shortened line segment) Reexecute algorithm 16 Efficiency In many applications, the clipping window is small relative to the size of the entire data base Most line segments are outside one or more side of the window and can be eliminated based on their outcodes Inefficiency when code has to be reexecuted for line segments that must be shortened in more than one step 17 Liang – Barsky clipping In computer graphics, the Liang–Barsky algorithm (named after You-Dong Liang and Brian A. Barsky) is a line clipping algorithm. The Liang–Barsky algorithm uses the parametric equation of a line and inequalities describing the range of the clipping window to determine the intersections between the line and the clipping window. With these intersections it knows which portion of the line should be drawn. This algorithm is significantly more efficient than Cohen– Sutherland. The idea of the Liang-Barsky clipping algorithm is to do as much testing as possible before computing line intersections. Consider first the usual parametric form of a straight line: A point is in the clip window, if To compute the final line segment: A line parallel to a clipping window edge has for that boundary. If for that the line is completely outside and can be eliminated. When the line proceeds outside to inside the clip window and when , the line proceeds inside to outside. For nonzero , gives the intersection point. Here Liang-Barsky Leave Enter Leave Leave Leave Enter Enter Enter Liang-Barsky - Algorithm Compute entering u values, which are qk/pk for each pk<0 Compute leaving u values, which are qk/pk for each pk>0 Parameter value for small u end of line is:usmall= max(0, entering u’s) parameter value for large t end of line is: ularge=min(1, leaving u’s) If usmall<ularge, there is a line segment - compute endpoints by substituting t values Improvement (and actual Liang-Barsky): compute u’s for each edge in turn (some rejects occur earlier like this.) Area Clipping Similarly to lines, areas must be clipped to a window boundary Consideration must be taken as to which portions of the area must be clipped Sutherland-Hodgman Area Clipping Algorithm A technique for clipping areas developed by Sutherland & Hodgman Put simply the polygon is clipped by comparing it against each boundary in turn Original Area Clip Left Clip Right Sutherland turns up again. This time with Gary Hodgman with whom he worked at the first ever graphics company Evans & Sutherland Clip Top Clip Bottom The Sutherland-Hodgeman Polygon-Clipping Algorithms clips a given polygon successively against the edges of the given cliprectangle. These clip edges are denoted with e1, e2, e3, and e4, here. The closed polygon is represented by a list of its vertices (v1 to vn; Since we got 15 vertices in the example shown above, vn = v15.). Clipping is computed successively for each edge. The output list of the previous clipping run is used as the input list for the next clipping run. Sutherland-Hodgman Area Clipping Algorithm (cont…) To clip an area against an individual boundary: Consider each vertex in turn against the boundary Vertices inside the boundary are saved for clipping against the next boundary Vertices outside the boundary are clipped If we proceed from a point inside the boundary to one outside, the intersection of the line with the boundary is saved If we cross from the outside to the inside intersection point and the vertex are saved Sutherland-Hodgman Example Each example shows the point being processed (P) and the previous point (S) Saved points define area clipped to the boundary in question S S P I P Save Point P Save Point I S P I P S No Points Saved Save Points I & P Other Area Clipping Concerns Clipping concave areas can be a little more tricky as often superfluous lines must be removed Window Window Window Clipping curves requires more work Window For circles we must find the two intersection points on the window boundary