-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoint.h
More file actions
235 lines (233 loc) · 13.6 KB
/
Copy pathPoint.h
File metadata and controls
235 lines (233 loc) · 13.6 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Point.h
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright (c) 2011-2024 Adrian Maurer. All rights reserved.
// Distributed under the MIT software license (http://www.opensource.org/licenses/mit-license.php).
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#ifndef AMA_POINT_H
#define AMA_POINT_H
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <cmath>
#include <concepts>
#include "Constant.h"
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
namespace ama {
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Point<T>
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
class Point
{
// ELEMENT DATA
public:
T x, y;
// CONSTRUCTION / DESTRUCTION / ASSIGNMENT
public:
constexpr Point() noexcept : x(T(0)), y(T(0)) {}
constexpr Point(const T v) noexcept : x(v), y(v) {}
constexpr Point(const T vx, const T vy) noexcept : x(vx), y(vy) {}
// ELEMENT FUNCTIONS
public:
constexpr Point& operator+=(const Point<T>& p) noexcept {x += p.x, y += p.y; return *this;}
constexpr Point& operator-=(const Point<T>& p) noexcept {x -= p.x, y -= p.y; return *this;}
constexpr Point& operator*=(const Point<T>& p) noexcept {x *= p.x, y *= p.y; return *this;}
constexpr Point& operator/=(const Point<T>& p) noexcept {x /= p.x, y /= p.y; return *this;}
constexpr Point& operator+=(const T v) noexcept {x += v, y += v; return *this;}
constexpr Point& operator-=(const T v) noexcept {x -= v, y -= v; return *this;}
constexpr Point& operator*=(const T v) noexcept {x *= v, y *= v; return *this;}
constexpr Point& operator/=(const T v) noexcept {x /= v, y /= v; return *this;}
constexpr Point& translateX(const T tx) noexcept {x += tx; return *this;}
constexpr Point& translateY(const T ty) noexcept {y += ty; return *this;}
constexpr Point& translateXY(const Point<T>& p) {return operator+=(p);}
constexpr Point& scaleX(const T sx) noexcept {x *= sx; return *this;}
constexpr Point& scaleY(const T sy) noexcept {y *= sy; return *this;}
constexpr Point& scaleXY(const Point<T>& p) noexcept {return operator*=(p);}
Point& rotate(const T a) noexcept requires std::floating_point<T>;
constexpr Point& shearX(const T sx) noexcept {x += y * sx; return *this;}
constexpr Point& shearY(const T sy) noexcept {y += x * sy; return *this;}
};
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Point<T> - ELEMENT FUNCTIONS
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
inline Point<T>& Point<T>::rotate(const T a) noexcept
requires std::floating_point<T>
{
const T ca = cos(a);
const T sa = sin(a);
const T tx = x;
x = tx * ca - y * sa;
y = tx * sa + y * ca;
return *this;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Point<T> - GOBAL FUNCTIONS
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> operator+(Point<T> p) noexcept
{
return p;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> operator-(Point<T> p) noexcept
{
return {-p.x, -p.y};
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> operator+(Point<T> p1, const Point<T>& p2) noexcept
{
return p1 += p2;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> operator-(Point<T> p1, const Point<T>& p2) noexcept
{
return p1 -= p2;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> operator*(Point<T> p1, const Point<T>& p2) noexcept
{
return p1 *= p2;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> operator/(Point<T> p1, const Point<T>& p2) noexcept
{
return p1 /= p2;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> operator+(Point<T> p, const T v) noexcept
{
return p += v;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> operator+(const T v, Point<T> p) noexcept
{
return p += v;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> operator-(Point<T> p, const T v) noexcept
{
return p -= v;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> operator-(const T v, const Point<T>& p) noexcept
{
return {v - p.x, v - p.y};
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> operator*(Point<T> p, const T v) noexcept
{
return p *= v;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> operator*(const T v, Point<T> p) noexcept
{
return p *= v;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> operator/(Point<T> p, const T v) noexcept
{
return p /= v;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> operator/(const T v, const Point<T>& p) noexcept
{
return {v / p.x, v / p.y};
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> translateX(Point<T> p, const T tx) noexcept
{
return p.translateX(tx);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> translateY(Point<T> p, const T ty) noexcept
{
return p.translateY(ty);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> translateXY(Point<T> p, const Point<T>& tp) noexcept
{
return p.translateXY(tp);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> scaleX(Point<T> p, const T sx) noexcept
{
return p.scaleX(sx);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> scaleY(Point<T> p, const T sy) noexcept
{
return p.scaleY(sy);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> scaleXY(Point<T> p, const Point<T>& sp) noexcept
{
return p.scaleXY(sp);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> rotate(Point<T> p, const T ra) noexcept
{
return p.rotate(ra);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> shearX(Point<T> p, const T sx) noexcept
{
return p.shearX(sx);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Point<T> shearY(Point<T> p, const T sy) noexcept
{
return p.shearY(sy);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
inline T calcLength(const Point<T>& p) noexcept
requires std::floating_point<T>
{
return sqrt(p.x * p.x + p.y * p.y);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
inline T calcAngle(const Point<T>& p) noexcept
requires std::floating_point<T>
{
if (p.x == T(0.0))
{
if (p.y == T(0.0)) return T(0.0);
else if (p.y < T(0.0)) return T(rad_270);
else if (p.y > T(0.0)) return T(rad_090);
}
else
{
const T a = atan(p.y / p.x);
if (p.x < T(0.0)) return a + T(rad_180);
else if (p.x > T(0.0)) return p.y < T(0.0) ? a + T(rad_360) : a;
}
return T(0.0);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
} //namespace ama
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#endif // AMA_POINT_H
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------