📄 cube.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;
}
}
generate1(float x,float y,float z,int m,int n,int s)
{
int i,j,t;
point1 pot1[2500],pot2[2500];
surface sf0[6]={{1,{0,0,25}},{2,{0,0,-25}},{3,{-25,0,0}},{4,{25,0,0}},{5,{0,-25,0}},{6,{0,25,0}}};
surface sf1[6];
for(i=0;i<6;i++)
{ crossx(&sf0[i].xin,&sf1[i].xin,1,x);
crossy(&sf0[i].xin,&sf1[i].xin,1,y);
crossz(&sf0[i].xin,&sf1[i].xin,1,z); }
for(i=0;i<6;i++)
for(j=0;j<5-i;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].xin.z=t;
}
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,pot2,2500,y);
crossz(pot2,pot2,2500,z);
for(i=0;i<2500;i++)
{ if(s==1)putpixel(pot2[i].x+200+m,pot2[i].y+200+n,pot2[i].color);
else putpixel(pot2[i].x+200+m,pot2[i].y+200+n,0);
}
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,pot2,2500,y);
crossz(pot2,pot2,2500,z);
for(i=0;i<2500;i++)
{ if(s==1) putpixel( pot2[i].x+200+m,pot2[i].y+200+n,pot2[i].color);
else putpixel( pot2[i].x+200+m,pot2[i].y+200+n,0);
}
}
void main()
{
int driver=DETECT,mode;
float x=0,y=0,z=0; int m=0,n=0,a=5,b=5,s=0;
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"\\tc");
initgraph(&driver,&mode,"\\tc");
while(!kbhit())
{ x+=pi/30;y+=pi/30;
m+=a;n+=b;
if(x==pi) {x=y=z=0; }
if((m==435)&&(a==5))a=-5;
if((m==-200)&&(a==-5))a=5;
if((n==210)&&(b==5))b=-5;
if((n==-180)&&(b==-5))b=5;
generate1(x,y,z,m,n,1);
delay(1000);
generate1(x,y,z,m,n,0);}
getch();
closegraph();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -