Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. Rotation
  2.  
  3. #include "bits/stdc++.h"
  4. #include "graphics.h"
  5.  
  6. using namespace std;
  7.  
  8. #define PI                 2 * acos(0.0)
  9. #define toRad(deg)         deg * PI / 180
  10.  
  11. struct Matrix
  12. {
  13.       int row;
  14.       int col;
  15.       double mat[4][4];
  16.  
  17.       Matrix(int row = 0, int col = 0) : row(row), col(col)
  18.       {
  19.             for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) mat[i][j] = 0.0;
  20.       }
  21.       Matrix multiply(const Matrix &A, const Matrix &B)
  22.       {
  23.             Matrix C;
  24.             C.row = A.row;
  25.             C.col = B.col;
  26.             for (int i = 0; i < A.row; i++)
  27.             {
  28.                   for (int j = 0; j < B.col; j++)
  29.                   {
  30.                         double sum = 0;
  31.                         for (int k = 0; k < A.col; k++)
  32.                         {
  33.                               sum += A.mat[i][k] * B.mat[k][j];
  34.                         }
  35.                         C.mat[i][j] = sum;
  36.                   }
  37.             }
  38.             return C;
  39.       }
  40. };
  41.  
  42. struct Point
  43. {
  44.       double x;
  45.       double y;
  46.       Point(double x = 0, double y = 0)
  47.             : x(x)
  48.             , y(y) {}
  49. };
  50.  
  51. void rotat(vector <Point> &points, Point pivotPoint, double angle)
  52. {
  53.       // display object before Rotation
  54.       setcolor(RED);
  55.       line(points[0].x, points[0].y, points[1].x, points[1].y);
  56.       line(points[1].x, points[1].y, points[2].x, points[2].y);
  57.       line(points[0].x, points[0].y, points[2].x, points[2].y);
  58.  
  59.       Matrix rotaionMatrix(2, 2);
  60.       rotaionMatrix.mat[0][0] = cos(toRad(angle));
  61.       rotaionMatrix.mat[0][1] = -sin(toRad(angle));
  62.       rotaionMatrix.mat[1][0] = sin(toRad(angle));
  63.       rotaionMatrix.mat[1][1] = cos(toRad(angle));
  64.  
  65.       for (Point &p : points)
  66.       {
  67.             // Shifting the pivot point to the origin
  68.             // and the given points accordingly
  69.             Point shifted;
  70.             shifted.x = p.x - pivotPoint.x;
  71.             shifted.y = p.y - pivotPoint.y;
  72.  
  73.             Matrix shiftedMatrix(2, 1);
  74.             shiftedMatrix.mat[0][0] = shifted.x;
  75.             shiftedMatrix.mat[1][0] = shifted.y;
  76.  
  77.             Matrix rotatedMatrix = shiftedMatrix.multiply(rotaionMatrix, shiftedMatrix);
  78.  
  79.             Point rotatedPoint;
  80.             rotatedPoint.x = rotatedMatrix.mat[0][0];
  81.             rotatedPoint.y = rotatedMatrix.mat[1][0];
  82.  
  83.             // shift it back
  84.             rotatedPoint.x = rotatedPoint.x + pivotPoint.x;
  85.             rotatedPoint.y = rotatedPoint.y + pivotPoint.y;
  86.  
  87.             p.x = rotatedPoint.x;
  88.             p.y = rotatedPoint.y;
  89.       }
  90.       // display object after rotation
  91.       setcolor(GREEN);
  92.       line(points[0].x, points[0].y, points[1].x, points[1].y);
  93.       line(points[1].x, points[1].y, points[2].x, points[2].y);
  94.       line(points[0].x, points[0].y, points[2].x, points[2].y);
  95. }
  96.  
  97.  
  98. signed main()
  99. {
  100.       freopen("rotation_in.txt", "r", stdin);
  101.  
  102.       vector <Point> points(3);
  103.       for (Point &p : points) cin >> p.x >> p.y;
  104.       Point pivotPoint;
  105.       cin >> pivotPoint.x >> pivotPoint.y;
  106.       double angle;
  107.       cin >> angle;
  108.  
  109.       int gd = DETECT;
  110.       int gm;
  111.       detectgraph(&gd, &gm);
  112.       initgraph(&gd, &gm, "");
  113.       rotat(points, pivotPoint, angle);
  114.       getch();
  115.       closegraph();
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement