-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoords2.h
93 lines (80 loc) · 2.48 KB
/
Coords2.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
#pragma once
constexpr double pi = 3.14159265358979323846264338327950288;
template<typename T>
class Coords2
{
public:
Vector2<T> xVector, yVector;
Vector2<T> pos;
Coords2(){}
Coords2(const Vector2<T> &_pos, const T angle)
{
xVector = Vector2<T>(cos(angle), sin(angle));
yVector = Vector2<T>(cos(angle + T(pi) / T(2.0)), sin(angle + T(pi) / T(2.0)));
pos = _pos;
}
const Vector2<T> GetPointRelativePos(const Vector2<T> &globalPoint) const
{
Vector2<T> delta = globalPoint - pos;
return Vector2<T>(delta * xVector,
delta * yVector);
}
const Vector2<T> GetAxisRelativeOrientation(const Vector2<T> &globalAxis) const
{
Vector2<T> delta = globalAxis;
return Vector2<T>(delta * xVector,
delta * yVector);
}
const Vector2<T> GetPointGlobalPos (const Vector2<T> &relativePoint) const
{
return pos + xVector * relativePoint.x + yVector * relativePoint.y;
}
const Vector2<T> GetAxisGlobalOrientation (const Vector2<T> &relativeAxis) const
{
return xVector * relativeAxis.x + yVector * relativeAxis.y;
}
const Coords2<T> GetGlobalCoords(const Coords2<T> &localCoords)
{
Coords2<T> res;
res.pos = GetPointGlobalPos(localCoords.pos);
res.xVector = GetAxisGlobalOrientation(localCoords.xVector);
res.yVector = GetAxisGlobalOrientation(localCoords.yVector);
return res;
}
const Coords2<T> GetLocalCoords(const Coords2<T> &globalCoords)
{
Coords2<T> res;
res.pos = GetPointRelativePos(globalCoords.pos);
res.xVector = GetAxisRelativeOrientation(globalCoords.xVector);
res.yVector = GetAxisRelativeOrientation(globalCoords.yVector);
return res;
}
void Identity()
{
xVector = Vector2<T>(1.0f, 0.0f);
yVector = Vector2<T>(0.0f, 1.0f);
pos = Vector2<T>::zeroVector();
}
void SetRotation(const T &angle)
{
xVector = Vector2<T>(cos(angle), sin(angle));
yVector = Vector2<T>(cos(angle + pi / 2.0), sin(angle + pi / 2.0));
}
void Rotate(const T &angle)
{
this->xVector.Rotate(angle);
this->yVector.Rotate(angle);
}
static const Coords2<T> defCoords()
{
Coords2<T> coords;
coords.pos = Vector2<T>::zeroVector();
coords.xVector = Vector2<T>::xAxis();
coords.yVector = Vector2<T>::yAxis();
return coords;
}
};
typedef Coords2<float> Coords2f;
typedef Coords2<double> Coords2d;
typedef Coords2f Coords2f;
typedef Coords2d Coords2d;