-
Notifications
You must be signed in to change notification settings - Fork 1
/
Line.h
122 lines (90 loc) · 2.28 KB
/
Line.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include "Shape.h"
#include "Matrix.h"
using namespace std;
#ifndef LINE_H
#define LINE_H
class Line : public Shape {
public:
const int N = 2;
const int M = 3;
int angle = 0;
Matrix *mt;
Matrix *dm;
Line(int x1, int y1, int x2, int y2) {
mt = new Matrix();
dm = new Matrix();
mt->createM1(2, 3);
dm->createM1(2, 3);
mt->mas[0][0] = x1;
mt->mas[0][1] = y1;
mt->mas[1][0] = x2;
mt->mas[1][1] = y2;
mt->printMatrix();
}
void draw(SDL_Renderer *renderer) override {
for (int i = 0; i < mt->n; i++) {
for (int j = 0; j < mt->m; j++) {
dm->mas[i][j] = mt->mas[i][j];
}
}
cout << "Angle is " << angle << endl;
rotate(angle);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(renderer,
(mt->mas[0][0]), ceil(mt->mas[0][1]),
(mt->mas[1][0]), ceil(mt->mas[1][1])
);
for (int i = 0; i < mt->n; i++) {
for (int j = 0; j < mt->m; j++) {
mt->mas[i][j] = dm->mas[i][j];
}
}
}
void changeAngle(double an) override {
this->angle += an;
}
void move(double dx, double dy) override{
auto *move = new Matrix();
move->generateMoveMatrix(dx, dy);
mt = mt->mul(move);
mt->printMatrix();
}
void scale(double sx, double sy) override {
auto *scale = new Matrix();
scale->generateScaleMatrix(sx, sy);
moveToZero();
mt = mt->mul(scale);
moveAfter();
mt->printMatrix();
}
void rotate(double angle) override {
auto rotateX = new Matrix();
rotateX->createRotateMatrix(angle);
moveToZero();
mt = mt->mul(rotateX);
moveAfter();
normalizeCoordinates();
mt->printMatrix();
}
private:
double ox, oy;
void moveToZero() {
ox = - (mt->mas[0][0] + mt->mas[1][0]) / 2;
oy = - (mt->mas[0][1] + mt->mas[1][1]) / 2;
move(ox, oy);
ox = -ox;
oy = -oy;
}
void moveAfter() {
move(ox, oy);
}
void normalizeCoordinates() {
for (int i = 0; i < mt->n; i++) {
for (int j = 0; j < mt->m; j++) {
mt->mas[i][j] /= mt->mas[i][mt->m-1];
}
}
}
};
#endif // LINE_H