Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. Scaling
  2.  
  3. #include "bits/stdc++.h"
  4. #include "graphics.h"
  5.  
  6. using namespace std;
  7.  
  8. struct Matrix
  9. {
  10.       int row;
  11.       int col;
  12.       int mat[5][5];
  13.  
  14.       Matrix(int row = 0, int col = 0) : row(row), col(col)
  15.       {
  16.             for (int i = 0; i < row; i++)
  17.             {
  18.                   for (int j = 0; j < col; j++)
  19.                   {
  20.                         mat[i][j] = 0;
  21.                   }
  22.             }
  23.       }
  24.       Matrix multiply(const Matrix A, const Matrix B)
  25.       {
  26.             Matrix C;
  27.             C.row = A.row;
  28.             C.col = B.col;
  29.             for (int i = 0; i < A.row; i++)
  30.             {
  31.                   for (int j = 0; j < B.col; j++)
  32.                   {
  33.                         int sum = 0;
  34.                         for (int k = 0; k < A.col; k++)
  35.                         {
  36.                               sum += A.mat[i][k] * B.mat[k][j];
  37.                         }
  38.                         C.mat[i][j] = sum;
  39.                   }
  40.             }
  41.             return C;
  42.       }
  43. };
  44.  
  45. struct Point
  46. {
  47.       int x;
  48.       int y;
  49.       Point(int x = 0, int y = 0)
  50.             : x(x)
  51.             , y(y) {}
  52. };
  53.  
  54. void scale(vector <Point> &points, Point scaleFactor)
  55. {
  56.       // Triangle Before Scaling
  57.       setcolor(RED);
  58.       line(points[0].x, points[0].y, points[1].x, points[1].y);
  59.       line(points[1].x, points[1].y, points[2].x, points[2].y);
  60.       line(points[0].x, points[0].y, points[2].x, points[2].y);
  61.  
  62.       // Initializing scaling matrix
  63.       Matrix scalingMatrix(2, 2);
  64.       scalingMatrix.mat[0][0] = scaleFactor.x;
  65.       scalingMatrix.mat[1][1] = scaleFactor.y;
  66.  
  67.       // Scaling the triangle
  68.       for (Point &p : points)
  69.       {
  70.             Matrix pointToMatrix = Matrix(2, 1);
  71.             pointToMatrix.mat[0][0] = p.x;
  72.             pointToMatrix.mat[1][0] = p.y;
  73.             Matrix mul = pointToMatrix.multiply(scalingMatrix, pointToMatrix);
  74.             p.x = mul.mat[0][0];
  75.             p.y = mul.mat[1][0];
  76.       }
  77.  
  78.       // Triangle after Scaling
  79.       setcolor(GREEN);
  80.       line(points[0].x, points[0].y, points[1].x, points[1].y);
  81.       line(points[1].x, points[1].y, points[2].x, points[2].y);
  82.       line(points[0].x, points[0].y, points[2].x, points[2].y);
  83. }
  84.  
  85. signed main()
  86. {
  87.       freopen("scale_in.txt", "r", stdin);
  88.  
  89.       vector <Point> points(3);
  90.       for (Point &p : points) cin >> p.x >> p.y;
  91.       Point scaleFactor;
  92.       cin >> scaleFactor.x >> scaleFactor.y;
  93.  
  94.       int gd = DETECT;
  95.       int gm;
  96.       detectgraph(&gd, &gm);
  97.       initgraph(&gd, &gm, "");
  98.       scale(points, scaleFactor);
  99.       getch();
  100.       closegraph();
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement