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

📄 gongzhuanzizhuan2.c

📁 自己做的一个公转自转,希望能够给大家带来帮助.
💻 C
字号:
#include "graphics.h"
#include "math.h"
#include "mem.h"
#define pi 3.1415926
typedef struct {
            int x;
            int y;
            int z;
            int color;
               } point1;

typedef struct {int index;
                point1 xin;}surface;



void crossx(point1 * pot1,point1 * pot2,int n,float x)
{ int i;
  for(i=0;i<=n-1;i++)
  {  pot2[i].y= cos(x)*pot1[i].y-sin(x)*pot1[i].z;
     pot2[i].z= sin(x)*pot1[i].y+cos(x)*pot1[i].z;
     pot2[i].x=pot1[i].x;
     pot2[i].color=pot1[i].color;

   }

}
void crossy(point1 * pot1,point1 * pot2,int n,float y)
{ int i;
  for(i=0;i<=n-1;i++)
  {pot2[i].x=cos(y)* pot1[i].x+sin(y)*pot1[i].z;
   pot2[i].z=-sin(y)*pot1[i].x+cos(y)*pot1[i].z;
   pot2[i].y=pot1[i].y;
   pot2[i].color=pot1[i].color;
   }
}
void crossz(point1 * pot1,point1 * pot2,int n,float z)
{ int i;
  for(i=0;i<=n-1;i++)
   { pot2[i].x=cos(z)*pot1[i].x-sin(z)*pot1[i].y;
     pot2[i].y=sin(z)*pot1[i].x+cos(z)*pot1[i].y;
     pot2[i].z=pot1[i].z;
     pot2[i].color=pot1[i].color;
   }
}
void crossx1(point1 * pot1,point1 * pot2,float x)
{
     pot2->y=cos(x)*pot1->y-sin(x)*pot1->z;
     pot2->z= sin(x)*pot1->y+cos(x)*pot1->z;
     pot2->x=pot1->x;


}
void crossy1(point1 * pot1,point1 * pot2,float y)
{

   pot2->x=cos(y)* pot1->x+sin(y)*pot1->z;
   pot2->z=-sin(y)*pot1->x+cos(y)*pot1->z;
   pot2->y=pot1->y;

}
void crossz1(point1 * pot1,point1 * pot2,float z)
{

     pot2->x= cos(z)*pot1->x-sin(z)*pot1->y;
     pot2->y= sin(z)*pot1->x+cos(z)*pot1->y;
     pot2->z=pot1->z;
}

void huatu(float x,float y,float z,int s,int index)

{   int i,j;
    point1 pot1[2500],pot2[2500];
  switch(index)
 { case 1:
     {   for(i=0;i<50;i++)
         for(j=0;j<50;j++)
       {    pot1[i*50+j].x=i-25;
           pot1[i*50+j].y=j-25;
           pot1[i*50+j].z=25;
           pot1[i*50+j].color=1;
       }

      crossx(pot1,pot2,2500,x);
      crossy(pot2,pot1,2500,y);
      crossz(pot1,pot2,2500,z);
      

       for(i=0;i<2500;i++)
       { if(s==1)putpixel(pot2[i].x+300,pot2[i].y+200,pot2[i].color);
       else putpixel(pot2[i].x+300,pot2[i].y+200,0);
       }
     }break;

  case 2:
     { for(i=0;i<50;i++)
       for(j=0;j<50;j++)

      {
         pot1[i*50+j].x=i-25; pot1[i*50+j].y=j-25;
         pot1[i*50+j].z=-25;  pot1[i*50+j].color=2;
      }
      crossx(pot1,pot2,2500,x);
      crossy(pot2,pot1,2500,y);
      crossz(pot1,pot2,2500,z);
      for(i=0;i<2500;i++)
      { if(s==1) putpixel( pot2[i].x+300,pot2[i].y+200,pot2[i].color);
      else putpixel( pot2[i].x+300,pot2[i].y+200,0);
      }
     }break;
   case 3:
     { for(i=0;i<50;i++)
       for(j=0;j<50;j++)

      {
         pot1[i*50+j].x=-25; pot1[i*50+j].y=i-25;
         pot1[i*50+j].z=j-25;  pot1[i*50+j].color=3;
      }
      crossx(pot1,pot2,2500,x);
      crossy(pot2,pot1,2500,y);
      crossz(pot1,pot2,2500,z);
      for(i=0;i<2500;i++)
      { if(s==1) putpixel( pot2[i].x+300,pot2[i].y+200,pot2[i].color);
      else putpixel( pot2[i].x+300,pot2[i].y+200,0);
      }
     }break;
     case 4:
     { for(i=0;i<50;i++)
       for(j=0;j<50;j++)

      {
         pot1[i*50+j].x=25; pot1[i*50+j].y=i-25;
         pot1[i*50+j].z=j-25;  pot1[i*50+j].color=4;
      }
      crossx(pot1,pot2,2500,x);
      crossy(pot2,pot1,2500,y);
      crossz(pot1,pot2,2500,z);
      for(i=0;i<2500;i++)
      { if(s==1) putpixel( pot2[i].x+300,pot2[i].y+200,pot2[i].color);
      else putpixel( pot2[i].x+300,pot2[i].y+200,0);
      }
     }break;
     case 5:
     { for(i=0;i<50;i++)
       for(j=0;j<50;j++)

      {
         pot1[i*50+j].x=i-25; pot1[i*50+j].y=-25;
         pot1[i*50+j].z=j-25;  pot1[i*50+j].color=5;
      }
      crossx(pot1,pot2,2500,x);
      crossy(pot2,pot1,2500,y);
      crossz(pot1,pot2,2500,z);
      for(i=0;i<2500;i++)
      { if(s==1) putpixel( pot2[i].x+300,pot2[i].y+200,pot2[i].color);
      else putpixel( pot2[i].x+300,pot2[i].y+200,0);
      }
     }break;

     case 6:
     { for(i=0;i<50;i++)
       for(j=0;j<50;j++)

      {
         pot1[i*50+j].x=i-25; pot1[i*50+j].y=25;
         pot1[i*50+j].z=j-25;  pot1[i*50+j].color=6;
      }
      crossx(pot1,pot2,2500,x);
      crossy(pot2,pot1,2500,y);
      crossz(pot1,pot2,2500,z);
      for(i=0;i<2500;i++)
      { if(s==1) putpixel( pot2[i].x+300,pot2[i].y+200,pot2[i].color);
      else putpixel( pot2[i].x+300,pot2[i].y+200,0);
      }
     }break;

 }
}
void huatu2(float x,float y,float o,float o1,int s,int index)

{   int i,j;
    point1 pot1[400],pot2[400];
  switch(index)
 { case 1:
     {   for(i=0;i<20;i++)
         for(j=0;j<20;j++)
       {   pot1[i*20+j].x=i-10;
           pot1[i*20+j].y=j-10;
           pot1[i*20+j].z=10;


        crossz1(&pot1[i*20+j],&pot2[i*20+j],o);
        pot2[i*20+j].x+=200*cos(o1);
        pot2[i*20+j].y+=200*sin(o1);
        pot2[i*20+j].color=7;
        }
      crossx(pot2,pot1,400,x);
      crossy(pot1,pot2,400,y);
       for(i=0;i<400;i++)
       { if(s==1)putpixel(pot2[i].x+300,pot2[i].y+200,pot2[i].color);
       else putpixel(pot2[i].x+300,pot2[i].y+200,0);
       }
     }break;

  case 2:
     { for(i=0;i<20;i++)
       for(j=0;j<20;j++)

      {
         pot1[i*20+j].x=i-10; pot1[i*20+j].y=j-10;
         pot1[i*20+j].z=-10;
        crossz1(&pot1[i*20+j],&pot2[i*20+j],o);
        pot2[i*20+j].x+=200*cos(o1);
        pot2[i*20+j].y+=200*sin(o1);
          pot2[i*20+j].color=8;
      }
      crossx(pot2,pot1,400,x);
      crossy(pot1,pot2,400,y);

      for(i=0;i<400;i++)
      { if(s==1) putpixel( pot2[i].x+300,pot2[i].y+200,pot2[i].color);
      else putpixel( pot2[i].x+300,pot2[i].y+200,0);
      }
     }break;
   case 3:
    { for(i=0;i<20;i++)
       for(j=0;j<20;j++)

      {
         pot1[i*20+j].x=-10; pot1[i*20+j].y=i-10;
         pot1[i*20+j].z=j-10;
         crossz1(&pot1[i*20+j],&pot2[i*20+j],o);
        pot2[i*20+j].x+=200*cos(o1);
        pot2[i*20+j].y+=200*sin(o1);
        pot2[i*20+j].color=9;
      }
      crossx(pot2,pot1,400,x);
      crossy(pot1,pot2,400,y);

      for(i=0;i<400;i++)
      { if(s==1) putpixel( pot2[i].x+300,pot2[i].y+200,pot2[i].color);
      else putpixel( pot2[i].x+300,pot2[i].y+200,0);
      }
     }break;
     case 4:
     { for(i=0;i<20;i++)
       for(j=0;j<20;j++)

      {
         pot1[i*20+j].x=10; pot1[i*20+j].y=i-10;
         pot1[i*20+j].z=j-10;
         crossz1(&pot1[i*20+j],&pot2[i*20+j],o);
        pot2[i*20+j].x+=200*cos(o1);
        pot2[i*20+j].y+=200*sin(o1);
        pot2[i*20+j].color=10;
      }
      crossx(pot2,pot1,400,x);
      crossy(pot1,pot2,400,y);

      for(i=0;i<400;i++)
      { if(s==1) putpixel( pot2[i].x+300,pot2[i].y+200,pot2[i].color);
      else putpixel( pot2[i].x+300,pot2[i].y+200,0);
      }
     }break;
     case 5:
     { for(i=0;i<20;i++)
       for(j=0;j<20;j++)

      {
         pot1[i*20+j].x=i-10; pot1[i*20+j].y=-10;
         pot1[i*20+j].z=j-10;
        crossz1(&pot1[i*20+j],&pot2[i*20+j],o);
        pot2[i*20+j].x+=200*cos(o1);
        pot2[i*20+j].y+=200*sin(o1);
        pot2[i*20+j].color=11;
      }
      crossx(pot2,pot1,400,x);
      crossy(pot1,pot2,400,y);

      for(i=0;i<400;i++)
      { if(s==1) putpixel( pot2[i].x+300,pot2[i].y+200,pot2[i].color);
      else putpixel( pot2[i].x+300,pot2[i].y+200,0);
      }
     }break;

     case 6:
     { for(i=0;i<20;i++)
       for(j=0;j<20;j++)

      {
         pot1[i*20+j].x=i-10; pot1[i*20+j].y=10;
         pot1[i*20+j].z=j-10;
        crossz1(&pot1[i*20+j],&pot2[i*20+j],o);
        pot2[i*20+j].x+=200*cos(o1);
        pot2[i*20+j].y+=200*sin(o1);pot2[i*20+j].color=12;
     }
      crossx(pot2,pot1,400,x);
      crossy(pot1,pot2,400,y);

      for(i=0;i<400;i++)
      { if(s==1) putpixel( pot2[i].x+300,pot2[i].y+200,pot2[i].color);
      else putpixel( pot2[i].x+300,pot2[i].y+200,0);
      }
     }break;

 }
}



void zhongxing(float x,float y,int s)
{
     int i,j,t;

surface sf0[6]={{1,{0,0,25,0}},{2,{0,0,-25,0}},{3,{-25,0,0,0}},{4,{25,0,0,0}},{5,{0,-25,0,0}},{6,{0,25,0,0}}};
surface sf1[6];

     for(i=0;i<6;i++)
     {
        crossx1(&sf0[i].xin,&sf1[i].xin,x);
        crossy1(&sf1[i].xin,&sf0[i].xin,y);
        crossz1(&sf0[i].xin,&sf1[i].xin,0);
        sf1[i].index=sf0[i].index;
}


      for(i=0;i<6;i++)
      for(j=0;j<5;j++)
     {   if(sf1[j].xin.z>=sf1[j+1].xin.z)
         { t=sf1[j].xin.z;
          sf1[j].xin.z=sf1[j+1].xin.z;
          sf1[j+1].xin.z=t;
           t=sf1[j].index;
          sf1[j].index=sf1[j+1].index;
          sf1[j+1].index=t;    }
     }


     for(i=2;i>=0;i--)
     {
     huatu(x,y,0,s,sf1[i].index);
       }

}
void weixing(float x,float y,float o,float o1,int s)
{
     int i,j,t;

surface sf0[6]={{1,{0,0,10,0}},{2,{0,0,-10,0}},{3,{-10,0,0,0}},{4,{10,0,0,0}},{5,{0,-10,0,0}},{6,{0,10,0,0}}};
surface sf1[6];
     for(i=0;i<6;i++)
     {


         crossz1(&sf0[i].xin,&sf1[i].xin,o);

        sf1[i].xin.x+=200*cos(o1);sf1[i].xin.y+=200*sin(o1);



         crossx1(&sf1[i].xin,&sf0[i].xin,x);

         crossy1(&sf0[i].xin,&sf1[i].xin,y); /**/

         sf1[i].index=sf0[i].index;


 /* printf("%d        %d         %d             %d\n",sf1[i].xin.x,sf1[i].xin.y,sf1[i].xin.z,sf1[i].index); */

}

      for(i=0;i<6;i++)
      for(j=0;j<5;j++)
     {   if(sf1[j].xin.z>=sf1[j+1].xin.z)

         { t=sf1[j].xin.z;
          sf1[j].xin.z=sf1[j+1].xin.z;
          sf1[j+1].xin.z=t;
           t=sf1[j].index;
          sf1[j].index=sf1[j+1].index;
          sf1[j+1].index=t;
         }
     }
 for(i=2;i>=0;i--)
     {
        huatu2(x,y,o,o1,s,sf1[i].index);
     }

}

void xuanzhuan(float o,float x,float y,float o1,int s)
{

     int i,j,t;point1 pot={0,0,0},pot1;

     pot.x+=200*cos(o1);pot.y+=200*sin(o1);

     crossx1(&pot,&pot1,x);
     crossy1(&pot1,&pot,y);

     if(pot.z>0) { zhongxing(x,y,s);/**/weixing(x,y,o,o1,s);}
     else { weixing(x,y,o,o1,s);zhongxing(x,y,s);/**/}
}

void main()
{
int driver=DETECT,mode;
float x=0,y=0,o=0,o1=0,key;
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"\\tc");
initgraph(&driver,&mode,"\\tc");
for(;;)
{
o+=pi/5;o1+=pi/50;
  if(x==pi) {x=y=0; }
  if(o1==pi) o1=0;
  if(o==pi)  o=0;

   xuanzhuan(o,x,y,o1,1);

   xuanzhuan(o,x,y,o1,0);

delay(1000);
if(bioskey(1))

   { key=bioskey(0);

   if(key==0x4d00)
     {
       x+=pi/30;
       if(x==pi) x=0;
       /* xuanzhuan(o,x,y,o1,1);xuanzhuan(o,x,y,o1,0); */
     }

   if(key==0x011b)
   {printf("esc be press");
   break;
   }

  if(key==0x4b00)
   {
       x-=pi/30;
       if(x==pi) x=0;

       /* xuanzhuan(o,x,y,o1,1); xuanzhuan(o,x,y,o1,0); */

    }
   if(key==0x5000)
      {

       y+=pi/30;
       if(y==pi) y=0;
        /*xuanzhuan(o,x,y,o1,1);  xuanzhuan(o,x,y,o1,0);  */
         }
    if(key==0x4800)
      {

       y-=pi/30;
       if(y==pi) y=0;
       /* xuanzhuan(o,x,y,o1,1);xuanzhuan(o,x,y,o1,0); */}



 }
 }
 /*weixing();
zhongxing(0.5,0.5,1);
while(!kbhit())

{ x+=pi/35;y+=pi/35;z+=pi/35;
  m+=a;n+=b;
  if(x==pi) {x=y=z=0; }
   if((m==435)&&(a==5))a=-5;
  if((m==-185)&&(a==-5))a=5;

  if((n==210)&&(b==5))b=-5;
  if((n==-180)&&(b==-5))b=5;

   generate1(x,y,0,m,n,1);
   delay(1000);
   generate1(x,y,0,m,n,0);}*/
getch();
closegraph();
}




⌨️ 快捷键说明

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