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.
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:
02 . What must we remove? ๐
For the sake of simplicity, let us use some colours.
Now that we have assigned the area codes, we can add some lines here and there.
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 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.