-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transform.cpp
55 lines (47 loc) · 1.25 KB
/
Transform.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
#include <iostream>
#include <cmath>
#include "CImg.h"
#include "Transform.h"
Transformation::Transformation(const SqMatrix& mat): curMatrix(mat) {
if (curMatrix.rowCount() != 3)
throw "Unable to handle matrices of dimension != 3.";
}
void Transformation::translate(double x, double y) {
curMatrix(0,2) += x;
curMatrix(1,2) += y;
}
void Transformation::rotate(double theta) {
SqMatrix mat = SqMatrix::identity(3);
mat(0,0) = std::cos(theta);
mat(0,1) = -std::sin(theta);
mat(1,0) = std::sin(theta);
mat(1,1) = std::cos(theta);
curMatrix *= mat;
}
std::pair<double, double> Transformation::transformCoord(double x, double y, bool debug) const {
Matrix m2(3, 1);
m2(0,0) = x;
m2(1,0) = y;
m2(2,0) = 1;
Matrix result(curMatrix * m2);
result(0,0) /= result(2,0);
result(1,0) /= result(2,0);
result(2,0) /= result(2,0);
if (debug) {
std::cout<<"==========\n";
std::cout<<"Input: ("<<x<<", "<<y<<")\n";
std::cout<<"CurMatrix:\n";
curMatrix.print();
std::cout<<"------\n";
std::cout<<"Multiplier\n";
m2.print();
std::cout<<"------\n";
std::cout<<"Result:\n";
result.print();
std::cout<<"======\n\n";
}
return std::make_pair(result(0,0), result(1,0));
}
Transformation Transformation::inverse() const {
return curMatrix.inverse();
}