-
Notifications
You must be signed in to change notification settings - Fork 1
/
affine.cpp
120 lines (102 loc) · 2.7 KB
/
affine.cpp
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
#include "affine.h"
#include <cassert>
Affine::Affine()
{
transMat = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
};
scaleMat = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
};
refMat = {
{-1, 0, 0},
{0, -1, 0},
{0, 0, 1}
};
rotateMat = {
{0, 0, 0},
{0, 0, 0},
{0, 0, 1}
};
}
QPoint Affine::translate(const QPoint &point, int dx, int dy)
{
setTranslate(dx, dy);
return getPointFromMat(mul(getMatFromPoint(point), transMat));
}
QPoint Affine::rotate(const QPoint &point, int x, int y, int angle)
{
setTranslate(-x, -y);
auto trans = mul(getMatFromPoint(point), transMat);
setRotate(angle);
auto rot = mul(trans, rotateMat);
setTranslate(x, y);
return getPointFromMat(mul(rot, transMat));
}
QPoint Affine::scale(const QPoint &point, float sX, float sY)
{
setScale(sX, sY);
return getPointFromMat(mul(getMatFromPoint(point), scaleMat));
}
QPoint Affine::scale(const QPoint &point, float sX, float sY, int x, int y)
{
QPoint transP = translate(point, -x, -y);
QPoint scaleP = scale(transP, sX, sY);
return translate(scaleP, x, y);
}
QPoint Affine::reflect(const QPoint &point, int x, int y)
{
setReflect(x, y);
return getPointFromMat(mul(getMatFromPoint(point), refMat));
}
vector<vector<double> > Affine::getMatFromPoint(const QPoint &point) const
{
return {
{static_cast<double>(point.x()),
static_cast<double>(point.y()), 1}
};
}
QPoint Affine::getPointFromMat(const vector<vector<double> > &mat) const
{
return QPointF(mat[0][0], mat[0][1]).toPoint();
}
void Affine::setTranslate(int dx, int dy)
{
transMat[2][0] = dx;
transMat[2][1] = dy;
}
void Affine::setScale(float sx, float sy)
{
scaleMat[0][0] = static_cast<double>(sx);
scaleMat[1][1] = static_cast<double>(sy);
}
void Affine::setRotate(int angle)
{
rotateMat[0][0] = std::cos(angle * M_PI/180.0);
rotateMat[0][1] = std::sin(angle * M_PI/180.0);
rotateMat[1][0] = -rotateMat[0][1];
rotateMat[1][1] = rotateMat[0][0];
}
void Affine::setReflect(int x, int y)
{
refMat[2][0] = x << 1;
refMat[2][1] = y << 1;
}
vector<vector<double> > Affine::mul(const vector<vector<double> > &point,
const vector<vector<double> > &mat) const
{
vector<vector<double> > result(point.size(),
vector<double>(mat.front().size()));
for(size_t i = 0; i < result.size(); ++i){
for(size_t j = 0; j < result.front().size(); ++j){
for(size_t k = 0; k < point.front().size(); ++k){
result[i][j] += point[i][k] * mat[k][j];
}
}
}
return result;
}