You are on page 1of 3

#include<stdio.

h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>
main()
{
int m,i,j=0,and,xmin,ymin,xmax,ymax,x1,y1,x2,y2,code1[4],code2[4];
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
printf("\n enter window boundary(left,top,right,bottom):");
scanf("%d%d%d%d",&xmin,&ymin,&xmax,&ymax);
printf("\n enter co-ordinates of line:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
if(x1<xmin)
code1[3]=1;
else
code1[3]=0;
if(x1>xmax)
code1[2]=1;
else
code1[2]=0;
if(y1<ymin)
code1[1]=1;
else
code1[1]=0;
if(y1>ymax)
code1[0]=1;
else
code1[0]=0;
if(x2<xmin)
code2[3]=1;
else
code2[3]=0;
if(x2>xmax)
code2[2]=1;
else
code2[2]=0;
if(y2<ymin)
code2[1]=1;
else
code2[1]=0;
if(y2>ymax)
code2[0]=1;
else
code2[0]=0;

for(i=0;i<4;i++)
{
if(code1[i]==0)
if(code2[i]==0)
j++;
}
if(j==4)
{
printf("\n the line is completely inside");
exit(0);
}
for(i=0;i<5;i++)
{
if(code1[i]==1&&code2[i]==1)
{
and=1;
break;
}
else
and=0;
}
if(and==1)
{
printf("\n the line is completely outside");
exit(0);
}
printf("\n the line is partially inside and outside");
m=(y2-y1)/(x2-x1);
if(x2>xmax)
{
y2=y2+m*(xmax-x2);
x2=xmax;
}
if(y2>ymax)
{
x2=x2+(ymax-y2)/m;
y2=ymax;
}
if(x1<xmin)
{
y1=y1+m*(xmin-x1);
x1=xmin;
}
if(y1<ymin)
{
x1=x1+(ymin-y1)/m;
y1=ymin;
}

if(y1>ymax)
{
x1=x1+(ymax-y1)/m;
y1=ymax;
}
if(y2<ymin)
{
x2=x2+(ymin-y2)/m;
y2=ymin;
}
setcolor(GREEN);
line(x1,y1,x2,y2);
getch();
}

You might also like