# c++ - program's(rotation of triangle) abnormal termination

i have following c++ program. i have to rotate a triangle about some point at some angle in anticlockwise direction. i'm achieving this by following code.

my logic is that firstly i'm inputting the point about which rotation is occur ,rotation angle and then co-ordinates of triangle. after that i applied the matrix formula

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

where M refers to matrix in above formula.

by multiplying these matrix in reverse order that is <-- someone got the result.

but when i run this code the program leads to abnormal termination.

My code is:

``#include<iostream.h>#include<conio.h>#include<graphics.h>#include<math.h>#define pi 3.14159265void 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;elsecin>>t[j][i];}}redi=(deg*pi)/180.0;rotation(tx,ty,redi,t);getch();}``

I'm using TC++ IDE.

i think there is some problem in your these loops 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];
}
}
}
``````

multiply your matrices one by one(use different loop for each multiplication) and run your program again.

OR

use this modified code:

``````#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;
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();
}
``````