-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransform.h
115 lines (102 loc) · 2.81 KB
/
Transform.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
#ifndef TRANSFORM_H
#define TRANSFORM_H
#include "gdiplus.h"
#include "objidl.h"
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
using namespace Gdiplus;
class Transform {
public:
double translateX = 0, translateY = 0;
double rotateAngle = 0, rotateCenterX = 0, rotateCenterY = 0;
double scaleX = 1, scaleY = 1;
GraphicsState save;
void applyTransform(Graphics &g);
void parseTransform(const string &content);
void resetTransform(Graphics &g);
void appleMultipleTransforms(Graphics &g);
};
void Transform::applyTransform(Graphics &g) {
save = g.Save();
Matrix matrix;
if (translateX != 0 || translateY != 0) {
matrix.Translate(translateX, translateY);
}
if (rotateAngle != 0) {
matrix.RotateAt(rotateAngle, PointF(rotateCenterX, rotateCenterY));
}
if (scaleX != 1 || scaleY != 1) {
matrix.Scale(scaleX, scaleY);
}
g.SetTransform(&matrix);
}
void Transform::parseTransform(const string &content) {
if (content == "") {
return;
}
istringstream stream(content);
string token;
while (std::getline(stream, token, ')')) {
size_t pos = token.find('(');
if (pos == std::string::npos)
continue;
std::string type = token.substr(0, pos);
if (type[0] == ' ') {
type = type.substr(1);
}
std::string params = token.substr(pos + 1);
std::istringstream paramStream(params);
std::vector<double> values;
double value;
while (paramStream >> value) {
values.push_back(value);
if (paramStream.peek() == ',')
paramStream.ignore();
}
if (type == "translate") {
translateX = values[0];
if (values.size() > 1)
translateY = values[1];
} else if (type == "rotate") {
rotateAngle = values[0];
if (values.size() > 2) {
rotateCenterX = values[1];
rotateCenterY = values[2];
}
} else if (type == "scale") {
scaleX = values[0];
if (values.size() > 1)
scaleY = values[1];
else
scaleY = scaleX;
} else if (type == "matrix") {
translateX = values[4];
translateY = values[5];
scaleX = values[0];
scaleY = values[3];
rotateAngle = atan2(values[1], values[0]) * 180 / 3.14159265358979323846;
} else {
/*throw std::runtime_error("Unsupported transform type: " + type);*/
}
}
}
void Transform::resetTransform(Graphics &g) {
g.Restore(save);
translateX = translateY = 0;
rotateAngle = rotateCenterX = rotateCenterY = 0;
scaleX = scaleY = 1;
}
void Transform::appleMultipleTransforms(Graphics &g) {
save = g.Save();
Matrix matrix;
matrix.Translate(translateX, translateY);
matrix.RotateAt(rotateAngle, PointF(rotateCenterX, rotateCenterY));
matrix.Scale(scaleX, scaleY);
g.MultiplyTransform(&matrix);
}
#endif