What the f*ck is Cohen Sutherland Line Cipping Algorithm?

Cohen Sutherland Line Clipping Algorithm is used to determine which parts of a line lie within a specified rectangular region.

ยท

4 min read

What the f*ck is Cohen Sutherland Line Cipping Algorithm?

The Cohen-Sutherland line clipping algorithm is used in computer graphics to efficiently determine which parts of a line lie within a specified rectangular region, called a clipping window. It's particularly useful in rendering graphics on a screen where only a portion of the scene is visible.

01 . Area Codes ๐Ÿ“

In Cohen Sutherland Line Clipping Algorithm, we use something known as Area Codes. Area Codes are nothing but four digit binary numbers with the four digits representing LEFT RIGHT BOTTOM TOP (from the least significant bit to the most significant bit).

If an Area Code is 0110, it implies that the area lies in the BOTTOM-RIGHT. Similarly, if the area code is 1001, it implies that the area lies in the TOP-LEFT.

The first step in the algorithm is to divide the area around the viewing window/clipping window in nine different regions and assign area codes to that region. If an area is in top center of the window, it will have the code 1000, and if it is in top right it will have to code 1010. Similarly, if the area is in center left, it will have the code 0001. The clipping window is neither top nor bottom, neither left nor right. Hence, it has the code 0000. The following is the code of all regions around clipping window:

The codes for the nine regions of the Cohen-Sutherland algorithm in the...  | Download Scientific Diagram

02 . What must we remove? ๐Ÿ‡

For the sake of simplicity, let us use some colours.

The bit-based encoding for each of the regions around the clip window.

Now that we have assigned the area codes, we can add some lines here and there.

The second case of the Cohen-Sutherland line clipping algorithm when the line endpoints both lie on the same side of the clip window

Now, we can observe that the endpoints of the lines lie in different region. In this algorithm, to find if a line should we removed or not, we find the AND product of the region where the start point of the line lies and the region where the end point of the line lies.

For example, in the first line in the figure above (the one lying completly in the pink region), the start region's area code is 1001 and the end region's area code is 0001. The AND product of the regions is 1001 AND 0001 = 0001.

If the AND product is NOT 0000, then we remove the line. Hence, the first line shall be removed. Similarly, we will observe that all of the above lines must be removed. Then, what must we keep?

03 . What must we keep? ๐ŸŠ

Now, if we are removing all the lines then which lines must be kept? Any line that lies completely within the region 0000 must be kept without any clipping. They must be kept as a whole and never removed.

However, if a line lies in 0000, but some part of it is outside 0000 region, then it must be clipped!!! For example, two of these lines must be clipped.

The third and final case of the Cohen-Sutherland line clipping algorithm when the line endpoints lie on different sides of the clip plane

The clipping must take place at the intersection of the 0000 and the other region. The part lying inside 0000 must be kept while the part lying outside must be clipped.

As a rule of thumb we can remember if the AND product between the start and the end region codes of the line is 0000, then the line must be clipped. We must also remember that if both the start and the end region code is 0000, then no clipping is necessary and line must be kept as a whole.

After clipping this is how the lines will effectively look like:

This was all about Cohen Sutherland Line Clipping Algorithm. I will not be talking about its code implementation (because even ChatGPT can give you the code for this algorithm - the main objective of this article was the UNDERSTAND the line clipping algorithm).

ย