Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Rotation
- #include "bits/stdc++.h"
- #include "graphics.h"
- using namespace std;
- #define PI 2 * acos(0.0)
- #define toRad(deg) deg * PI / 180
- struct Matrix
- {
- int row;
- int col;
- double mat[4][4];
- Matrix(int row = 0, int col = 0) : row(row), col(col)
- {
- for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) mat[i][j] = 0.0;
- }
- Matrix multiply(const Matrix &A, const Matrix &B)
- {
- Matrix C;
- C.row = A.row;
- C.col = B.col;
- for (int i = 0; i < A.row; i++)
- {
- for (int j = 0; j < B.col; j++)
- {
- double sum = 0;
- for (int k = 0; k < A.col; k++)
- {
- sum += A.mat[i][k] * B.mat[k][j];
- }
- C.mat[i][j] = sum;
- }
- }
- return C;
- }
- };
- struct Point
- {
- double x;
- double y;
- Point(double x = 0, double y = 0)
- : x(x)
- , y(y) {}
- };
- void rotat(vector <Point> &points, Point pivotPoint, double angle)
- {
- // display object before Rotation
- setcolor(RED);
- line(points[0].x, points[0].y, points[1].x, points[1].y);
- line(points[1].x, points[1].y, points[2].x, points[2].y);
- line(points[0].x, points[0].y, points[2].x, points[2].y);
- Matrix rotaionMatrix(2, 2);
- rotaionMatrix.mat[0][0] = cos(toRad(angle));
- rotaionMatrix.mat[0][1] = -sin(toRad(angle));
- rotaionMatrix.mat[1][0] = sin(toRad(angle));
- rotaionMatrix.mat[1][1] = cos(toRad(angle));
- for (Point &p : points)
- {
- // Shifting the pivot point to the origin
- // and the given points accordingly
- Point shifted;
- shifted.x = p.x - pivotPoint.x;
- shifted.y = p.y - pivotPoint.y;
- Matrix shiftedMatrix(2, 1);
- shiftedMatrix.mat[0][0] = shifted.x;
- shiftedMatrix.mat[1][0] = shifted.y;
- Matrix rotatedMatrix = shiftedMatrix.multiply(rotaionMatrix, shiftedMatrix);
- Point rotatedPoint;
- rotatedPoint.x = rotatedMatrix.mat[0][0];
- rotatedPoint.y = rotatedMatrix.mat[1][0];
- // shift it back
- rotatedPoint.x = rotatedPoint.x + pivotPoint.x;
- rotatedPoint.y = rotatedPoint.y + pivotPoint.y;
- p.x = rotatedPoint.x;
- p.y = rotatedPoint.y;
- }
- // display object after rotation
- setcolor(GREEN);
- line(points[0].x, points[0].y, points[1].x, points[1].y);
- line(points[1].x, points[1].y, points[2].x, points[2].y);
- line(points[0].x, points[0].y, points[2].x, points[2].y);
- }
- signed main()
- {
- freopen("rotation_in.txt", "r", stdin);
- vector <Point> points(3);
- for (Point &p : points) cin >> p.x >> p.y;
- Point pivotPoint;
- cin >> pivotPoint.x >> pivotPoint.y;
- double angle;
- cin >> angle;
- int gd = DETECT;
- int gm;
- detectgraph(&gd, &gm);
- initgraph(&gd, &gm, "");
- rotat(points, pivotPoint, angle);
- getch();
- closegraph();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement