#include //only sprintf #include #include #include #include #define line(a,b,c,d) line(a+320,b+240,c+320,d+240) double x[8],y[8],z[8],mz; void project() { int tx[8],ty[8],i; // Foreshorten for(i=0;i<8;i++) { tx[i]=x[i]*mz/(z[i]+mz); ty[i]=y[i]*mz/(z[i]+mz); } //Farther face. setcolor(YELLOW); for(i=0;i<3;i++) line(tx[i],ty[i], tx[i+1],ty[i+1]); line(tx[3],ty[3], tx[0],ty[0]); setcolor(RED); //Nearer face. for(i=4;i<7;i++) line(tx[i],ty[i],tx[i+1],ty[i+1]); line(tx[7],ty[7], tx[4],ty[4]); setcolor(WHITE); //Lines between. for(i=0;i<4;i++) line(tx[i],ty[i], tx[i+4],ty[i+4]); } void rotatez(double * x,double * y,double* ,double d) { int i; double nx,ny; d=d*M_PI/180; for(i=0;i<8;i++) { nx=cos(d)*x[i] - sin(d)*y[i]; ny=sin(d)*x[i] + cos(d)*y[i]; x[i]=nx; y[i]=ny; } } void rotatex(double* x,double* y,double* z,double d) { rotatez(y,z,x,d); } void rotatey(double* x,double* y,double* z,double d) { rotatez(z,x,y,d); } main() { int gd=DETECT,gm,hehe=0; char c,st[20]="100"; mz=300.0; x[0]=0; y[0]=0; z[0]=0; x[1]=100; y[1]=0; z[1]=0; x[2]=100; y[2]=100; z[2]=0; x[3]=0; y[3]=100; z[3]=0; x[4]=0; y[4]=0; z[4]=100; x[5]=100; y[5]=0; z[5]=100; x[6]=100; y[6]=100; z[6]=100; x[7]=0; y[7]=100; z[7]=100; initgraph(&gd,&gm,"c:\\tcpp30\\bgi"); /* rotatex(x,y,z,20); rotatey(x,y,z,20); rotatez(x,y,z,20); */ while(1) { while(!kbhit()) { cleardevice(); project(); if(hehe&1) rotatex(x,y,z,2); if(hehe&2) rotatey(x,y,z,2); if(hehe&4) rotatez(x,y,z,2); outtextxy(0,400,st); delay(25); } c=getch(); if(c=='q') return 0; if(c=='x') if(hehe&1) hehe&=6; else hehe|=1; if(c=='y') if(hehe&2) hehe&=5; else hehe|=2; if(c=='z') if(hehe&4) hehe&=3; else hehe|=4; sprintf(st,"%d%d%d",hehe&1?1:0,hehe&2?1:0,hehe&4?1:0); } }