Clipping: Clipping is a process of dividing an object into visible and invisible positions and displaying the visible portion and discarding the invisible portion. Generally we have clipping algorithms for the following primitive types. –Point clipping –Line clipping –Area clipping (Polygon) –Curve clipping –Text clipping
Point clipping algorithm or Simple visibility algorithm: If xleft, xright, ybottom, and ytop denote the boundaries of a rectangle clipping window into a point (x, y) lies inside the window including the boundary if xleft ≤ x ≤ xright ybottom ≤ y ≤ ytop.
Point clipping algorithm or Simple visibility algorithm: where xleft, xright, ybottom and ytop are the positions of the edges of the screen. These inequalities provide us with a very simple method of clipping pictures on a point-by-point basis; we substitute the co-ordinates of each point for x and y and if the point fails to satisfy either inequality, it is invisible.
Line clipping: The below figure shows a number of different attitudes that a straight line segment may take with respected to the screen. It is an extremely useful fact that clipping to a convex boundary such as square screen never generates more than one visible segment of a straight line. This means that the visible segment of a straight line can be determined simply by computing its two endpoints.
Line clipping Algorithm: 1.If a, b are the end points of the line, 1.check if the line is totally visible by checking whether the endpoints are totally inside the boundary. 2. If both end points left, right are above and below the window then the line is trivially invisible and hence reject.
Line clipping Algorithm: If one point is inside and one point is outside then a portion of a line is invisible and a portion of a line is visible. This can be achieved by finding intersection of the line with corresponding boundaries such that the line segment outside the window is discarded, and the line segment inside the boundary is displayed.
Line clipping Algorithm: If the two points are outside such that the line is diagonally passing the window then the intersection with the two boundaries are calculated and the position of the line from one boundary to other is displayed and the remaining two portions of the line are discarded.
Cohen-Sutherland line clipping algorithm: This is one of the most popular line clipping algorithms. This was introduced by Dan Cohen and Ivan Sutherland. It was designed not only to find the endpoints very rapidly but also to reject even more rapidly any line that is clearly invisible. This makes it a very good algorithm. For clipping pictures that are much larger than the screen.
Cohen-Sutherland line clipping algorithm… Assign a four bit code to all regions as shown in the figure. Every line end point in a picture is assigned a 4-bit binary code known as region code, that identifies the location of the point relative to the boundaries of the clipping rectangle. Each bit position in the region code is used to indicate one of the 4 rectangle coordinates positions of the point with respected to clipping window to the left, right, below and top.
Cohen-Sutherland line clipping algorithm… –Bit 1 – left –Bit 2 – right –Bit 3 – below –Bit 4 – top A value of 1 in any bit position indicates that the point is in the relative position otherwise the bit position is set to zero. If a point is within the clipping the region code is 0000.
Cohen-Sutherland line clipping algorithm… If the code of both the endpoints are 0000 then the line is totally visible and hence draw the line. Bit values in the region codes are determined by comparing endpoint co- ordinate values (x, y) to the clip boundaries.
Cohen-Sutherland line clipping algorithm… Bit 1 is set to 1 if x < wmin. The other three bit values can be determined using similar comparison or calculating differences between endpoint co-ordinates and clipping boundaries. Use the resultant sign of each differences calculation to set the corresponding value in the region code.
Cohen-Sutherland line clipping algorithm… –Bit 1 is the sign bit of x-xwmin –Bit 2 is the sign bit of xwmax-x –Bit 3 is the sign bit of y-ywmin –Bit 4 is the sign bit of ywmax-y If any line have 1 in the same bit position in the region codes for each endpoint are completely outside the clipping rectangle, so we discard the line
Cohen-Sutherland line clipping algorithm… For a line with end points co-ordinates (x1, y1) and (x2, y2) then the y coordinate of the intersection point with a vertical boundary can be obtained with the calculation y=y1 + m(x-x1) (1), where the x value is set either to xleft or xright.
Cohen-Sutherland line clipping algorithm… Similarly if we are looking for the intersection with a horizontal boundary, the x co-ordinate can be calculated as x = x1 + 1/m (y-y1) (2) where y set either ybottom or ytop.
Cohen-Sutherland line clipping algorithm… So the points are given by left: (xleft, y=m(xleft-x1) + y1) right: (xright, y=m(xright-x1) +y1 ) top: (x=1/m (ytop-y1) + x1, ytop) bottom: (x=1/m (ybottom-y1) + x1, ybottom) where (x1, y1) and (x2, y2) are the points and xleft, xright, ytop and ybottom are the boundaries of the window and m is the slope which is given by m = y2-y1 / x2-x1 (3).
Example Display the corresponding visible portion of line leaving the outside boundary Example: Clip the line with the boundaries (-1, 1) of x and (-1, 1) of y and the points are (1/2, 1/4) and (1/2, 3/2).
Example Solution: Given xleft= -1 and xright= 1 ytop= 1 and ybottom= -1 let A=(1/2, 1/4) and B=(1/2, 3/2) i.e., A=(0.5, 0.25) and B=(0.5, 1.5). So the line is the bitwise position of A is 0000 and for B is So clearly B is not in the region. So we have to find the horizontal intercept point. i.e., (x=1/m (ytop-y1) + x1, ytop).
Example m= y2-y1 = 1.5 – 0.25 = ∞ x2-x1 0.5 – 0.5 The point is x= 1/ ∞ ( ) = 0.5 y=1 The point is (0.5, 1).
Example Example: Clip the line with the boundaries (0, 0) and (15, 15) and the points are (2, 3) and (9, 10). Solution: Given (x1, y1) = (2, 3) (x2, y2) = (9, 10) Here xwmin = 0, ywmin = 0. Xwmax = 15, ywmax = 15. Bit 1 is the sign bit of x-xwmin
Example Bit 2 is the sign bit of xwmax-x Bit 3 is the sign bit of y-ywmin Bit 4 is the sign bit of ywmax-y For (2, 3) the region code is For (9, 10) the region code is So the line should be drawn.
Example Example: Clip the line with the boundaries (0, 0) and (15, 15) and the points are (2, - 5) and (2, 18). Solution: Region code for (2, -5) is 2-0=0, 15-2=0, -5-0=1, 15+5=0. So it is in 0100 region. So (2, -5) is not in the region.
Example So we have to find the horizontal intercept point (x=1/m (ybottom-y1) + x1, ybottom); m=y2-y1 / x2-x1. m= 18-(-5) / 2-2 = 23/0 = ∞. Then x= 1/∞ (0-(-5)) + 2 = 2. y=0. The point is (2, 0). So we have to draw the line from (2, 0).
Example Region code for (2, 18) is 2-0=0, 15-2=0, 18-0=0, 15-18=1. The code is Clearly it is out of range. So we have to find the horizontal intercept point on top.
Example (x=1/m (ytop-y1) + x1, ytop) m=y2-y1 / x2-x1 = ∞. x= 1/∞ (15-2) + 2 =2. y = ytop = 15. The point is (2, 15). Then draw the line between (2, 15) and (2, 0).