简单的图形变换
/* WIN-TC BGI 图形编程模板 */#include "Conio.h"
#include "graphics.h"
#define closegr closegraph
#include "math.h"
#define PI 3.1415926
float a_[3]={0},a1_[3]={0},a2_[3]={0},a3_[3]={0};
void initgr(void) /* BGI初始化 */
{
int gd = DETECT, gm = 0; /* 和gd = VGA,gm = VGAHI是同样效果 */
registerbgidriver(EGAVGA_driver);/* 注册BGI驱动后可以不需要.BGI文件的支持运行 */
initgraph(&gd, &gm, "");
}
void line_(float x,float y,float x1,float y1,int color) /*画直线*/
{
float k,t,i,j,t1,t2,n;
t1=y1-y;
t2=x1-x;
if (abs(x1-x)>=1)
{
k=t1/t2;
if (abs(k)<2)
for (i=0;i<abs(x1-x);i+=0.5)
{ if (x1-x>0)
t=x+i;
else t=x-i;
putpixel(t,k*(t-x)+y,color);
}
else for (i=0;i<abs(x1-x);i++)
{
if (x1-x>0&&k>0)
{t=x+i;
n=k*(t-x)+y;
for (j=0;j<=k;j++)
putpixel(t,n+j,color);
}
else if (x1-x>0&&k<0)
{
t=x+i;
n=k*(t-x)+y;
for (j=0;j<=-k;j++)
putpixel(t,n-j,color);
}
else if (x1-x<0&&k>0)
{
t=x-i;
n=k*(t-x)+y;
for (j=0;j<k;j++)
putpixel(t,n-j,color);
}
else if (x1-x&&k<0)
{
t=x-i;
n=k*(t-x)+y;
for (j=0;j<=-k;j++)
putpixel(t,n+j,color);
}
putpixel(t,k*(t-x)+y,color);
}
}
else for (i=0;i<abs(y1-y);i+=0.5)
{
if (y1-y>0)
t=y+i;
else t=y-i;
putpixel(x,t,color);
}
}
void jz(float x[3],float a[3],float b[3][3]) /*矩阵函数*/
{ int i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
x[i]+=a[j]*b[j][i];
}
}
for (i=0;i<3;i++)
{a[i]=x[i];
x[i]=0;
}
}
int main(void)
{ float a[3]={50,50,1},b[3][3]={1,1,0,0,1,0,0,0,1},x[3]={0};
float a1[3]={100,50,1},a2[3]={100,100,1},a3[3]={50,100,1};
float t;
/* float b1[3][3]={1,0,0,0,1,0,30,50,1}; */
int i=0;
initgr(); /* BGI初始化 */
/* line_(a[0],a[1],a1[0],a1[1],3);
line_(a1[0],a1[1],a2[0],a2[1],3);
line_(a2[0],a2[1],a3[0],a3[1],3);
line_(a3[0],a3[1],a[0],a[1],3);
*/
while(!kbhit())
{i++;
t=PI/20;
b[0][0]=cos(t);
b[0][1]=sin(t);
b[1][0]=-sin(t);
b[1][1]=cos(t);
jz(x,a,b);
jz(x,a1,b);
jz(x,a2,b);
jz(x,a3,b);
line_(a[0]+200,a[1]+200,a1[0]+200,a1[1]+200,i);
line_(a1[0]+200,a1[1]+200,a2[0]+200,a2[1]+200,i);
line_(a2[0]+200,a2[1]+200,a3[0]+200,a3[1]+200,i);
line_(a3[0]+200,a3[1]+200,a[0]+200,a[1]+200,i);
delay(1000000000);
/* line_(a[0]+200,a[1]+200,a1[0]+200,a1[1]+200,0);
line_(a1[0]+200,a1[1]+200,a2[0]+200,a2[1]+200,0);
line_(a2[0]+200,a2[1]+200,a3[0]+200,a3[1]+200,0);
line_(a3[0]+200,a3[1]+200,a[0]+200,a[1]+200,0);
*/
}
getch(); /* 暂停一下,看看前面绘图代码的运行结果 */
closegr(); /* 恢复TEXT屏幕模式 */
/* for (i=0;i<2;i++)
printf ("a:%f ",a[i]);
for (i=0;i<2;i++)
printf ("a1:%f ",a1[i]);
for (i=0;i<2;i++)
printf ("a2:%f ",a2[i]);
for (i=0;i<2;i++)
printf ("a3:%f ",a3[i]);
getch();*/
return 0;
}