Cohen Sutherland uses region code to clip a portion of the line which is not present in the visible region. It divides a region into 9 columns based on (X_MAX,Y_MAX) and (X_MIN,Y_MIN).
The central part is viewing region or window, all the lines which lie within this region are completely visible. A region code is always assigned to endpoints of the given line.
To check whether the line is visible or not.
A line can be drawn:
a) Inside the Window, if that is the case, then no clipping is required
b) Completely outside the Window, if that is the case, then no clipping is required because entire line isn’t in the window.
c) Partially inside or outside the window, if that is the case, then we need to find the intersection point and clipping would take place.
Cohen Sutherland line clipping algorithm
1) First, define a window or View plane. Get coordinates from the user of a line.
2) Initialize the region code for initial and end coordinates of a line to 0000.
3) Check whether the line lies within, partially or outside the window.
- Now, Assign the region code for both the initial and end coordinates.
- After Assigning, If both the endpoints give 0000, then the line is completely within the window.
- Else perform AND operation, if the result is not 0000, then the line is not inside the window and that line would not be considered for clipping.
- Else the line is partially inside the window.
4) After confirming the line is partially inside the window, the next step is to find the intersection point at the window boundary. By using the following formula:
If the line passes through the top,
If the line passes through the bottom,
if the line passes through the left region,
if the line passes through the right region,
5) Now, overwrite the endpoint with a new one and update it.
6) Repeat 7 step till your line doesn’t get clipped completely.