⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newmorning.c

📁 计算机图形学
💻 C
📖 第 1 页 / 共 4 页
字号:
      printf("%f\t",m->h[i][j]);
      }
    printf("\n");
}
void  multiple(struct matrix *a,struct matrix *b)
 { int r,c,t; float ab,a1,b1;struct matrix *m;
    for (r=0;r<3;r++)
    for (c=0;c<3;c++)
    { ab=0.0;
     for (t=0;t<3;t++)
       { a1=(a->h[r][t]);  b1=(b->h[t][c]);  ab=a1*b1+ab; }   
        m->h[r][c]=ab;
     }
  for (r=0;r<3;r++)
  for (c=0;c<3;c++)
  b->h[r][c]=m->h[r][c];      
}
void translate(float tx,float ty,struct matrix *thematrix)
{ struct matrix *m;
   setidentity(m);
   m->h[0][2]=tx;   m->h[1][2]=ty;  
   multiple(m,thematrix);  
 }
 void translatee(float tx,float ty,struct matrix *thematrix)
{  struct matrix *m;  
   setidentity(m);
   m->h[0][2]=-tx;   m->h[1][2]=-ty;
   multiple(m,thematrix);  
 }
void scale(float tx,float ty,struct matrix *thematrix)
{ struct matrix *m;
 setidentity(m);
 m->h[0][0]=tx; m->h[1][1]=ty;      
  multiple(m,thematrix);     
 }
 void rotate(float t,struct matrix *thematrix)
 {  float a;struct matrix *m;
    setidentity(m);a=radius( t);
    m->h[0][0]=cos(a);m->h[0][1]=-sin(a);
    m->h[1][0]=sin(a);m->h[1][1]=cos(a);
    multiple(m,thematrix);     
  } 
 void transformpoints(int n,struct wc *pts,struct matrix *thematrix)
 {  int k,j;float a,b,ss1,tt1; 
    for(j=0;j<n;pts++,j++)
    {  a=pts->x; b=pts->y;
       tt1=thematrix->h[0][0]*a+thematrix->h[0][1]*b+thematrix->h[0][2];      
       ss1=thematrix->h[1][0]*a+thematrix->h[1][1]*b+thematrix->h[1][2];
       pts->x=tt1;pts->y=ss1;         
     }  
} 
void setarea(int num,struct wc *pts,int color)
   { int i; 
    for (i=0;i<num-1;pts++,i++)       
    line(pts->x,pts->y,(pts+1)->x,(pts+1)->y);      
    line(pts->x,pts->y,(pts-num+1)->x,(pts-num+1)->y);  
    }
xuanzhuan()
{  int gdriver=DETECT,gmode;
   float angle,x,y,s,t;
   struct wc *pts;
   struct wc a[3]={20,300,120,300,70,230};  
   struct wc re={20,300}; 
   setidentity(thematrix);
    initgraph(&gdriver,&gmode,"");
    printf("please input the angle and s,t,x1,y1");
  scanf("%f,%f,%f",&angle,&s,&t,&x,&y);
   line(20,300,220,300);   line(20,300,20,100); 
   line(20,300,70,230);outtextxy(20,300,"0");
   outtextxy(20,100,"y");outtextxy(220,300,"x");
   line(70,230,120,300);
   translatee(20,300,thematrix);
   rotate(angle,thematrix);
   scale(s,t,thematrix);
   translate(20,300,thematrix);    
   pts=a; transformpoints(3,pts,thematrix);
   setarea(3,pts,BLUE);
   putpixel(20,300,BLUE);
   setidentity(thematrix);   
   getch();
  jingzhimenu();
}

/*************************************************************************/
struct matrix1{ float h[4][4]; };
 struct point3{float x,y,z;  };
 struct point2 {float x,y;};                           
 struct matrix1 *thematrix1;
 struct point2 *pp;                      
 float radius1(float a)
 { return a*3.14/180; } 
 void setidentity1(struct matrix1 *m)
 {
    int i,j;
    for (i=0;i<4;i++)
    for (j=0;j<4;j++)
    m->h[i][j]=(i==j);
}
 void show1(struct matrix1 *m)
{   int i,j;
   for (i=0;i<4;i++)
   { printf("\n");
     for (j=0;j<4;j++)
     printf("%f\t",m->h[i][j]);
   }
 }
void multiple1(struct matrix1 * a,struct matrix1 *b)
{   int r,c,t;float ab,a1,b1;
    struct matrix1 *m;
     setidentity1(m);
   for (r=0;r<4;r++)
    for (c=0;c<4;c++)
    {ab=0.0;
    for (t=0;t<4;t++)
    { a1=(a->h[r][t]);  b1=(b->h[t][c]);   ab=a1*b1+ab;  }   
      m->h[r][c]=ab;
    }
  for (r=0;r<4;r++)
  for (c=0;c<4;c++)
  b->h[r][c]=m->h[r][c];       
}     
 void translate1(float tx,float ty,float tz,struct matrix1 *thematrix1)
 {struct  matrix1 *m;
   setidentity1(m);
   m->h[0][3]=tx;
   m->h[1][3]=ty;
   m->h[2][3]=tz;
   multiple1(m,thematrix1);
 }
 void rotate1(float t,struct matrix1 *thematrix1)
 {float a;
   struct matrix1 *m;
    setidentity(m);
    a=radius(t);
    m->h[0][0]=cos(a);  m->h[0][2]=sin(a);
    m->h[2][0]=-sin(a);    m->h[2][2]=cos(a);
    multiple1(m,thematrix1);
  }
  void toushi(float  distance)                                      
  {  struct  matrix1 *m;
     setidentity1 (m);
     m->h[2][2]=0;   m->h[3][2]=-1/distance;
     show(m);
     multiple1(m,thematrix1);
   }
 void transformpoints1(int n,struct point3 *pts,struct matrix1 *thematrix1)
 {  int k,j;
    float a,b,c,ss1,tt1,qq1,pp1; 
     for(j=0;j<n;pts++,j++)
      {   a=pts->x;  b=pts->y;  c=pts->z;     
          tt1=thematrix1->h[0][0]*a+thematrix1->h[0][1]*b+thematrix1->h[0][2]*c+thematrix1->h[0][3];
          ss1=thematrix1->h[1][0]*a+thematrix1->h[1][1]*b+thematrix1->h[1][2]*c+thematrix1->h[1][3];
          qq1=thematrix1->h[2][0]*a+thematrix1->h[2][1]*b+thematrix1->h[2][2]*c+thematrix1->h[2][3];
          pp1=thematrix1->h[3][0]*a+thematrix1->h[3][1]*b+thematrix1->h[3][2]*c+thematrix1->h[3][3];
          pts->x=tt1/pp1;pts->y=ss1/pp1;
      }     
} 
void draw()
{ line(150,150,250,250);line(150,150,150,30);line(150,150,50,250);                /*画立方体*/
 line(150,90,200,140); line(200,140,200,200);line(200,200,150,250);
 line(150,90,100,140);line(100,140,100,200);line(100,200,150,250);
 line(100,140,150,190);line(150,190,200,140);line(150,190,150,250);
 outtextxy(250,250,"x");outtextxy(150,30,"y");outtextxy(50,250,"z");
 outtextxy(150,150,"o");
 }
void draw1()                                                     /*画透视图*/
{line(300,200,500,200);outtextxy(350,195,"50");outtextxy(400,195,"100");                line(300,500,300,100);outtextxy(295,250,"-50");outtextxy(295,300,"-100");
  outtextxy(295,350,"-150");outtextxy(295,400,"-200");outtextxy(300,200,"0");
  outtextxy(500,200,"X");outtextxy(300,100,"Y");line(350,257,350,314);
  line(300,333,347,386);line(347,386,347,293);line(347,293,300,267);
  line(350,314,404,350);line(404,350,404,276);line(404,276,350,257);
  line(300,333,350,314);line(347,386,404,350);line(300,267,350,257);
  line(347,293,404,276);
 }  

touying()
{int i=DETECT,j;
  struct point3 *p;
 struct point3 a[8]={0,0,0,1,0,0,1,1,0,0,1,0,0,0,1,1,0,1,1,0,1,1};
 initgr();
 setidentity1(thematrix1); rotate1(60.0,thematrix1);
 translate1(0,-2,1,thematrix1);
 toushi(-2); 
 p=a;
 transformpoints1(8,p,thematrix1);
 initgraph(&i,&j,"");
draw();
draw1();
 getch();
 free(p);
  free(thematrix1);
  jingzhimenu();
   }

  /*平移*/
/***********************************************************************/
/**************************************************************************/
 struct matrix2{    float h[3][3];   };            /*定义一个结构体表示矩阵*/
struct wc2{    float x,y; };                      /*定义一个结构体表示点的坐标*/
float radius2(float a)                            /*将角度转化为弧度的函数*/
{ return a*3.14/180;}  
void setidentity2( struct matrix2 *m)                 /*将矩阵单位化*/
 { int i,j;
     for (i=0;i<3;i++)
     for (j=0;j<3;j++)
     m->h[i][j]=(i==j);    
 }
 void  multiple2(struct matrix2 *a,struct matrix2 *b)     /*两个矩阵相乘*/
 { int r,c,t; float ab,a1,b1; struct matrix2 *m;
    for (r=0;r<3;r++)
    for (c=0;c<3;c++)
    {ab=0.0;
           for (t=0;t<3;t++)
            { a1=(a->h[r][t]);  b1=(b->h[t][c]);  ab=a1*b1+ab; }   
           m->h[r][c]=ab;
     }
  for (r=0;r<3;r++)
  for (c=0;c<3;c++)
  b->h[r][c]=m->h[r][c];      
}
void translate2(float tx,float ty,struct matrix2 *thematrix2)    /*平移*/
{ struct matrix2 *m;
   setidentity2(m);
   m->h[0][2]=tx;   m->h[1][2]=ty;  
   multiple2(m,thematrix2);
 }
void scale2(float sx,float sy,struct matrix2 *thematrix2,struct wc2 re)  /*放缩*/
{  struct matrix2 *m;
   setidentity2(m);
      m->h[0][0]=sx; m->h[1][1]=sy;
      m->h[0][2]=(1-sx)*re.x;
      m->h[1][2]=(1-sy)*re.y;      
      multiple2(m,thematrix2);
}
 void rotate2(float t,struct matrix2 *thematrix2,struct wc2 re)    /*旋转*/
 {  float a;
    struct matrix2 *m;
    setidentity2(m);
    a=radius2( t);
    m->h[0][0]=cos(a);m->h[0][1]=-sin(a);
    m->h[1][0]=sin(a);m->h[1][1]=cos(a);
    m->h[0][2]=re.x*(1-cos(a))+re.y*sin(a);
    m->h[1][2]=re.y*(1-cos(a))+re.x*sin(a);  
    multiple2(m,thematrix2);
  } 
 void transformpoints2(int n,struct wc2 *pts,struct matrix2 *thematrix2)   /*最终的坐标点*/
 {  int k,j; float a,b,ss1,tt1;     
    for(j=0;j<n;pts++,j++)
    {  a=pts->x; b=pts->y;
       tt1=thematrix2->h[0][0]*a+thematrix2->h[0][1]*b+thematrix2->h[0][2];      
       ss1=thematrix2->h[1][0]*a+thematrix2->h[1][1]*b+thematrix2->h[1][2];
       pts->x=tt1;pts->y=ss1;     
     }  
} 
void setarea2(int num,struct wc2 *pts,int color)            /*画出图形*/
   { int i; 
       for (i=0;i<num-1;pts++,i++)
            line(pts->x,pts->y,(pts+1)->x,(pts+1)->y);
            line(pts->x,pts->y,(pts-num+1)->x,(pts-num+1)->y);
     }                         
pingyi()
{  int gdriver=DETECT,gmode;
   char c;
   float angle,x,y,s,t;
   struct wc2 *pts;
   struct wc2 a[3]={50.0,50.0,150.0,50.0,100.0,150.0};
   struct wc2 re={100.0,100.0};
   struct matrix2 *thematrix2;
   setidentity2(thematrix2);initgraph(&gdriver,&gmode,"");
   printf("please choose the choice :1. xuanzhuan 2.pingyi 3.fangsuo\n");
   c=getch();
   if (c=='1')
   {printf("please input the angle\n");
   scanf("%f",&angle);
   line(100,150,50,50);
   line(50,50,150,50);                
   line(150,50,100,150); 
   rotate2(angle,thematrix2,re);
   pts=a;
   transformpoints2(3,pts,thematrix2);
   setarea2(3,pts,BLUE);
   putpixel(100,100,BLUE);
   outtextxy(100,100,"(100,100)");
   getch();
   closegraph();
   jingzhimenu();
   }
   if (c=='2')
   { printf("please input the x,y\n");
   scanf("%f,%f",&x,&y);
   line(100,150,50,50);
   line(50,50,150,50);                
   line(150,50,100,150); 
   translate2(x,y,thematrix2);
   pts=a;
   transformpoints2(3,pts,thematrix2);
   setarea2(3,pts,BLUE);
   putpixel(100,100,BLUE);
   outtextxy(100,100,"(100,100)");
   getch();
   closegraph();
   jingzhimenu();
   }
   if (c=='3')
   {printf("please input the s,t\n");
   scanf("%f,%f",&s,&t);
   line(100,150,50,50);
   line(50,50,150,50);                
   line(150,50,100,150); 
   scale2(s,t,thematrix2,re);
   pts=a;
   transformpoints2(3,pts,thematrix2);
   setarea2(3,pts,BLUE);
   putpixel(100,100,BLUE);
   outtextxy(100,100,"(100,100)");
   getch();
   closegraph();
   jingzhimenu();
   }

   getch();
   closegraph();

  }


/***********************************************************************/
/**************************************************************************/



/*****6. 曲线的生成******/
/*****Hermite曲线的生成******/

void DrawHermiteCurve(int x1,int y1,int x2,int y2,int xr1,int yr1,int xr2,int yr2,int color)
  {

   int i, oldx, oldy, m1, m2, m3, m4, x, y, k1, k2;

   oldx = x1;
   oldy = y1;
   m1 = Iterative3;
   m2 = m3 = m4 = 0;
   for (i=0; i<Iterative; i++) 
     {
      k1 = (i << 1) + 1;
      k2 = (k1+i)*i + k1;
      m4 += (k2 -= (k1 *= Iterative));
      m3 += (k1 = k2 - k1) + Iterative2;
      m2 -= (k2 += k1);
      m1 += k2;
      x = (int) (((long) x1*m1 + (long) x2*m2 + (long) xr1*m3 +
                  (long) xr2*m4) / Iterative3);
      y = (int) (((long) y1*m1 + (long) y2*m2 + (long) yr1*m3 +
                  (long) yr2*m4) / Iterative3);
      setcolor(color);
      line(oldx,oldy,x,y);
      oldx = x;
      oldy = y;
     }
  }

void DrawBezierCurve(int x1,int y1,int x2,int y2,int xr1,int yr1,int xr2,int yr2,int color)
  {

   DrawHermiteCurve(x1,y1,x2,y2,3*(xr1-x1),3*(yr1-y1),3*(x2-xr2),3*(y2-yr2),color);  /*braizer。曲线*/
  }
hemit()
{
int x1,x2,y1,y2,xr1,xr2,yr1,yr2;
 initgr();

  /* BGI初始化 */
  x1=200;
  y1=150;
  x2=300;

  y2=450;
  xr1=100;
  yr1=200;

  xr2=400 ;
  yr2=300;
   outtextxy(50,50,"This is hermite curve and Bezier curve!");
  DrawHermiteCurve( x1, y1, x2, y2, xr1, yr1, xr2, yr2,2) ;
  getch();
  DrawHermiteCurve(x1,y1,x2,y2,3*(xr1-x1),3*(yr1-y1),3*(x2-xr2),3*(y2-yr2),1);
  getch();
jingzhimenu();}

/****************************************************************************/
/*三次Bezier曲线的生成*/
void DisplayCubicBezierCurve3(Vector P[4],int count)
{
  float C[2][4],t,deltat;
  Vector V,newV;
  int i,j;

  for(j=0;j<2;j++)
     {
       C[j][0]=P[0][j];/*P[0][j]-2*P[1][j]+P[2][j];*/
       C[j][1]=-3*P[0][j]+3*P[1][j];
       C[j][2]=3*P[0][j]-6*P[1][j]+3*P[2][j];
       C[j][3]=-1*P[0][j]+3*P[1][j]-3*P[2][j]+P[3][j];

     }

  V[X]=C[X][0];V[Y]=C[Y][0];
  deltat=1.0/count;
  t=0.0;
  for(i=1;i<=count;i++)
    {
      t+=deltat;
      newV[X]=C[X][0]+t*C[X][1]+t*t*C[X][2]+t*t*t*C[X][3];
      newV[Y]=C[Y][0]+t*C[Y][1]+t*t*C[Y][2]+t*t*t*C[Y][3];
      line(V[X],V[Y],newV[X],newV[Y]);
      V[X]=newV[X];  V[Y]=newV[Y];
    }
}

/****************************************************************/

/****************************************************************/
/*二次Bezier曲线的生成*/

void DisplayCubicBezierCurve2(Vector P[3],int count)
{
  float C[2][3],t,deltat;
  Vector V,newV;
  int i,j;

  for(j=0;j<2;j++)
     {
       C[j][0]=P[0][j];/*P[0][j]-2*P[1][j]+P[2][j];*/
       C[j][1]=-2*P[0][j]+2*P[1][j];
       C[j][2]=P[0][j]-2*P[1][j]+P[2][j];

     }

  V[X]=C[X][0];V[Y]=C[Y][0];
  deltat=1.0/count;
  t=0.0;
  for(i=1;i<=count;i++)
    {
      t+=deltat;
      newV[X]=C[X][0]+t*C[X][1]+t*t*C[X][2];
      newV[Y]=C[Y][0]+t*C[Y][1]+t*t*C[Y][2];
      setcolor(YELLOW);
      line(V[X],V[Y],newV[X],newV[Y]);
      V[X]=newV[X];V[Y]=newV[Y];
    }
}
/****************************************************************/



 brzie()
{
  int count;
  Vector P1[4]={{50,400},{100,200},{300,150},{400,350}};
  Vector P2[3]={{50,400},{100,200},{300,150}};
  count=2000;
  initgr(); /* BGI初始化 */
  setcolor(2);
  outtextxy(50,50,"This is Bezier 2 curve!");
  line(50,400,100,200);
  getch();
  setcolor(5);
  line(100,200,300,150);
  getch();
  setcolor(BLUE);
  line(300,150,400,350);
  getch();
  setcolor(YELLOW);
  DisplayCubicBezierCurve2(P2,count);
  getch();
  outtextxy(50,50,"This is Bezier 3 curve!");
  DisplayCubicBezierCurve3(P1,count);
  getch();
   /* 暂停一下,看看前面绘图代码的运行结果 */
 jingzhimenu();
}




/***************************************************/
/*******************************************************/




/*主程序开始*/
/***************************************************/
/*******************************************************/
main()
{
char a,b;
menu();
a=getch();
while(a!='e'&&a!='E')
{if(a=='Z'||a=='z')
midpoint();
else if(a=='y'||a=='Y')
{ b=getch();
 if(b=='m'||b=='M')
midyuan();
else if(b=='z'||b=='Z')
zhengfuyuan();
}
else if(a=='t'||a=='T')
{ b=getch();
 if(b=='m'||b=='M')
midtuoyuan();
else if(b=='w'||b=='W')
weibutuoyuan();
}
else if(a=='c'||a=='C')
caijian();
else if(a=='D'||a=='d')
zifu();
else if(a=='p'||a=='P')
PaoWuxian();
else if(a=='A'||a=='a')
tianchong();
else if(a=='q'||a=='Q')
{b=getch();
if(b=='b'||b=='B')
brzie();
if(b=='h'||b=='H')
hemit();}
else if(a=='x'||a=='X')
xuanzhuan();
else if(a=='s'||a=='S')
touying();
else if(a=='l'||a=='L')
pingyi();
else
{
settextstyle(3,1,6);
outtextxy(500,250,"ERROR!");
}
a=getch();
}
end();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -