Новичок
Профиль
Группа: Участник
Сообщений: 1
Регистрация: 31.5.2009
Репутация: нет Всего: нет
|
Помогите пожалуйста!!!! Нужно написать программу движения объемной буквы и ее вращения в пространстве!!! | Код | #include <iostream.h> #include <stdio.h> #include <math.h> #include <conio.h> #include <stdlib.h> #include <GRAPHICS.H> #include <dos.h> typedef float TMatrix[4][4]; typedef float TVector[4]; int maxVertexs=24; TVector Vertexs[24]= {{-20,-30,-5,1}, {-20,20,-5,1}, {-10,20,-5,1}, {0,0,-5,1},
{10,20,-5,1}, {20,20,-5,1}, {20,-30,-5,1}, {10,-30,-5,1},
{10,-5,-5,1}, {0,-25,-5,1}, {-10,-5,-5,1}, {-10,-30,-5,1},
{-20,-30,5,1}, {-20,20,5,1}, {-10,20,5,1}, {0,0,5,1},
{10,20,5,1}, {20,20,5,1}, {20,-30,5,1}, {10,-30,5,1},
{10,-5,5,1}, {0,-25,5,1}, {-10,-5,5,1}, {-10,-30,5,1}};
int maxEdges=36; int Edges[36][2]= {{0,1}, {1,2}, {2,3}, {3,4}, {4,5}, {5,6},
{6,7}, {7,8}, {8,9}, {9,10}, {10,11}, {11,0},
{12,13}, {13,14}, {14,15}, {15,16}, {16,17}, {17,18},
{18,19}, {19,20}, {20,21}, {21,22}, {22,23}, {23,12},
{0,12}, {1,13}, {2,14}, {3,15}, {4,16}, {5,17},
{6,18}, {7,19}, {8,20}, {9,21}, {10,22}, {11,23}};
TMatrix prMatrix= { {1,0,0,0}, {0,1,0,0}, {0,0,0,-1/500}, {0,0,0,1}};
int Round(float x) { if ((x-floor(x))>=0.5) {return ceil(x);} else return floor(x); }
void VMMult(TVector V,TMatrix M,TVector &Vr) { TVector Temp; for (int j=0;j<4;j++) { Temp[j]=0; for (int i=0;i<4;i++) Temp[j]=Temp[j]+V[i]*M[i][j]; } Vr[0]=Temp[0]/Temp[3]; Vr[1]=Temp[1]/Temp[3]; Vr[2]=Temp[2]/Temp[3]; Vr[3]=1; } void MMMult(TMatrix A,TMatrix B,TMatrix &Mr) { for (int i=0;i<4;i++) for (int j=0;j<4;j++) { Mr[i][j]=0; for (int k=0;k<4;k++) Mr[i][j]=Mr[i][j]+A[i][k]*B[k][j]; } }
void MoveShape(float dx,float dy,float dz) //{ matrica peremeshenie} { TMatrix T; T[0][0]=1 ; T[0][1]=0 ; T[0][2]=0 ; T[0][3]=0; T[1][0]=0 ; T[1][1]=1 ; T[1][2]=0 ; T[1][3]=0; T[2][0]=0 ; T[2][1]=0 ; T[2][2]=1 ; T[2][3]=0; T[3][0]=dx; T[3][1]=dy; T[3][2]=dz; T[3][3]=1;
for (int i=0;i<24;i++) { VMMult(Vertexs[i],T,Vertexs[i]); } }
void RotateShape(float ax,float ay,float az) // {vrajenie po x} { TMatrix Rx,Ry,Rz,R; float cosa,sina; cosa=cos(ax); sina=sin(ax); Rx[0][0]=1 ; Rx[0][1]=0 ; Rx[0][2]=0 ; Rx[0][3]=0; Rx[1][0]=0 ; Rx[1][1]=cosa ; Rx[1][2]=sina ; Rx[1][3]=0; Rx[2][0]=0 ; Rx[2][1]=-sina; Rx[2][2]=cosa ; Rx[2][3]=0; Rx[3][0]=0 ; Rx[3][1]=0 ; Rx[3][2]=0 ; Rx[3][3]=1;
cosa=cos(ay); // {po y} sina=sin(ay); Ry[0][0]=cosa ; Ry[0][1]=0 ; Ry[0][2]=-sina; Ry[0][3]=0; Ry[1][0]=0 ; Ry[1][1]=1 ; Ry[1][2]=0 ; Ry[1][3]=0; Ry[2][0]=sina ; Ry[2][1]=0 ; Ry[2][2]=cosa ; Ry[2][3]=0; Ry[3][0]=0 ; Ry[3][1]=0 ; Ry[3][2]=0 ; Ry[3][3]=1;
cosa=cos(az); // {po z} sina=sin(az); Rz[0][0]=cosa ; Rz[0][1]=sina ; Rz[0][2]=0 ; Rz[0][3]=0; Rz[1][0]=-sina; Rz[1][1]=cosa ; Rz[1][2]=0 ; Rz[1][3]=0; Rz[2][0]=0 ; Rz[2][1]=0 ; Rz[2][2]=1 ; Rz[2][3]=0; Rz[3][0]=0 ; Rz[3][1]=0 ; Rz[3][2]=0 ; Rz[3][3]=1;
MMMult(Rx,Ry,R); MMMult(R,Rz,R); for (int i=0;i<24;i++) {VMMult(Vertexs[i],R,Vertexs[i]);} }
int GetCoord(TVector p,int num) { if (num==0) { return Round(p[0]+320);} if (num==1) { return Round(240-p[1]);} else return 0; } void DrawWire() { TVector p; for (int e=0;e<36;e++) { VMMult(Vertexs[Edges[e][0]],prMatrix,p); moveto(GetCoord(p,0),GetCoord(p,1));
VMMult(Vertexs[Edges[e][1]],prMatrix,p); lineto(GetCoord(p,0),GetCoord(p,1)); } }
void main() { int gD,gM; char ch; int modekey; int grDriver; int grMode; int ErrCode;
{grDriver=DETECT; initgraph(&grDriver,&grMode,"C:\\Borland\\BORLANDC\\bgi"); ErrCode=graphresult(); if (ErrCode==grOk) {exit; modekey=0;} do { cleardevice(); if (modekey==0) { outtextxy(0,0,""); outtextxy(0,10,"(t,g) - y; (f,h)-x; (r,y)-z - MOVEMENT"); outtextxy(0,20,"p - EXIT"); outtextxy(0,30,"o - ROTATION"); } if (modekey==1) { outtextxy(0,0,""); outtextxy(0,10,"(t,g) - y; (f,h)-x; (r,y)-z - ROTATION"); outtextxy(0,20,"p - EXIT"); outtextxy(0,30,"m - MOVEMENT"); } DrawWire(); ch=getch(); if (modekey==0) { switch(ch) {
case 't': {MoveShape(0,10,0); break;} case 'g': {MoveShape(0,-10,0); break;} case 'f': {MoveShape(-10,0,0); break;} case 'h': {MoveShape(10,0,0); break;} case 'r': {MoveShape(0,0,-10); break;} case 'y': {MoveShape(0,0,10); break;} case 'o': {modekey=1; break;} exit(1); }}
if (modekey==1) { switch(ch) {
case 't': {RotateShape(-M_PI/16,0,0); break;} case 'g': {RotateShape(M_PI/16,0,0); break;} case 'f': {RotateShape(0,-M_PI/16,0); break;} case 'h': {RotateShape(0,M_PI/16,0); break;} case 'r': {RotateShape(0,0,-M_PI/16); break;} case 'y': {RotateShape(0,0,M_PI/16); break;} case 'm': {modekey=0; break;} exit(1); }}
} while (ch!='p'); closegraph(); } }
|
Само движение буквы работает нормально , а когда ее вращаешь выдает ошибку Floating point error:Domain. Abnormal program termination Подскажите в чем проблема.
|