Line Clipping Algorithms are used for clipping line with respect to a rectangle called as clipping window. Cohen Sutherland line clipping algorithm checks both the endpoints with four boundaries of rectangle(top,bottom,left and right) to decide whether the point lies inside or outside the four boundaries.This checking is done using a four bit code corresponding to four boundaries where each of the bit specifies whether point is outside or inside a particular boundary.The line that does not intersect the clipping window is completely outside or inside the clipping window and does not need to be clipped. However for the lines that intersect the clipping window, the intersection point of line need to be found with clipping window.

```
#include<stdio.h>
#include<conio.h>
/*Header file for built-in graphics functions such as plot() and initgraph()*/
#include<graphics.h>
#include<math.h>
#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);
enum { TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
int xmin,xmax,ymin,ymax;
/*This function uses 4 bit codes for both the endpoints to find if the line needs to be clipped and if yes, it finds the intersection point and displays the clipped line */
void clip(float x0,float y0,float x1,float y1)
{
outcode outcode0,outcode1,outcodeOut;
int accept = FALSE,done = FALSE;
outcode0 = CompOutCode(x0,y0);
outcode1 = CompOutCode(x1,y1);
do
{
if(!(outcode0|outcode1))
{
accept = TRUE;
done = TRUE;
}
else if(outcode0 & outcode1)
done = TRUE;
else
{
float x,y;
outcodeOut = outcode0?outcode0:outcode1;
if(outcodeOut & TOP)
{
x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
}
else if(outcodeOut & BOTTOM)
{
x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
}
else if(outcodeOut & RIGHT)
{
y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
else
{
y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
}
if(outcodeOut==outcode0)
{
x0 = x;
y0 = y;
outcode0 = CompOutCode(x0,y0);
}
else
{
x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1);
}
}
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);/*line(x1,y1,x2,y2) draws line on screen with (x1,y1) and (x2,y2) specifying the endpoint of line*/
/*outtextxy(x,y,"string") prints string on screen starting with the pixel specified by(x,y)*/
outtextxy(200,20,"LINE AFTER CLIPPING");
/*rectangle(x1,y1,x2,y2) draws rectangle on screen with (x1,y1) and (x2,y2) specifying coordinates of top-left and bottom-right vertex respectively.*/
rectangle(xmin,ymin,xmax,ymax);
}
/* This function checks a point(x,y) with four boundaries to see whether it lies outside or inside and returns result in form of a 4 bit code*/
outcode CompOutCode(float x,float y)
{
outcode code = 0;
if(y>ymax)
code|=TOP;
else if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else if(x<xmin)
code|=LEFT;
return code;
}
int main( )
{
int x1,y1,x2,y2;
int gd = DETECT,gm ;
initgraph(&gd, &gm, "c:\\tc\\bgi");/*loads the graphic driver and puts system into graphics mode*/
/*
syntax for initgraph() is:
void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);
*graphdriver is an integer that specifies the graphics driver to be used.DETECT requests auto-detect for the driver.
*graphmode is an integer that specifies the initial graphics mode.If *graphdriver equals DETECT,*graphmode is set by initgraph to the highest resolution available for the detected driver.
*/
printf("\t\t\tCOHEN SUTHERLAND LINE CLIPPING ALGORITHM\n\n\tEnter the endpoints of line");
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
printf("\n\tEnter the rectangular coordinates of clipping window");
scanf("%d %d %d %d",&xmin,&ymin,&xmax,&ymax);
/*outtextxy(x,y,"string") prints string on screen starting with the pixel specified by(x,y)*/
outtextxy(200,100,"LINE BEFORE CLIPPING");
/*line(x1,y1,x2,y2) draws line on screen with (x1,y1) and (x2,y2) specifying the endpoint of line*/
line(x1,y1,x2,y2);
/*rectangle(x1,y1,x2,y2) draws rectangle on screen with (x1,y1) and (x2,y2) specifying coordinates of top-left and bottom-right vertex respectively.*/
rectangle(xmin,ymin,xmax,ymax);
getch();
cleardevice();
clip(x1,y1,x2,y2);
getch();
return 0;
}
```

You can run this code in Turbo C++. Dev C++ and Microsoft visual studio do not support graphics libraries.

You can download Turbo C++ using this link Turbo C++

Other articles related to Computer Graphics available on TutsHeap are:

Boundary-Fill Algorithm

Flood-Fill Algorithm

Mid-Point Ellipse Drawing Algorithm

Mid-Point Circle Drawing Algorithm

DDA Line Drawing Algorithm

Bresenhamâ€™s Line Drawing Algorithm

This content has been helpful to you?

Thanks for contributing!

Pingback: Mid-Point Ellipse Drawing Algorithm | Tuts Heap()

Pingback: Boundary-Fill Algorithm | Tuts Heap()

Pingback: Mid-Point Circle Drawing Algorithm | Tuts Heap()

Pingback: DDA Line Drawing Algorithm | Tuts Heap()

Pingback: Bresenham’s Line Drawing Algorithm | Tuts Heap()

Pingback: Flood-Fill Algorithm - Tuts Heap()