#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void clip(double*,double*,double*,double*,int *);
int clipt(double denom,double num,double *te,double *tl);
double xmin,ymin,xmax,ymax;
int main()
{
double x0,y0,x1,y1;
int vis;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter coordinates of window:\n");
scanf("%lf%lf%lf%lf",&xmin,&ymin,&xmax,&ymax);
printf("Enter coordinates of line:\n");
scanf("%lf%lf%lf%lf",&x0,&y0,&x1,&y1);
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(x0,y0,x1,y1);
getch();
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
clip(&x0,&y0,&x1,&y1,&vis);
if(vis)
{
line(x0,y0,x1,y1);
}
getch();
getch();
closegraph();
return 0;
}
void clip(double *x0,double *y0,double *x1,double *y1,int *vis)
{
double dx=*x1-*x0;
double dy=*y1-*y0;
double te=0.0,tl=1.0;
*vis=0;
if(clipt(dx,xmin-*x0,&te,&tl))
if(clipt(-dx,*x0-xmax,&te,&tl))
if(clipt(dy,ymin-*y0,&te,&tl))
if(clipt(-dy,*y0-ymax,&te,&tl))
{
*vis=1;
if(tl<1)
{
*x1=*x0+tl*dx;
*y1=*y0+tl*dy;
}
if(te>0)
{
*x0=*x0+te*dx;
*y0=*y0+te*dy;
}
}
return ;
}
int clipt(double denom,double num,double *te,double *tl)
{
double t;
if(denom>0)//PE
{
t=num/denom;
if(t>*tl)
return 0;
else if(t>*te)
*te=t;
}
else if(denom<0)//PL
{
t=num/denom;
if(t<*te)
return 0;
else
*tl=t;
}
else if(num>0)
return 0;
return 1;
}
No comments:
Post a Comment
Have some problem with this code? or Request the code you want if you cant find it in Blog Archive.