计算机图形学 几何图形变换算法(缩放、旋转和平移) C语言编写

#include"graphics.h"
#include "stdafx.h"
#include "math.h"
#include "iostream.h"
  
 double xmax=639.0,ymax=399.0;
 double f[3][3],xx,yy;
 int scx(double xj){
   int x;
   x=(int)(xj+xmax/2);
   return(x);
 }
 int scy(double yi){
   int y;
   y=int(ymax-(int)(yi+(ymax/2)));
   return(y);
 }
void transfer(double dx,double 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;
 }
 
 void rotate(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;
 }
 
void scale(double ss){
 f[0][0]=ss;f[0][1]=0.0;f[0][2]=0.0;
    f[1][0]=0.0;f[1][1]=ss;f[1][2]=0.0;
    f[2][0]=0.0;f[2][1]=0.0;f[2][2]=1.0;
 }
 
void 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));
 }
 
double affinex(double x,double y,double d){
   xx=x*f[0][0]+y*f[1][0]+d*f[2][0];
   return(xx);
 }
 double affiney(double x,double y,double d){
   yy=x*f[0][1]+y*f[1][1]+d*f[2][1];
   return(yy);
 }

int main(){
 
 int driver=DETECT,mode,i,r=60;
    static double x1[]={0.0,10.0,80.0,100.0,0.0};
    static double y1[]={0.0,50.0,50.0,0.0,0.0};
    static double x2[5],y2[5],x3[5],y3[5],x4[5],y4[5];
    initgraph(&driver,&mode,"");
    axis();
    for(i=0;i<=3;i++){
  line(scx(x1[i]),scy(y1[i]),scx(x1[i+1]),scy(y1[i+1]));
 }
    scale(2.0);
    for(i=0;i<=4;i++){
     x2[i]=affinex(x1[i],y1[i],1.0);
        y2[i]=affiney(x1[i],y1[i],1.0);
       Sleep(100);
    }
    for(i=0;i<=3;i++){
  line(scx(x2[i]),scy(y2[i]),scx(x2[i+1]),scy(y2[i+1]));
  
    }
    rotate(6.0);
    for(i=0;i<=4;i++){
     x3[i]=affinex(x2[i],y2[i],1.0);
        y3[i]=affiney(x2[i],y2[i],1.0);
        Sleep(100);
    }
    for(i=0;i<=3;i++){
  line(scx(x3[i]),scy(y3[i]),scx(x3[i+1]),scy(y3[i+1]));
    }
    transfer(50.0,-150.0);
    for(i=0;i<=4;i++){
  x4[i]=affinex(x3[i],y3[i],1.0);
        y4[i]=affiney(x3[i],y3[i],1.0);
        Sleep(100);
    }
    for(i=0;i<=3;i++){
  line(scx(x4[i]),scy(y4[i]),scx(x4[i+1]),scy(y4[i+1]));
    }
    getchar();
    closegraph();
 
 return 0;
 
}

你可能感兴趣的:(图形学)