clipping – 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 clipping – 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
What is Clipping in Computer Graphics https://programmerbay.com/what-is-clipping-in-computer-graphics/ https://programmerbay.com/what-is-clipping-in-computer-graphics/#respond Thu, 23 Jun 2022 17:48:42 +0000 https://programmerbay.com/?p=5136 Clipping is the process of drawing pixels only to a selected region or well-defined window. In this, pixels that lie outside the window or clip region get removed by using various clipping algorithms. It is mainly used for artistic purposes.

Technically, it can be viewed as a method or procedure that determines whether a pixel is outside or inside of a specified region. The method is termed as clipping or clipping algorithm.


There are various applications ranging from surface visibility identification in three-dimensional view, drawing operation, to object creation using solid modeling method.

Based on application, clipping window boundaries is identified i.e. polygon.

A portion of two-dimensional scene that is chosen for display is termed as clipping window.

A region to which an object inside the clipping window is mapped is called viewport.

Read : What is window and viewport?

A line or an object can be partially or completely inside in a window.

If the given line or object is partially inside the clip region, then the portion which is outside the window would be removed.

Point Clipping:

In point clipping, a point (x,y) is checked whether it falls within the window or not.

Clipping windowPoint clipping in C graphics

Considering a rectangular window, as mentioned similar to above diagram,  suppose we have a point , P = (x,y) if it doesn’t meet below condition, then it would not be visible for display.
  • A point that lies inside the clipping window is considered as visible and no clipping is required for such case
  •  If it lies outside the window, then it would be considered as  invisible
Further, point clipping is not often used when it’s compared to polygon clipping or line clipping.

 Line Clipping:

A line clipping comes into play, where the extra portion of a given line that lies outside the window is cut off from the clip region. There is a popular line clipping algorithm named Cohen Sutherland algorithm.

Clipping window

Line Clipping in C graphics

  • If a line intersects the clipping window, then the line is required to be clipped as it is considered as partially visible
  • If a line falls inside the clipping window, then it is considered as visible
  • Otherwise, invisible
In line clipping, a line segment is first validated whether it is entirely inside or outside clipping window. It is also possible to have a line lies partially inside. In such case, it requires to be clipped out the portion of line segment that falls outside the clipping window.
 

 Polygon clipping:

A polygon is two dimensional shape that is made up of more than two straight lines. For example. triangle, quadrilateral.
 
 

Ploygon clipping also follows the same concept, it cuts out vertices of the polygon that is outside the windows. Sutherland–Hodgman algorithm and Weiler-Atherton polygon clipping algorithm techniques are used for clipping a polygon.

Clipping a polygon is a complex procedure, unlike line clipping where only two points need to map one another. In polygon clipping, a method may generate a sequence of vertices as output that can be represented as clipping boundaries.

Clipping window

Polygon Clipping

  • If a part of polygon intersects the clipping window, then a particular portion that lies outside the window is needed to be removed

Text Clipping:

It can be defined as a process where a string (a sequence of character) that falls outside the window is removed. 

There are various text clipping methods and its usage depends on character generation mechanisms. all-or-none clipping is the simplest method for clipping text corresponding to a window.

Clipping window

Text clipping

 
  • If string falls outside the clip window, then don’t consider it
  • If string falls partially inside the window, then discard it completely
  • Otherwise. consider it
 
Exterior Clipping
 
Normally, the clipping is performed to save the portion of the picture that lies within a clipping window. Oppose to this, exterior clipping is a type of clipping used to cut off pixels that fall inside the window and save the pixel that come outside the window. One of its application is Multiple Windows in Display Screen.
 
Interior Clipping
 
As name suggest, it is a clipping type that is used for removing pixels that falls outside the window and save the pixel that is inside the window.

]]>
https://programmerbay.com/what-is-clipping-in-computer-graphics/feed/ 0