四月 27, 2011

Dev C++3D方塊


#include "conio.h"
#include "graphics.h"
#include "stdio.h"
#include "math.h"

int device=DETECT,mode,px[8],py[8],mlen=100;
double mx,my,mz,mpi=atan(1.)/45*180;

int main()
{ double mdgx=0,mdgy=0,mdgz=45;
char mkey=-1;
int getpx(int sx,int sy,int sz);
int getpy(int sx,int sy,int sz);
void showbox();

// initgraph(&device,&mode,"c:tcbgi");
initwindow(640,480);
while(mkey!=27)
{ setcolor(15);
line(320,0,320,240);
line(320,240,639,240);
line(320,240,81,479);

if(mkey!=0)
{ mx=mdgx/180*mpi;
my=mdgy/180*mpi;
mz=mdgz/180*mpi;

showbox();
}
if(kbhit())
{ mkey=getch();
if(mkey==0)
mkey=getch();
}
else
mkey=0;
switch(mkey) {
case 75:
mdgy=(mdgy-5);
break;
case 77:
mdgy=(mdgy+5);
break;
case 72:
mdgx=(mdgx-5);
break;
case 80:
mdgx=(mdgx+5);
break;
}
}
closegraph();
return 0;
}

int getpx(int sx,int sy,int sz)
{ double x1,y1,z1,x2,y2;
int px;

x1 = sx*cos(my)+sz*sin(my);
y1 = sy;
z1 = -sx*sin(my)+sz*cos(my);
x2 = x1;
y2 = y1*cos(mx)-z1*sin(mx);
px = x2*cos(mz)-y2*sin(mz);
/* py = x2*sin(mz)+y2*cos(mz); */
return px+320;
}

int getpy(int sx,int sy,int sz)
{ double x1,y1,z1,x2,y2;
int py;

x1 = sx*cos(my)+sz*sin(my);
y1 = sy;
z1 = -sx*sin(my)+sz*cos(my);
x2 = x1;
y2 = y1*cos(mx)-z1*sin(mx);
/* px = x2*cos(mz)-y2*sin(mz); */
py = x2*sin(mz)+y2*cos(mz);
return py+240;
}

void showbox()
{ px[0]=getpx(-mlen,-mlen,-mlen);
py[0]=getpy(-mlen,-mlen,-mlen);
px[1]=getpx(-mlen,-mlen,mlen);
py[1]=getpy(-mlen,-mlen,mlen);
px[2]=getpx(-mlen,mlen,-mlen);
py[2]=getpy(-mlen,mlen,-mlen);
px[3]=getpx(-mlen,mlen,mlen);
py[3]=getpy(-mlen,mlen,mlen);
px[4]=getpx(mlen,-mlen,-mlen);
py[4]=getpy(mlen,-mlen,-mlen);
px[5]=getpx(mlen,-mlen,mlen);
py[5]=getpy(mlen,-mlen,mlen);
px[6]=getpx(mlen,mlen,-mlen);
py[6]=getpy(mlen,mlen,-mlen);
px[7]=getpx(mlen,mlen,mlen);
py[7]=getpy(mlen,mlen,mlen);

cleardevice();
setcolor(LIGHTRED);
line(px[0],py[0],px[1],py[1]);
line(px[0],py[0],px[2],py[2]);
line(px[1],py[1],px[3],py[3]);
line(px[2],py[2],px[3],py[3]);
setcolor(LIGHTGREEN);
line(px[4],py[4],px[5],py[5]);
line(px[4],py[4],px[6],py[6]);
line(px[5],py[5],px[7],py[7]);
line(px[6],py[6],px[7],py[7]);
setcolor(LIGHTBLUE);
line(px[0],py[0],px[4],py[4]);
line(px[1],py[1],px[5],py[5]);
line(px[2],py[2],px[6],py[6]);
line(px[3],py[3],px[7],py[7]);

}


Posted by superman at 02:08  迴響 (0)引用 (0)C語言教學

引用URL

http://blog2.cyhs.tp.edu.tw/teacher/trackback.php?id=2757
回應文章