## Rotation tranformation in C graphics

Program Description

Rotation transformation in C graphics.
The program demonstrates how to perform rotation transformation of a given object (using C/C++ graphics) with respect to a specific point along with source code.

The object co-ordinates are taken from the user and rotation transformation matrix is used to obtain the new image co-ordinates of the transformed object.
The program prompts the user for number of vertices in the polygon and takes their vertex co-ordinates in a cyclic order.It then asks the user for amount of rotation(angle in degrees) with respect to a specific point given by the user.
The user is expected to enter the appropriate angle of rotation(+ve anticlockwise,-ve clockwise). The final image of the rotated object along with original object is drawn.

Program
```/*********************************************
*    Rotation tranformation in C graphics
*
*    code.cheraus.com
*
**********************************************/

#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<dos.h>

//function declarations
void mul(float mat[3][3],float vertex[10][3],int n);
void rotate(float vertex[10][3],int n);
void init(float vertex[10][3],int n);

int main()
{
int i,x,y;

float vertex[10][3];
int n;

//clear the screen
clrscr();

//taking inputs for the polygon object
cout<<"Enter the no. of vertex : ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the points (x,y): ";
cin>>x>>y;
vertex[i][0]=x;
vertex[i][1]=y;
vertex[i][2]=1;
}

//calling the rotate() function to rotate the given object
rotate(vertex,n);

getch();

return 0;
}

/*
Function definition for init()

Function: init(). This function initialises the screen in graphics mode and
loads the appropriate graphics driver.  It also draws the
co-ordinate axes,legends, and the image of the given object matrix.

Input: 1) Object matrix (to be rotated later)
2) Number of vertices(rows in object matrix)

Returns: This function does not return anything

*/

void init(float vertex[10][3],int n)
{

//initialisation of the graphics driver
int gd=DETECT,gm,i;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");

//drawing the co-ordinate axes
setcolor(10);
line(0,240,640,240);      //drawing X axis
line(320,0,320,480);      //drawing Y axis

//drawing graph legends
setcolor(3);
line(450,20,490,20);
setcolor(15);
line(450,50,490,50);
setcolor(6);
outtextxy(350,20,"Original");
outtextxy(350,50,"Transformed");

//drawing the object
setcolor(3);
for(i=0;i<n-1;i++)
{
line(320+vertex[i][0],240-vertex[i][1],320+vertex[i+1][0],240-vertex[i+1][1]);
}
line(320+vertex[n-1][0],240-vertex[n-1][1],320+vertex[0][0],240-vertex[0][1]);

}

/*
Function definition for mul()

Function: mul().This function multiplies the two given matrices(object matrix and
rotation transformation matrix) and then draws the final image.

Input: 1) rotation transformation matrix
2) Object matrix (to be rotated)
3) Number of vertices (number of rows in object matrix)

Returns: This function does not return anything

*/
void mul(float mat[3][3],float vertex[10][3],int n)
{
int i,j,k;    // loop variables

//array for storing final image matrix
float res[10][3];

//multiplying the object matrix with rotation transformation matrix
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{ res[i][j]=0;
for(k=0;k<3;k++)
{
res[i][j] = res[i][j] + vertex[i][k]*mat[k][j];
}
}
}

//drawing the transformed image (rotated object)
setcolor(15);
for(i=0;i<n-1;i++)
{
line(320+res[i][0],240-res[i][1],320+res[i+1][0],240-res[i+1][1]);
}
line(320+res[n-1][0],240-res[n-1][1],320+res[0][0],240-res[0][1]);

}

/*
Function definition for rotate()

Function: rotate(). This function rotates the given object in the desired direction
with respect to the desired point and displays the transformed
image.Amount of rotation and the reference point is taken from
the user.

Inputs: 1)Object matrix
2)Number of vertices in the object

Returns: This function does not return anything

*/

void rotate(float vertex[][3],int n)
{
//rotation transformation matrix
float rot[3][3];

float theta,xp,yp,a;

//taking reference point
cout<<"\nEnter the point of rotation (x,y): ";
cin>>xp>>yp;

//taking abgle of rotation
cout<<"Enter the angle of rotation: ";
cin>>theta;

//converting angle in degrees to radians
a=(theta*3.142)/180;

//initializing the rotation transformation matrix as per the required information
rot[0][0]=cos(a);
rot[1][0]=-1*sin(a);
rot[2][0]=-xp*cos(a)+yp*sin(a)+xp;
rot[0][1]=sin(a);
rot[1][1]=cos(a);
rot[2][1]=-xp*sin(a)-yp*cos(a)+yp;
rot[0][2]=0;
rot[1][2]=0;
rot[2][2]=1;

//initializing the graphics mode and drawing the original object
init(vertex,n);

//multiplying the object with rotation transformation matrix
//	and displaying the scaled image
mul(rot,vertex,n);

}
```

Output
Output Image of Getting Data From User

Final Output