Composite Transformation

What is Composite Transformation?

When we are performing two or more transformations in a geometric figure to create a new figure or shape, we call it a composite transformation.

In the code given below, you will get a choice that which transformation you want to perform in the given figure among Translation, Rotation, and Scaling.





#include bits/stdc++.h ;
#include graphics.h; using namespace std; int gd,gm; //function for rotation void rotation(int side[][3],int n) { int theta,res[100][100]; printf("Enter the rotation angle:"); scanf("%d",&theta); int mat[3][3]={{cos(theta),sin(theta),0},{-sin(theta),cos(theta),0},{0,0,1}}; detectgraph(&gd,&gm); initgraph(&gd,&gm,""); for(int i=0;i<n-1;i++) line(side[i][0],side[i][1],side[i+1][0],side[i+1][1]);\ line(side[n-1][0],side[n-1][1],side[0][0],side[0][1]); for(int i=0;i<n;i++) for(int j=0;j<3;j++) for(int k=0;k<3;k++) res[i][j]+=side[i][k]*mat[k][j]; for(int i=0;i<n-1;i++) line(res[i][0],res[i][1],res[i+1][0],res[i+1][1]); line(res[n-1][0],res[n-1][1],res[0][0],res[0][1]); getch(); } //function for scaling void scaling(int side[][3],int n) { int sx,sy,res[100][100]; printf("Enter the scaling co-ordinates:"); scanf("%d%d",&sx,&sy); int mat[3][3]={{sx,0,0},{0,sy,0},{0,0,1}}; detectgraph(&gd,&gm); initgraph(&gd,&gm,""); for(int i=0;i<n-1;i++) line(side[i][0],side[i][1],side[i+1][0],side[i+1][1]); line(side[n-1][0],side[n-1][1],side[0][0],side[0][1]); for(int i=0;i<n;i++) for(int j=0;j<3;j++) for(int k=0;k<3;k++) res[i][j]+=side[i][k]*mat[k][j]; for(int i=0;i<n-1;i++) line(res[i][0],res[i][1],res[i+1][0],res[i+1][1]); line(res[n-1][0],res[n-1][1],res[0][0],res[0][1]); getch(); } //function for translation void translation(int side[][3],int n) { int tx,ty,res[100][100]; printf("Enter the translation co-ordinates:"); scanf("%d%d",&tx,&ty); int mat[3][3]={{1,0,0},{0,1,0},{tx,ty,1}}; detectgraph(&gd,&gm); initgraph(&gd,&gm,""); for(int i=0;i<n-1;i++) line(side[i][0],side[i][1],side[i+1][0],side[i+1][1]); line(side[n-1][0],side[n-1][1],side[0][0],side[0][1]); for(int i=0;i<n;i++) for(int j=0;j<3;j++) for(int k=0;k<3;k++) res[i][j]+=side[i][k]*mat[k][j]; for(int i=0;i<n-1;i++) line(res[i][0],res[i][1],res[i+1][0],res[i+1][1]); line(res[n-1][0],res[n-1][1],res[0][0],res[0][1]); getch(); }
//driver program
int main()
{ int n,ch; printf("Enter number of sides:"); scanf("%d",&n); int side[n][3]; for(int i=0;i<n;i++) scanf("%d%d%d",&side[i][0],&side[i][1],&side[i][2]); printf("1-Translation\n2-Rotation\n3-Scaling\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: translation(side,n); break; case 2: rotation(side,n); break; case 3: scaling(side,n); break; default: printf("Wrong choice!!"); break; } return 0;
}