孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

例程1
#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;
  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();
}


例程2
#include <graphics.h>
#include "affine.c"

main()
{
  int graphdriver=DETECT,graphmode;
  static double x1[]={0.0,10.0,100.0,110.0,0.0};
  static double y1[]={0.0,50.0,50.0,0.0,0.0};
  static double x2[5],y2[5];
  int i;
  double r,xx,yy;
  initgraph(&graphdriver,&graphmode,"");
  for (r=0;r<=360;r+=10)
  {
    rotate(r);
    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();
}

例程3
#include <graphics.h>
#include <math.h>
#include "affine.c"

main()
{
  int driver=DETECT,mode;
  static double x1[]={-20.0,0.0,30.0,10.0,-20.0};
  static double y1[]={0.0,17.0,-10.0,-20.0,0.0};
  static double x2[5],y2[5];
  int i;
  double r;
  initgraph(&driver,&mode,"");
  axis();
  for (r=0;r<=360;r+=10)
  {
    rotate(r);
    for (i=0;i<=4;i++)
    {
      x2[i]=affinex(x1[i],y1[i],1.0);
      y2[i]=affiney(x1[i],y1[i],1.0);
    }
    parallel(cos(r/180*3.1415926)*150.0,sin(r/180*3.1415926)*100.0);
    for (i=0;i<=4;i++)
    {
      x2[i]=affinex(x2[i],y2[i],1.0);
      y2[i]=affiney(x2[i],y2[i],1.0);
      line(scx(x2[i]),scy(y2[i]),scx(x2[i+1]),scy(y2[i+1]));
    }
    for (i=0;i<=3;i++)
    {
      line(scx(x2[i]),scy(y2[i]),scx(x2[i+1]),scy(y2[i+1]));
    }
  }
  getch();
  closegraph();
}


其中用到的affine.c
double sin(),cos();
double xmax=639.0,ymax=399.0;
double f[3][3],xx,yy;

scx(xj)      //将实际的x坐标转换为屏幕坐标
double xj;
{
  int x;
  x=(int)(xj+xmax/2);
  return x;
}

scy(yi)     //将实际的y坐标转换为屏幕坐标
double yi;
{
  int y;
  y=ymax-(int)(yi+(ymax/2));
  return y;
}

parallel(dx,dy)   //完成平移变换,dx,dy分别为x轴方向和y轴方向的平移量
double dx,dy;
{
  f[0][0]=1.0;  f[0][1]=0.0;   f[0][2]=0.0;
  f[1][0]=0.0;  f[0][1]=1.0;   f[1][2]=0.0;
  f[2][0]=dx;   f[2][1]=dy;    f[2][2]=1.0;
}

rotate(theta)    //以原点为中心的旋转变换,旋转角度为theta
double theta;
{
  double th;
  th=theta/180*3.1415926;
  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[1][2]=0.0;
  f[2][0]=0.0;  f[2][1]=0.0;  f[2][2]=1.0;
}

scale(s)
double s;   //比例变换,比例系数为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()   //对称变换,对称轴为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.0;   f[1][2]=0.0;
  f[2][0]=0.0;   f[2][1]=0.0;   f[2][2]=1.0;
}

taisho_y()    //对称变换,对称轴为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]=1.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()    //对称变换,对称轴为x=y
{
  f[0][0]=0.0;  f[0][1]=1.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)    //对称变换,对称轴为任意直线

{
  float y,p;
  y=bb*bb-aa*aa;
  p=aa*aa+bb*bb;
  f[0][0]=-y/p; f[0][1]=-2*aa*bb/p;    f[0][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()     //坐标变换为屏幕中心为原点,座和上分别为x轴和y轴的正方向
{
  line(scx(0),scy(-ymax/2),scx(0),scy(ymax/2));
  line(scx(-xmax),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]=0.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);
}

posted on 2011-03-05 20:44  孤独的猫  阅读(416)  评论(0编辑  收藏  举报