Thursday, February 24, 2011

Cohen-Sutherland Line Clipping Algorithm

Cohen-Sutherland Line-Clipping Algorithm

First we test whether both endpoints are inside (and hence draw the line segment) or whether both are left of , right of , below , or above (then we ignore line segment). Otherwise we split the line segment into two pieces at a clipping edge (and thus reject one part). Now we proceed iteratively.
A rather simple accept-reject test is the following:
Divide the plane into 9 regions and assign a 4 bit code to each:
1000
...above top edge
0100
...below bottom edge
0010
...right of right edge
0001
...left of left edge
                            
                                              Codes for the 9 regions associated to clipping rectangle

If both codes are zero then the line segment is completely inside the rectangle. If the bitwise-and of these codes is not zero then the line does not hit since both endpoints lie on the wrong side of at least one boundary line (corresponding to a bit equal to 1). Otherwise take a line which is met by the segment (for this find one non-zero bit), divide the given line at the intersection point in two parts and reject the one lying in the outside half plane
.

                   

Figure of Cohen-Sutherland line clipping