CG/Assignment A-2 (Cohen Sutherland Line Clipping) (using built-in function).cpp

118 lines
2.0 KiB
C++
Raw Normal View History

/*
Problem Statement: Write C++ program to implement Cohen Sutherland line clipping algorithm.
Code from Computer Graphics (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/CG
*/
// BEGINNING OF CODE
2023-10-28 01:19:18 +05:30
#include<iostream>
#include<graphics.h>
#include<math.h>
#include<cstdlib>
using namespace std;
2023-10-28 01:19:18 +05:30
static int LEFT = 1, RIGHT = 2, BOTTOM = 4, TOP = 8, xmax,ymax,xmin,ymin;
2023-10-28 01:19:18 +05:30
int getcode(int x,int y)
{
int code = 0;
if(y>ymax) code |= TOP;
if(y<ymin) code |= BOTTOM;
if(x<xmin) code |= LEFT;
if(x>xmax) code |= RIGHT;
return code;
}
int main()
{
int gd = DETECT,gm;
int x1,y1,x2,y2;
cout<<"Enter top left and bottom right coordinates: ";
cin>>xmin>>ymin>>xmax>>ymax;
cout<<"Enter endpoints of line: ";
cin>>x1>>y1>>x2>>y2;
initgraph(&gd,&gm,NULL);
2023-10-28 01:19:18 +05:30
//outtext("Before clipping");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
delay(10000);
int outcode1 = getcode(x1,y1);
int outcode2 = getcode(x2,y2);
int accept = 0;
while(1)
{
float m = (float)(y2-y1)/(x2-x1);
if(outcode1==0 && outcode2==0)
{
accept = 1;
break;
}
else if((outcode1 & outcode2)!= 0)
{
break;
}
else
{
int x,y;
int temp;
if(outcode1 == 0)
{
temp = outcode2;
}
else
{
temp = outcode1;
}
if(temp & TOP)
{
x = x1 + (ymax - y1)/m;
y = ymax;
}
else if(temp & BOTTOM)
{
x = x1 + (ymin - y1)/m;
y = ymin;
}
else if(temp & LEFT)
{
x = xmin;
y = y1 + m*(xmin - x1);
}
else if(temp & RIGHT)
{
x = xmax;
y = y1 + m*(xmax - x1);
}
if(temp == outcode1)
{
x1 = x;
y1 = y;
outcode1 = getcode(x1,y1);
}
else
{
x2 = x;
y2 = y;
outcode2 = getcode(x2,y2);
}
}
}
setcolor(BROWN);
if(accept)
{
cleardevice();
//outtext("After clipping");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
}
else
{
cleardevice();
//outtext("After clipping");
rectangle(xmin,ymin,xmax,ymax);
}
getch();
closegraph();
return 0;
}
// END OF CODE