cohen sutherland algorithm – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 21 Aug 2022 07:18:04 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg cohen sutherland algorithm – Programmerbay https://programmerbay.com 32 32 Explain Cohen Sutherland Line Clipping Algorithm https://programmerbay.com/cohen-sutherland-line-clipping-algorithm/ https://programmerbay.com/cohen-sutherland-line-clipping-algorithm/#respond Mon, 25 Jul 2022 18:12:28 +0000 https://www.programmerbay.com/?p=2165 Line clipping is a clipping concept in which lines that lies outside the clipping window is removed from the clip region. As a result, only lines which is inside the view plane are visible. Cohen Sutherland Algorithm is one of the popular line clipping algorithm used for the purpose.

Must Read : What is Clipping in Computer Graphics

What is Cohen Sutherland Line Clipping?

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.

Region Code

region code structure

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.

Algorithm of Cohen Sutherland Line Clipping

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,
x=x+(W_ymax-y)/slope ;
y=W_ymax;
If the line passes through the bottom,
x=x+(W_ymin-y)/slope ;
y=W_ymin;
if the line passes through the left region,
y=y+(W_xmin-x)*slope,
x1=W_xmin;
if the line passes through the right region,
y1=y1+(W_xmax-x1)*slope ,
x1=W_xmax

5) Now, overwrite the endpoint with a new one and update it.
6) Repeat 4th step till your line doesn’t get clipped completely.

C Program for Clipping a line using Cohen Sutherland Algorithm

 

 

]]>
https://programmerbay.com/cohen-sutherland-line-clipping-algorithm/feed/ 0