此程序用到两个文件,一个是被调用程序affine.c,另一个是主程序cuoqie.c,在运行前请注意自己设置好图形驱动程序的路径。
affine.c如下:
double sin(),cos();
double xmax=639.0,ymax=399.0;
double f[3][3],xx,yy;
scx(xj)
double xj;
{int x;
x=(int)(xj+xmax/2);
return(x);
}
scy(yj)
double yj;
{int y;
y=(int)(ymax-(yj+(ymax/2)));
return(y);
}
parallel(dx,dy)
double dx,dy;
{f[0][0]=1.0;f[0][1]=0.0;f[0][2]=0.0;
f[1][0]=0.0;f[1][1]=1.0;f[1][2]=0.0;
f[2][0]=dx;f[2][1]=dy;f[2][2]=1.0;
}
rotate(theta)
double theta;
{double th;
th=theta/180*3.1415927;
f[0][0]=cos(th);f[0][1]=sin(th);f[0][2]=0.0;f[1][0]=-sin(th);
f[1][1]=cos(th);f[2][0]=0.0;f[2][1]=0.0;f[2][2]=1.0;
}
scale(s)
double s;
{f[0][0]=s;f[0][1]=0.0;f[0][2]=0.0;
f[1][0]=0.0;f[1][1]=s;f[1][2]=0.0;
f[2][0]=0.0;f[2][1]=0.0;f[2][2]=1.0;
}
taisho_x()
{f[0][0]=1.0;f[0][1]=0.0;f[0][2]=0.0;
f[1][0]=0.0;f[1][1]=-1;f[1][2]=0.0;
f[2][0]=0.0;f[2][1]=0.0;f[2][2]=1.0;
}
taisho_y()
{f[0][0]=-1.0;f[0][1]=0.0;f[0][2]=0.0;
f[1][0]=0.0;f[1][1]=1.0;f[1][2]=0.0;
f[2][0]=0.0;f[2][1]=0.0;f[2][2]=1.0;
}
taisho_o()
{f[0][0]=-1.0;f[0][1]=0.0;f[0][2]=0.0;
f[1][0]=0.0;f[1][1]=1.0;f[1][2]=0.0;
f[2][0]=0.0;f[2][1]=0.0;f[2][2]=1.0;
}
taisho_xy()
{f[0][0]=0.0;f[0][1]=0.0;f[0][2]=0.0;
f[1][0]=1.0;f[1][1]=0.0;f[1][2]=0.0;
f[2][0]=0.0;f[2][1]=0.0;f[2][2]=1.0;
}
taishl(aa,bb,cc)
double aa,bb,cc;
{float y,p;
y=bb*bb-aa*aa;
p=aa*aa+bb*bb;
f[0][0]=-y/p;f[2][1]=-2*aa*bb/p;f[2][2]=0.0;
f[1][0]=-2*aa*bb/p;f[1][1]=-y/p;f[1][2]=0.0;
f[2][0]=-2*aa*cc/p;f[2][1]=-2*bb*cc/p;f[2][2]=1.0;
}
axis()
{line(scx(0),scy(-ymax/2),scx(0),scy(ymax/2));
line(scx(-xmax/2),scy(0.0),scx(xmax/2),scy(0.0));
}
tuoq(a,b)
double a,b;
{f[0][0]=1.0;f[0][1]=b;f[0][2]=1.0;
f[1][0]=a;f[1][1]=1.0;f[1][2]=0.0;
f[2][0]=0.0;f[2][1]=0.0;f[2][2]=1.0;
}
affinex(x,y,d)
double x,y,d;
{xx=x*f[0][0]+y*f[1][0]+d*f[2][0];
return(xx);
}
affiney(x,y,d)
double x,y,d;
{yy=x*f[0][1]+y*f[1][1]+d*f[2][1];
return(yy);
}
cuoqie.c如下:
#include<graphics.h>
#include"affine.c"
main()
{int driver=DETECT,mode;
static double x1[]={0.0,10.0,100.0,110.0,0.0};
static double y1[]={0.0,50.0,50.0,-10.0,0.0};
static double x2[5],y2[5];
int i;
double x,xx,yy;
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"");
axis();
for(x=-300;x<=200;x=x+10)
{parallel(x,x/2);
for(i=0;i<=4;i++)
{x2[i]=affinex(x1[i],y1[i],1.0);
y2[i]=affiney(x1[i],y1[i],1.0)/2;
}
for(i=0;i<=3;i++)
{line(scx(x2[i]),scy(y2[i]),scx(x2[i+1]),scy(y2[i+1]));
}
}
getch();
closegraph();
}
运行后将出现坐标系和一系列错切的四边形。
评论