C++ Program to implement rotation in graphics

In linear algebra, a rotation matrix is a matrix that is used to perform a rotation in Euclidean space. For example the matrix
R = 
\begin{bmatrix}
\cos \theta & -\sin \theta \\
\sin \theta & \cos \theta \\
\end{bmatrix}
rotates points in the xy-Cartesian plane counterclockwise through an angle θ about the origin of the Cartesian coordinate system. To perform the rotation using a rotation matrixR, the position of each point must be represented by a column vector v, containing the coordinates of the point. A rotated vector is obtained by using the matrix multiplication Rv. Since matrix multiplication has no effect on the zero vector (i.e., on the coordinates of the origin), rotation matrices can only be used to describe rotations about the origin of the coordinate system.

Program

#include <math.h>
#include <iostream.h>
#include <conio.h>
#include <graphics.h>
void main()
{
int gd=DETECT,gm,x1,x2,y1,y2,x4,y4;
initgraph(&gd,&gm,"C:\\Tc\\BGI");
float angle=0,ang;
cout<<"\nROTATION OF A LINE\n";
cout<<"Enter the first coordinate of a line:";
cin>>x1>>y1;
cout<<"Enter the second coordinate of a line:";
cin>>x2>>y2;
line(x1,y1,x2,y2);
cout<<"Enter the angle:";
cin>>ang;
angle=(ang*3.14)⁄180;
setcolor(RED);
x4=x2-(((x2-x1)*cos(angle))-((y2-y1)+sin(angle)));
y4=y2-(((x2-x1)*sin(angle))+((y2-y1)*cos(angle)));
line(x2,y2,x4,y4);
getch();
closegraph();
}

0 comments:

Post a Comment