Cohen Sutherland Line Clipping Algorithm


Computer Graphics / Monday, November 19th, 2018
(Last Updated On: November 19, 2018)

Cohen Sutherland Line Clippings

This Cohen Sutherland Line Clipping algorithm is quite interesting. The clipping problem is simplified by dividing the area surrounding the window region into four segments Up, Down, Left, Right (U, D, L, R) and assignment of number 1 and 0 to respective segments helps in positioning the region surrounding the window. How this positioning of regions is performed can be well understood by considering Figure 1.

cohen-sutherland 01
Figure 1: Positioning of regions surrounding the window

In Figure 1 you might have noticed, that all coding of regions U, D, L, R is done with respect to window region. As window is neither Up nor Down, neither Left nor Right, so, the respective bits UDLR are 0000; now see region 1 of Figure 1. The positioning code UDLR is 1010, i.e., the region 1 lying on the position which is upper left side of the window. Thus, region 1 has UDLR code 1010 (Up so U=1, not Down so D=0, Left so L=1, not Right so R=0).

The meaning of the UDLR code to specify the location of region with respect to window is:

1st bit ⇒ Up (U); 2nd bit ⇒ Down (D); 3rd bit ⇒ Left (L); 4th bit ⇒ Right (R)

Now, to perform Line clipping for various line segment which may reside inside the window region fully or partially, or may not even lie in the widow region; we use the tool of logical ANDing between the UDLR codes of the points lying on the line.

Logical ANDing (^) operation between respective bits implies

1 ^ 1 = 1; 1 ^ 0 = 0; 0 ^ 1 = 0; 0 ^ 0 = 0

Note:

UDLR code of window is 0000 always and w.r.t. this will create bit codes of other regions.

A line segment is visible if both the UDLR codes of the end points of the line segment equal to 0000 i.e. UDLR code of window region. If the resulting code is not 0000 then, that line segment or section of line segment may or may not be visible.

Now, let us study how this clipping algorithm works. For the sake of simplicity we will tackle all the cases with the help of example lines l1 to l5 shown in Figure 2.

Each line segment represents a case.

cohen-sutherland 02
Figure 2: Various cases of Cohen Sutherland Line Clippings

Note, that in Figure 2, line l1is completely visible, l2 and l3 are completely invisible; l4 and l5 are partially visible. We will discuss these out comings as three separate cases.

Case 1: l1 → completely visible, i.e., Trivial acceptance (both points lie inside the window)

Case 2: l2 and l3 → Invisible, i.e., Trivial acceptance rejection

Case 3: l4 and l5 → partially visible (partially inside the window)

Now, let us examine these three cases with the help of this algorithm:

Case 1: (Trivial acceptance case) if the UDLR bit codes of the end points P, Q of a given line is 0000 then line is completely visible. Here this is the case as the end points a and b of line l1 are: a (0000), b (0000). If this trivial acceptance test is failed then, the line segment PQ is passed onto Case 2.

Case 2: (Trivial Rejection Case) if the logical intersection (AND) of the bit codes of the end points P, Q of the line segment is ≠ 0000 then line segment is not visible or is rejected.

Note that, in Figure 2, line 2 is completely on the top of the window but line 3 is neither on top nor at the in bottom plus, either on the LHS nor on the RHS of the window. We use the standard formula of logical ANDing to test the non visibility of the line segment.

So, to test the visibility of line 2 and 3 we need to calculate the logical intersection of end points for line 2 and line 3.

line l2:  bit code of end points are 1010 and 1000 logical intersection of end points = (1010) ^ (1000) = 1000 as logical intersection ” 0000. So line 2 will be invisible.

line l3: end points have bit codes  0010 and 0101 now logical intersection = 0000, i.e., 0010 ^ 0101 = 0000 from the Figure 2, the line is invisible. Similarly in line 4 one end point is on top and the other on the bottom so, logical intersection is 0000 but then it is partially visible, same is true with line 5. These are special cases and we will discuss them in case 3.

cohen-sutherland 03
Figure 3: Cohen Sutherland line clipping

Case 3: Suppose for the line segment PQ, both the trivial acceptance and rejection tests failed (i.e., Case 1 and Case 2 conditions do not hold, this is the case for l3, l4 and l5 line segments) shown above in Figure 3. For such non-trivial

Cases the algorithm is processed, as follows.

Since, both the bitcodes for the end points P, Q of the line segment cannot be equal to 0000. Let us assume that the starting point of the line segment is P whose bit code is not equal to 0000. For example, for the line segment l we choose P to be the bitcodes 1001. Now, scan the bitcode of P from the first bit to the fourth bit and find the position of the bit at which the bit value 1 appears at the first time. For the line segment l5 it appears at the very first position. If the bit value 1 occurs at the first position then proceed to intersect the line segment with the UP edge of the window and assign the first bit value to its point of intersection as 0. Similarly, if the bit value 1 occurs at the second position while scanning the bit values at the first time then intersect the line segment PQ with the Down edge of the window and so on. This point of intersection may be labeled as P’. Clearly the line segment PP’ is outside the window and therefore rejected and the new line segment considered for dipping will be P’Q. The coordinates of P’ and its remaining new bit values are computed. Now, by taking P as P’, again we have the new line segment PQ which will again be to Case 1 for clipping.

cohen-sutherland 04
Figure 4: Line Clipping – Geometrically

Geometrical study of the above type of clipping (it helps to find point of intersection of line PQ with any edge).

Let (x1, y1 ) and (x2 , y2) be the coordinates of P and Q respectively.

Top case /above case:
If y1 > ywmax then 1st bit of bit code = 1 (signifying above) else bit code = 0

Bottom case / below case:
If y1 < ywmin then 2nd bit = 1 (i.e. below) else bit = 0

Left case:
If x1 < xwmin then 3rd bit = 1 (i.e. left) else bit = 0

Right case:
If x1 > xwmax then 4th bit = 1 (i.e. right) else bit = 0

Algorithm

Pseudo-code of Cohen-Sutherland Algorithm

Limitations of Cohen-Sutherland clipping algorithm

1) Clipping window region can be rectangular in shape only and no other polygonal shaped window is allowed.

2) Edges of rectangular shaped clipping window has to be parallel to the x-axis and y-axis.

3) If end points of line segment lies in the extreme limits i.e., one at R.H.S other at L.H.S., and on one the at top and other at the bottom (diagonally) then, even if the line doesn’t pass through the clipping region it will have logical intersection of 0000 implying that line segment will be clipped but infect it is not so.

cohen-sutherland 05

 Example 01

Clip the line shown in Figure below using Cohen Sutherland Line clipping algorithm.

cohen-sutherland 06 q

Answer:

Using Cohen Sutherland Line clipping algorithm when we clip the line we get the line shown in Figure below as output.

cohen-sutherland 06

 

Leave a Reply

Your email address will not be published. Required fields are marked *