2013-09-30 3 views
0

J'ai le programme C++ suivant. Je dois faire pivoter un triangle autour d'un point à un certain angle dans le sens inverse des aiguilles d'une montre. J'y arrive en suivant le code. ma logique est que d'abord je saisis le point sur lequel la rotation se produit, l'angle de rotation, puis les coordonnées du triangle. après que j'ai appliqué la formule de matriceterminaison anormale du programme (rotation du triangle)

M(final coordinates)=M(translate triangle to original position)*M(Rotate triangle)*M(translate triangle to origin)*M(original coordinates) 

où M se réfère à la matrice dans la formule ci-dessus. En multipliant cette matrice dans l'ordre inverse < - quelqu'un a obtenu le résultat. Mais quand j'exécute ce code, le programme mène à une terminaison anormale.

Mon code est:

#include<iostream.h> 
#include<conio.h> 
#include<graphics.h> 
#include<math.h> 
#define pi 3.14159265 
void rotation(float tx,float ty,float redi,float t[][3]) 
{ 
clrscr(); 
int g=DETECT,gm; 
initgraph(&g,&gm,"C:\\TC\\BGI"); 
setbkcolor(8); 
int i,j,k; 
float x[3][3],c[3][3],r[3][3],mf[3][3],res[3][3]; 
for(j=0;j<3;j++) 
{ 
    for(k=0;k<3;k++) 
    { 
     if(j==k) 
     { 
      r[j][k]=1; 
      x[j][k]=1; 
     } 
     else 
     { 
      r[j][k]=0; 
      x[j][k]=0; 
     } 
    } 
} 
float co,si; 
co=cos(redi); 
si=sin(redi); 
r[0][0]=co; r[0][1]=-si; r[1][0]=si; r[1][1]=co; 

x[0][2]=-tx; 
x[1][2]=-ty; 
for(int mat=0;mat<3;mat++) 
{ 
    for(i=0;i<3;i++) 
    { 
     for(j=0;j<3;j++) 
     { 
      if(mat==0) c[i][j]=0; 
      if(mat==1) mf[i][j]=0; 
      if(mat==2) res[i][j]=0; 
      for(k=0;k<3;k++) 
      { 
       if(mat==0) 
        c[i][j]+=r[i][k]*x[k][j]; 
       if(mat==1) 
       { 
        x[0][2]=tx; 
        x[1][2]=ty; 
        mf[i][j]+=x[i][k]*c[k][j]; 
       } 
       if(mat==2) 
        res[i][j]+=mf[i][k]*t[k][j]; 

      } 
      cout<<res[i][j]; 
     } 
    } 
} 
line(res[0][0],res[1][0],res[0][1],res[1][1]); 
line(t[0][0],t[1][0],t[0][1],t[1][1]); 
line(res[0][2],res[1][2],res[0][1],res[1][1]); 
line(t[0][2],t[1][2],t[0][1],t[1][1]); 
line(res[0][2],res[1][2],res[0][0],res[1][0]); 
line(t[0][2],t[1][2],t[0][0],t[1][0]); 
for(i=0;i<3;i++) 
{ 
    putpixel(res[0][i],res[1][i],RED); 
    putpixel(t[0][i],t[1][i],RED); 
} 
closegraph(); 
getch(); 

} 
void main() 
{ 
clrscr(); 
float tx,ty,t[3][3]; 
float deg,redi; 
cout<<"Rotation about point : "; 
cin>>tx>>ty; 
cout<<"Enter angle of rotation :"; 
cin>>deg; 
for(int i=0;i<3;i++) 
{ 
    cout<<"Enter co-ordintes "<<i+1<<" of triangle: "; 
    for(int j=0;j<3;j++) 
    { 
     if(j==2) 
      t[j][i]=1; 
     else 
      cin>>t[j][i]; 
    } 
} 
redi=(deg*pi)/180.0; 
rotation(tx,ty,redi,t); 
getch(); 
} 

J'utilise TC++ IDE. s'il vous plaît aider ......

+0

Qu'est-ce que dit débogueur? Une ligne spécifique où elle se bloque systématiquement? – NotAgain

+0

ERREUR DE POINT FLOTTANT: DOMAINE TERMINAISON ABNORMALE – AmanS

Répondre

1

Je pense qu'il ya un problème dans vos ces boucles check it out

for(int mat=0;mat<3;mat++) 
{ 
    for(i=0;i<3;i++) 
    { 
    for(j=0;j<3;j++) 
    { 
     if(mat==0) c[i][j]=0; 
     if(mat==1) mf[i][j]=0; 
     if(mat==2) res[i][j]=0; 
     for(k=0;k<3;k++) 
     { 
      if(mat==0) 
       c[i][j]+=r[i][k]*x[k][j]; 
      if(mat==1) 
      { 
       x[0][2]=tx; 
       x[1][2]=ty; 
       mf[i][j]+=x[i][k]*c[k][j]; 
      } 
      if(mat==2) 
       res[i][j]+=mf[i][k]*t[k][j]; 

     } 
     cout<<res[i][j]; 
    } 
    } 
} 

multiplier vos matrices un par un (utiliser différentes boucle pour chaque multiplication) et exécutez votre programme à nouveau.

OU

utilisent ce code modifié:

#include<iostream.h> 
#include<conio.h> 
#include<graphics.h> 
#include<math.h> 
void main() 
{ 
clrscr(); 
int gd=DETECT,gm; 
initgraph(&gd,&gm,"C:\\TC\\BGI"); 
float x[3][3],y[3][3],z[3][3],p[3][3],q[3][3]; 
float angle,ptx,pty; 
int i,j,k; 
cout<<"Enter coordinates of triangle:"; 
for(i=0;i<3;i++) 
{ 
    for(j=0;j<2;j++) 
    { 
     cin>>y[j][i]; 
    } 
} 
y[2][0]=1; 
y[2][1]=1; 
y[2][2]=1; 
cout<<"Enter the point about:"; 
cin>>ptx>>pty; 
for(i=0;i<3;i++) 
{ 
    for(j=0;j<3;j++) 
    { 
     if(i==j) 
      x[i][j]=1; 
     else 
      x[i][j]=0; 
    } 
} 
x[0][2]=-ptx; 
x[1][2]=-pty; 
for(i=0;i<3;i++) 
{ 
    for(j=0;j<3;j++) 
    { 
     z[i][j]=0; 
     for(int k=0;k<3;k++) 
     { 
      z[i][j]+=x[i][k]*y[k][j]; 
     } 
    } 
} 
cout<<"Enter angle of rotation:"; 
cin>>angle; 
angle*=(3.14/180); 
for(i=0;i<3;i++) 
{ for(j=0;j<3;j++) 
    { 
     if(i==j) 
      x[i][j]=1; 
     else 
      x[i][j]=0; 
    } 
} 
x[0][0]=cos(angle); 
x[0][1]=-sin(angle); 
x[1][0]=sin(angle); 
x[1][1]=cos(angle); 
for(i=0;i<3;i++) 
{ 
    for(j=0;j<3;j++) 
    { 
     p[i][j]=0; 
     for(int k=0;k<3;k++) 
     { 
      p[i][j]+=(x[i][k]*z[k][j]); 
     } 
    } 
} 
for(i=0;i<3;i++) 
{ 
    for(j=0;j<3;j++) 
    { 
     if(i==j) 
      x[i][j]=1; 
     else 
      x[i][j]=0; 
    } 
} 
x[0][2]=ptx; 
x[1][2]=pty; 
for(i=0;i<3;i++) 
{ 
    for(j=0;j<3;j++) 
    { 
     q[i][j]=0; 
     for(int k=0;k<3;k++) 
     { 
      q[i][j]+=(x[i][k]*p[k][j]); 
     } 
    } 
} 
clrscr(); 
line(y[0][0],y[1][0],y[0][1],y[1][1]); 
line(y[0][0],y[1][0],y[0][2],y[1][2]); 
line(y[0][1],y[1][1],y[0][2],y[1][2]); 

line(q[0][0],q[1][0],q[0][1],q[1][1]); 
line(q[0][0],q[1][0],q[0][2],q[1][2]); 
line(q[0][1],q[1][1],q[0][2],q[1][2]); 
getch(); 
} 
+0

ouais son travail ....... merci – AmanS