#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int xwmin,ywmin,xwmax,ywmax;
void rcode(int,int,int []);
int check(int[],int[]);
void main()
{
int gdriver = DETECT, gmode, errorcode;
int x1,y1,x2,y2,code1[4],code2[4],x,y,m;
int point[2][2],p=0;
initgraph(&gdriver, &gmode, "c:/tc/bgi");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
printf("\n Enter the coordinates of window ");
printf("\n xwmin and ywmin: ");
scanf("%d%d",&xwmin,&ywmin);
printf("\n xwmax and ywmax: ");
scanf("%d%d",&xwmax,&ywmax);
printf("\n Enter the coordinates of line ");
printf("\n Enter the coordinate for the first end point of the line: ");
scanf("%d%d",&x1,&y1);
printf("\n Enter the coordinate for the second end point of the line: ");
scanf("%d%d",&x2,&y2);
rcode(x1,y1,code1);
rcode(x2,y2,code2);
if(check(code1,code2)==1)
{
rectangle(xwmin,ywmin,xwmax,ywmax);
printf("1");
}
else
{
m=(y2-y1)/(x2-x1);
rectangle(xwmin,ywmin,xwmax,ywmax);
line(x1,y1,x2,y2);
setcolor(4);
if(x1<xwmin)
{
y=y1+m*(xwmin-x1);
x=xwmin;
x1=x;
y1=y;
}
if(x2>xwmax)
{
y=y2+m*(xwmax-x2);
x=xwmax;
x2=x;
y2=y;
}
if(y1<ywmin)
{
x=x1+(ywmin-y1)/m;
y=ywmin;
x1=x;
y1=y;
}
if(y2>ywmax)
{
x=x2+(ywmax-y2)/m;
y=ywmax;
x2=x;
y2=y;
}
line(x1,y1,x2,y2);
}
getch();
}
int check(int code1[],int code2[])
{
int flag=0;
if(code1[3]==1&&code2[3]==1)
flag=1;
if(code1[2]==1&&code2[2]==1)
flag=1;
if(code1[1]==1&&code2[1]==1)
flag=1;
if(code1[0]==1&&code2[0]==1)
flag=1;
return flag;
}
void rcode(int x,int y,int code[])
{
if(y>ywmax)//bottom
code[2]=1;
else
code[2]=0;
if(y<ywmin)//top
code[3]=1;
else
code[3]=0;
if(x>xwmax)//right
code[1]=1;
else
code[1]=0;
if(x<xwmin)//left
code[0]=1;
else
code[0]=0;
}
0 Comments