Tuesday, March 25, 2014

C++ program to implement 2D Transformations

Description :Transformations are used to position objects, to shape objects, to change viewing positions, and even to change how something is viewed.
There are 4 main types of transformations that one can perform in 2 dimensions:-translations,scaling,rotation,shearing.


Code :
#include<iostream.h>
#include<stdlib.h>
#include<dos.h>
#include<conio.h>

#include<graphics.h>
#include<math.h>
class POLYGON
{
private:
int p[10][10],Result[10][10],X[10][10];
public:
int accept_poly(int [][10]);
void draw_poly(int [][10],int);
void draw_polyfloat(float [][10],int);
void matmult(int [][10],int [][10],int,int,int,int [][10]);
void matmultfloat(float [][10],int [][10],int,int,int,float [][10]);
void shearing(int [][10],int);
void scaling(int [][10],int);
void rotation(int [][10],int);
void translation(int [][10],int);
void reflection(int [][10],int);
};
int POLYGON :: accept_poly(int p[][10])
{
int i,n;
cout << “\n\n\t\tEnter no.of vertices:”;
cin >> n;
for(i=0;i<n;i++)
{
cout << “\n\n\t\tEnter (x,y)Co-ordinate of point P” << i << “: “;
cin >> p[0][i] >> p[1][i];
p[2][i] = 1;
}
p[0][n] = p[0][0];
p[1][n] = p[1][0];
p[2][n] = 1;
for(i=0;i<n;i++)
{
cout<<”\n”;
for(int j=0;j<3;j++)
{
cout<<p[i][j]<<”\t”;
}
}
getch();
return n;
}
void POLYGON :: draw_poly(int p[][10], int n)
{
int i,gd = DETECT,gm;
initgraph(&gd,&gm,”e:\\TC\\BGI”);
line(320,0,320,480);
line(0,240,640,240);
for(i=0;i<n;i++)
{
if(i!=n-1)
line(p[0][i]+320, -p[1][i]+240, p[0][i+1]+320, -p[1][i+1]+240);
else
line(p[0][i]+320, -p[1][i]+240, p[0][0]+320, -p[1][0]+240);
}
getch();
closegraph();
}
void POLYGON :: draw_polyfloat(float p[][10], int n)
{
int i,gd = DETECT,gm;
initgraph(&gd,&gm,”e:\\TC\\BGI”);
line(320,0,320,480);
line(0,240,640,240);
for(i=0;i<n;i++)
{
if(i!=n-1)
line(int(p[0][i])+320, -int(p[1][i])+240, int(p[0][i+1])+320, -int(p[1][i+1])+240);
else
line(int(p[0][i])+320, -int(p[1][i])+240, int(p[0][0])+320, -int(p[1][0])+240);
}
getch();
closegraph();
}
void POLYGON :: matmult(int mat1[][10],int mat2[][10],int r1,int c1,int c2,int mat3[][10])
{
int i,j,k;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
mat3[i][j] = 0;
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
for(k=0;k<c1;k++)
mat3[i][j] = mat3[i][j]+(mat1[i][k] * mat2[k][j]);
mat3[c2][0] = mat3[0][0];
mat3[c2][1] = mat3[0][1];
mat3[c2][2] = mat3[0][2];
}
void POLYGON :: matmultfloat(float mat1[][10],int mat2[][10],int r1,int c1,int c2,float mat3[][10])
{
int i,j,k;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
mat3[i][j] = 0;
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
for(k=0;k<c1;k++)
mat3[i][j] = mat3[i][j]+(mat1[i][k] * mat2[k][j]);
mat3[c2][0] = mat3[0][0];
mat3[c2][1] = mat3[0][1];
mat3[c2][2] = mat3[0][2];
}
void POLYGON :: translation(int p[10][10],int n)
{
int tx,ty,i,j;
cout << “\n\n\t\tEnter X-Translation tx: “;
cin >> tx;
cout << “\n\n\t\tEnter Y-Translation ty: “;
cin >> ty;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
X[i][j] = 0;
X[0][0] = X[1][1] = X[2][2] = 1;
X[0][2] = tx;
X[1][2] = ty;
matmult(X,p,3,3,n,Result);
cout << “\n\n\t\tPolygon after Translation…”;
draw_poly(Result,n);
}
void POLYGON :: reflection(int p[][10],int n)
{
int type,i,j;
cout << “\n\n **** Reflection Types ****”;
cout << “\n\n\t\t1.About X-Axis \n\n\t\t2.About Y-Axis \n\n\t\t3.About Origin \
\n\n\t\t4.About Line y = x \n\n\t\t5.About Line y = -x \
\n\n\t\tEnter your choice(1-5): “;
cin >> type;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
if(i == j)
X[i][j] = 1;
else
X[i][j] = 0;
}
switch(type)
{
case 1:
X[1][1] = -1;
break;
case 2:
X[0][0] = -1;
break;
case 3:
X[1][1] = -1;
X[0][0] = -1;
break;
case 4:
X[1][1] = 0;
X[0][0] = 0;
X[1][0] = 1;
X[0][1] = 1;
break;
case 5:
X[1][1] = 0;
X[0][0] = 0;
X[1][0] = -1;
X[0][1] = -1;
break;
}
matmult(X,p,3,3,n,Result);
cout << “\n\n\t\tPolygon after Reflection…”;
draw_poly(Result,n);
}
void POLYGON :: rotation(int p[][10],int n)
{
float type,rotate[10][10],result[10][10],i,j,Ang,Sinang,Cosang;
cout << “\n\n\t\tEnter the angle of rotation in degrees: “;
cin >> Ang;
cout << “\n\n **** Rotation Types ****”;
cout << “\n\n\t\t1.Clockwise Rotation \n\n\t\t2.Anti-Clockwise Rotation “;
cout << “\n\n\t\tEnter your choice(1-2): “;
cin >> type;
Ang = (Ang * 6.2832)/360;
Sinang = sin(Ang);
Cosang = cos(Ang);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
rotate[i][j] = 0;
rotate[0][0] = rotate[1][1] = Cosang;
rotate[0][1] = rotate[1][0] = Sinang;
rotate[2][2] = 1;
if(type == 1)
rotate[1][0] = -Sinang;
else
rotate[0][1] = Sinang;
matmultfloat(rotate,p,3,3,n,result);
cout << “\n\n\t\tPolygon after Rotation…”;
draw_polyfloat(result,n);
}
void POLYGON :: scaling(int p[][10],int n)
{
float Sx,Sy,result[10][10],scale[10][10],i,j;
cout << “\n\n\t\tEnter X-Scaling Sx: “;
cin >> Sx;
cout << “\n\n\t\tEnter Y-Scaling Sy: “;
cin >> Sy;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scale[i][j] = 0;
scale[0][0] = Sx;
scale[1][1] = Sy;
scale[2][2] = 1;
matmultfloat(scale,p,3,3,n,result);
cout << “\n\n\t\tPolygon after Scaling…”;
draw_polyfloat(result,n);
}
void POLYGON :: shearing(int p[][10],int n)
{
int Sx,Sy,type,i,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;
}
cout << “\n\n **** Shearing Types ****”;
cout << “\n\n\t\t1.X-Direction Shear \n\n\t\t2.Y-Direction Shear “;
cout << “\n\n\t\tEnter your choice(1-2): “;
cin >> type;
if(type == 1)
{
cout << “\n\n\t\tEnter X-Shear Sx: “;
cin >> Sx;
X[0][1] = Sx;
}
else
{
cout << “\n\n\t\tEnter Y-Shear Sy: “;
cin >> Sy;
X[1][0] = Sy;
}
matmult(X,p,3,3,n,Result);
cout << “\n\n\t\tPolygon after Shearing…”;
draw_poly(Result,n);
}
int menu()
{
int ch;
clrscr();
cout << “\n\n **** 2-D TRANSFORMATION ****”;
cout << “\n\n\t\t1.Translation \n\n\t\t2.Scaling \n\n\t\t3.Rotation \
\
n\n\t\t4.Reflection \n\n\t\t5.Shearing \n\n\t\t6.Exit”;
cout<<”\n\n\tEnter your choice(1-6): “;
cin >> ch;
return ch;
}
void main()
{
int ch,n,p[10][10];
POLYGON p1;
clrscr();
cout << “\n\n **** 2-D TRANSFORMATION ****”;
n = p1.accept_poly(p);
clrscr();
cout << “\n\n\t\tOriginal Polygon …”;
p1.draw_poly(p,n);
do
{
ch = menu();
switch(ch)
{
case 1:
p1.translation(p,n);
break;
case 2:
p1.scaling(p,n);
break;
case 3:
p1.rotation(p,n);
break;
case 4:
p1.reflection(p,n);
break;
case 5:
p1.shearing(p,n);
break;
case 6:
exit(0);
}
}while(ch!=6);
getch();
}

No comments:

Post a Comment