cohen sutherland – 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 – 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
C Program for Clipping a line using Cohen Sutherland Algorithm https://programmerbay.com/program-for-clipping-a-line-using-cohen-sutherland-algorithm/ https://programmerbay.com/program-for-clipping-a-line-using-cohen-sutherland-algorithm/#respond Mon, 25 Jul 2022 15:31:03 +0000 https://www.programmerbay.com/?p=2157 In this article, we’ll be clipping a line in C using Cohen Sutherland algorithm.

Clipping is a process of removing a portion of a line or an object that falls outside of the specified region. Cohen Sutherland is a line clipping algorithm which is used to clip out the extra portion of the line from view plane.

Must Read: What is Cohen Sutherland line clipping algorithm?

C program for line clipping using Cohen Sutherland algorithm

Program:

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int rcode_begin[4]={0,0,0,0},rcode_end[4]={0,0,0,0},region_code[4];
int W_xmax,W_ymax,W_xmin,W_ymin,flag=0;
float slope;
int x,y,x1,y1,i, xc,yc;
int gr=DETECT,gm;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\n****** Cohen Sutherlsnd Line Clipping algorithm ***********");
printf("\n Now, enter XMin, YMin =");

scanf("%d %d",&W_xmin,&W_ymin);
printf("\n First enter XMax, YMax =");
scanf("%d %d",&W_xmax,&W_ymax);
printf("\n Please enter intial point x and y= ");
scanf("%d %d",&x,&y);
printf("\n Now, enter final point x1 and y1= ");
scanf("%d %d",&x1,&y1);
cleardevice();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(x,y,x1,y1);
line(0,0,600,0);
line(0,0,0,600);
if(y>W_ymax)  {
rcode_begin[0]=1;	   // Top
flag=1 ;
}
if(y<W_ymin) {
rcode_begin[1]=1;           // Bottom
flag=1;
}
if(x>W_xmax)  {
rcode_begin[2]=1;           // Right
flag=1;
}
if(x<W_xmin)   {
rcode_begin[3]=1;           //Left
flag=1;
}

//end point of Line
if(y1>W_ymax){
rcode_end[0]=1;           // Top
flag=1;
}
if(y1<W_ymin) {
rcode_end[1]=1;           // Bottom
flag=1;
}
if(x1>W_xmax){
rcode_end[2]=1;           // Right
flag=1;
}
if(x1<W_xmin){
rcode_end[3]=1;           //Left
flag=1;
 }
if(flag==0)
{
printf("No need of clipping as it is already in window");
}
flag=1;
for(i=0;i<4;i++){
region_code[i]= rcode_begin[i] && rcode_end[i] ;
if(region_code[i]==1)
 flag=0;
}
if(flag==0)
{
printf("\n Line is completely outside the window");
}
else{
slope=(float)(y1-y)/(x1-x);
if(rcode_begin[2]==0 && rcode_begin[3]==1)   //left
{
y=y+(float) (W_xmin-x)*slope ;
x=W_xmin;

}
if(rcode_begin[2]==1 && rcode_begin[3]==0)       // right
{
y=y+(float) (W_xmax-x)*slope ;
x=W_xmax;

}
if(rcode_begin[0]==1 && rcode_begin[1]==0)      // top
{
x=x+(float) (W_ymax-y)/slope ;
y=W_ymax;

}
if(rcode_begin[0]==0 && rcode_begin[1]==1)     // bottom
{
x=x+(float) (W_ymin-y)/slope ;
y=W_ymin;

}
// end points
if(rcode_end[2]==0 && rcode_end[3]==1)   //left
{
y1=y1+(float) (W_xmin-x1)*slope ;
x1=W_xmin;

}
if(rcode_end[2]==1 && rcode_end[3]==0)       // right
{
y1=y1+(float) (W_xmax-x1)*slope ;
x1=W_xmax;

}
if(rcode_end[0]==1 && rcode_end[1]==0)      // top
{
x1=x1+(float) (W_ymax-y1)/slope ;
y1=W_ymax;

}
if(rcode_end[0]==0 && rcode_end[1]==1)     // bottom
{
x1=x1+(float) (W_ymin-y1)/slope ;
y1=W_ymin;

}
}
delay(1000);
clearviewport();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(0,0,600,0);
line(0,0,0,600);
setcolor(RED);
line(x,y,x1,y1);
getch();
closegraph();
}

 

 

Output:

Getting input from user:

cohen setherland

Before Clipping the line

cohen setherland before clipping

After Clipping the line:

cohen setherland after clipping

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