Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Scaling
- #include "bits/stdc++.h"
- #include "graphics.h"
- using namespace std;
- struct Matrix
- {
- int row;
- int col;
- int mat[5][5];
- 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;
- }
- }
- }
- 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++)
- {
- int 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
- {
- int x;
- int y;
- Point(int x = 0, int y = 0)
- : x(x)
- , y(y) {}
- };
- void scale(vector <Point> &points, Point scaleFactor)
- {
- // Triangle Before Scaling
- 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);
- // Initializing scaling matrix
- Matrix scalingMatrix(2, 2);
- scalingMatrix.mat[0][0] = scaleFactor.x;
- scalingMatrix.mat[1][1] = scaleFactor.y;
- // Scaling the triangle
- for (Point &p : points)
- {
- Matrix pointToMatrix = Matrix(2, 1);
- pointToMatrix.mat[0][0] = p.x;
- pointToMatrix.mat[1][0] = p.y;
- Matrix mul = pointToMatrix.multiply(scalingMatrix, pointToMatrix);
- p.x = mul.mat[0][0];
- p.y = mul.mat[1][0];
- }
- // Triangle after Scaling
- 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("scale_in.txt", "r", stdin);
- vector <Point> points(3);
- for (Point &p : points) cin >> p.x >> p.y;
- Point scaleFactor;
- cin >> scaleFactor.x >> scaleFactor.y;
- int gd = DETECT;
- int gm;
- detectgraph(&gd, &gm);
- initgraph(&gd, &gm, "");
- scale(points, scaleFactor);
- getch();
- closegraph();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement