You are on page 1of 2

#include<iostream.

h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int radius = 100;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xa,ya,xb,yb;
float t,maxenter,minleave;
maxenter=0.0;
minleave=1.0;
cout<<"enter rectangle\n";
cin>>xmin>>ymin>>xmax>>ymax;
rectangle(xmin,ymin,xmax,ymax);
cout<<"enter end points";
cin>>x1>>y1>>x2>>y2;
line(x1,y1,x2,y2);
int p[4],q[4];
p[0]=x1-x2;
q[0]=x1-xmin;
p[1]=x2-x1;
q[1]=xmax-x1;
p[2]=y1-y2;
q[2]=y1-ymin;
p[3]=y2-y1;
q[3]=ymax-y1;
for(int i=0;i<4;i++)
{
if(p[i]<0)
{
t=1.0*q[i]/p[i];
if(t>maxenter)
maxenter=t;
}
if(p[i]>0)
{
t=1.0*q[i]/p[i];
if(t<minleave)
minleave=t;

}
}
xa=x1+maxenter*(x2-x1);
ya=y1+maxenter*(y2-y1);
xb=x1+minleave*(x2-x1);
yb=x1+minleave*(y2-y1);
setcolor(RED);
line(xa,ya,xb,yb);
getch();
closegraph();
}

You might also like