📄 cube2.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 xuatu(float x,float y,float z,int m,int n,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+200+m,pot2[i].y+200+n,pot2[i].color);
else putpixel(pot2[i].x+200+m,pot2[i].y+200+n,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+200+m,pot2[i].y+200+n,pot2[i].color);
else putpixel( pot2[i].x+200+m,pot2[i].y+200+n,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+200+m,pot2[i].y+200+n,pot2[i].color);
else putpixel( pot2[i].x+200+m,pot2[i].y+200+n,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+200+m,pot2[i].y+200+n,pot2[i].color);
else putpixel( pot2[i].x+200+m,pot2[i].y+200+n,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+200+m,pot2[i].y+200+n,pot2[i].color);
else putpixel( pot2[i].x+200+m,pot2[i].y+200+n,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+200+m,pot2[i].y+200+n,pot2[i].color);
else putpixel( pot2[i].x+200+m,pot2[i].y+200+n,0);
}
}break;
}
}
generate1(float x,float y,float z,int m,int n,int s)
{
int i,j,t;
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++)
{ crossx1(&sf0[i].xin,&sf1[i].xin,x);
crossy1(&sf1[i].xin,&sf0[i].xin,y);
crossz1(&sf0[i].xin,&sf1[i].xin,z);
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--)
{ xuatu(x,y,z,m,n,s,sf1[i].index);
/* printf("%d\n",sf1[i].index); */}
}
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");
/*generate1(0,pi/15,0,m,n,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 + -