### Clip Lines - Cohen-Sutherland Line Clipping Algorithm - C++ Program - Graphics & Multimedia Lab

Cohen-Sutherland Line Clipping Algorithm

C++ program - Graphics & Multimedia Lab

Sourcecode:

```#include<graphics.h>

#include<iostream>

using namespace std;

#define MAX1 20

enum { TOP = 0x8, BOTTOM = 0x4, RIGHT = 0x2, LEFT = 0x1 };

enum { FALSE, TRUE };

typedef unsigned int outcode;

int m=0;

FILE *ip,*op;

outcode compute_outcode(int x, int y, int xmin, int ymin, int xmax, int ymax)

{

outcode oc = 0;

if (y > ymax)

oc |= TOP;

else if (y < ymin)

oc |= BOTTOM;

if (x > xmax)

oc |= RIGHT;

else if (x < xmin)

oc |= LEFT;

return oc;

}

void cohen_sutherland (float x1, float y1, float x2, float y2, float xmin, float ymin, float xmax, float ymax)

{

int accept;

int done;

outcode outcode1, outcode2;

accept = FALSE;

done = FALSE;

outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);

outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);

do

{

if (outcode1 == 0 && outcode2 == 0)

{

accept = TRUE;

done = TRUE;

}

else if (outcode1 & outcode2)

{

done = TRUE;

}

else

{

double x, y;

double m=(y2-y1)/(x2-x1);

int outcode_ex = outcode1 ? outcode1 : outcode2;

if (outcode_ex & TOP)

{

x = x1 + (ymax - y1)/m;

y = ymax;

}

else if (outcode_ex & BOTTOM)

{

x = x1 + (ymin - y1) /m;

y = ymin;

}

else if (outcode_ex & RIGHT)

{

y = y1 + (xmax - x1)* m;

x = xmax;

}

else

{

y = y1 + (xmin - x1)* m;

x = xmin;

}

if (outcode_ex == outcode1)

{

x1 = x;

y1 = y;

outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);

}

else

{

x2 = x;

y2 = y;

outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);

}

}

} while (done == FALSE);

if (accept == TRUE)

{

fprintf(op,"\nx[%d] y[%d] x[%d] y[%d]=%.3f %.3f %.3f %.3f:(clipped line)",m,m,m+1,m+1,x1,y1,x2,y2);

m=m+2;

line (x1, y1, x2, y2);

}

else if((accept == FALSE) && (done == TRUE))

{

fprintf(op,"\nThe line (%.3f,%.3f),(%.3f,%.3f)is trivially rejected.",x1,y1,x2,y2);

}

}

int main()

{

int n;

int i, j;

int ln[MAX1][4];

int clip[4];

int gd = DETECT, gm;

ip=fopen("csinput.txt","r");

op=fopen("csoutput.txt","w");

fscanf (ip,"%d", &n);

for (i=0; i<n; i++)

for (j=0; j<4; j++)

fscanf (ip,"%d", &ln[i][j]);

for (i=0; i<4; i++)

fscanf (ip,"%d", &clip[i]);

initgraph (&gd, &gm, NULL);

rectangle (clip[0], clip[1], clip[2], clip[3]);

for (i=0; i<n; i++)

line (ln[i][0], ln[i][1], ln[i][2], ln[i][3]);

getch();

cleardevice();

printf("AFTER CLIPPING");

rectangle (clip[0], clip[1], clip[2], clip[3]);

fprintf(op,"OUTPUT POINTS\n");

for (i=0; i<n; i++)

{

cohen_sutherland (ln[i][0], ln[i][1], ln[i][2], ln[i][3],

clip[0], clip[1], clip[2], clip[3]);

getch();

}

closegraph();

return 0;

}
```
Output:
```nn@linuxmint ~ \$ g++ lab7.cpp -lgraph
nn@linuxmint ~ \$ ./a.out```

"csinput.txt"
```4

40 40  125 220

10 10 40 210

75 80 175 175

60 230 210 45

55 55 200 2005 ```

"csoutput.txt"
```OUTPUT POINTS

x[0] y[0] x[1] y[1]=55.000 71.765 115.556 200.000:(clipped line)
The line (10.000,10.000),(40.000,210.000)is trivially rejected. ```