-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvector2d.hpp
284 lines (251 loc) · 7.55 KB
/
vector2d.hpp
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#pragma once
#include <array>
#include <math.h>
namespace espp {
/**
* @brief Container representing a 2 dimensional vector.
*
* Provides getters/setters, index operator, and vector / scalar math
* utilities.
*/
template <typename T> class Vector2d {
public:
/**
* @brief Constructor for the vector, defaults to 0,0.
* @param x The starting X value.
* @param y The starting Y value.
*/
explicit Vector2d(T x = T(0), T y = T(0)) : x_(x), y_(y) {}
/**
* @brief Vector copy constructor.
* @param other Vector to copy.
*/
Vector2d(const Vector2d &other) : x_(other.x_), y_(other.y_) {}
/**
* @brief Assignment operator
* @param other Vector to assign to this vector.
* @return This vector, updated to be a copy of \p other.
*/
Vector2d &operator=(const Vector2d &other) {
x_ = other.x_;
y_ = other.y_;
return *this;
}
/**
* @brief Returns vector magnitude: ||v||.
* @return The magnitude.
*/
T magnitude() const { return sqrt(magnitude_squared()); }
/**
* @brief Returns vector magnitude squared: ||v||^2.
* @return The magnitude squared.
*/
T magnitude_squared() const { return x_ * x_ + y_ * y_; }
/**
* @brief Getter for the x value.
* @return The current x value.
*/
T x() const { return x_; }
/**
* @brief Setter for the x value.
* @param v New value for \c x.
*/
void x(T v) { x_ = v; }
/**
* @brief Getter for the y value.
* @return The current y value.
*/
T y() const { return y_; }
/**
* @brief Setter for the y value.
* @param v New value for \c y.
*/
void y(T v) { y_ = v; }
/**
* @brief Index operator for vector elements.
* @note Returns a mutable reference to the element.
* @param index The index to return.
* @return Mutable reference to the element at \p index.
*/
T &operator[](int index) { return values_[index]; }
/**
* @brief Negate the vector.
* @return The new vector which is the negative.
*/
Vector2d operator-() const { return Vector2d(-x_, -y_); }
/**
* @brief Return a new vector which is the provided vector subtracted from
* this vector.
* @param rhs The vector to subtract from this vector.
* @return Resultant vector subtraction.
*/
Vector2d operator-(const Vector2d &rhs) const { return Vector2d(x_ - rhs.x_, y_ - rhs.y_); }
/**
* @brief Return the provided vector subtracted from this vector.
* @param rhs The vector to subtract from this vector.
* @return Resultant vector subtraction.
*/
Vector2d &operator-=(const Vector2d &rhs) {
x_ -= rhs.x_;
y_ -= rhs.y_;
return *this;
}
/**
* @brief Return a new vector, which is the addition of this vector and the
* provided vector.
* @param rhs The vector to add to this vector.
* @return Resultant vector addition.
*/
Vector2d operator+(const Vector2d &rhs) const { return Vector2d(x_ + rhs.x_, y_ + rhs.y_); }
/**
* @brief Return the vector added with the provided vector.
* @param rhs The vector to add to this vector.
* @return Resultant vector addition.
*/
Vector2d &operator+=(const Vector2d &rhs) {
x_ += rhs.x_;
y_ += rhs.y_;
return *this;
}
/**
* @brief Return a scaled version of the vector, multiplied by the provided
* value.
* @param v Value the vector should be multiplied by.
* @return Resultant scaled vector.
*/
Vector2d operator*(const T &v) const { return Vector2d(x_ * v, y_ * v); }
/**
* @brief Return the vector multiplied by the provided value.
* @param v Value the vector should be scaled by.
* @return Resultant scaled vector.
*/
Vector2d &operator*=(const T &v) {
x_ *= v;
y_ *= v;
return *this;
}
/**
* @brief Return a scaled version of the vector, divided by the provided
* value.
* @param v Value the vector should be divided by.
* @return Resultant scaled vector.
*/
Vector2d operator/(const T &v) const {
if (v != T(0)) {
return Vector2d(x_ / v, y_ / v);
} else {
return *this;
}
}
/**
* @brief Return the vector divided by the provided value.
* @param v Value the vector should be divided by.
* @return Resultant scaled vector.
*/
Vector2d &operator/=(const T &v) {
if (v != T(0)) {
x_ /= v;
y_ /= v;
}
return *this;
}
/**
* @brief Return a scaled version of the vector, divided by the provided
* vector value. Scales x and y independently.
* @param v Vector values the vector should be divided by.
* @return Resultant scaled vector.
*/
Vector2d operator/(const Vector2d &v) const {
auto _x = v.x_ != T(0) ? (x_ / v.x_) : x_;
auto _y = v.y_ != T(0) ? (y_ / v.y_) : y_;
return Vector2d(_x, _y);
}
/**
* @brief Return the vector divided by the provided vector values.
* @param v Vector of values the vector should be divided by.
* @return Resultant scaled vector.
*/
Vector2d &operator/=(const Vector2d &v) {
if (v.x_ != T(0)) {
x_ /= v.x_;
}
if (v.y_ != T(0)) {
y_ /= v.y_;
}
return *this;
}
/**
* @brief Dot product of this vector with another vector.
* @param other The second vector
* @return The dot product (x1*x2 + y1*y2)
*/
T dot(const Vector2d &other) const { return (x_ * other.x_) + (y_ * other.y_); }
/**
* @brief Return normalized (unit length) version of the vector.
* @return The normalized vector.
*/
Vector2d normalized() const {
T m = magnitude();
if (m > T(0)) {
return *this / m;
}
return *this;
}
/**
* @brief Rotate the vector by \p radians.
* @note This function is only available if T is a floating point value.
* @param radians Amount of rotation (in radians) to rotate the vector by.
* @return Rotated vector.
*/
template <class U = T,
typename std::enable_if<std::is_floating_point<U>::value>::type * = nullptr>
Vector2d rotated(T radians) const {
if (radians != T(0)) {
T x2 = x_ * cos(radians) - y_ * sin(radians);
T y2 = x_ * sin(radians) + y_ * cos(radians);
return Vector2d(x2, y2);
}
return *this;
}
protected:
union {
struct {
T x_;
T y_;
};
std::array<T, 2> values_;
};
};
typedef Vector2d<float> Vector2f; ///< Typedef for 32 bit floating point 2D vectors.
typedef Vector2d<double> Vector2f64; ///< Typedef for 64 bit floating point 2D vectors.
typedef Vector2d<int> Vector2i; ///< Typedef for integer 2D vectors.
typedef Vector2d<uint8_t> Vector2u8; ///< Typedef for 8 bit integer 2D vectors.
/**
* @brief Operator overload for scaling a vector v by a floating point value f.
* @param f Scaling factor.
* @param v Vector to be scaled.
* @return Scaled vector (v*f).
*/
[[maybe_unused]] static Vector2f operator*(float f, const Vector2f &v) {
return espp::Vector2f(v.x() * f, v.y() * f);
}
/**
* @brief Compare if two vectors are equal.
* @param lhs Left hand side vector
* @param rhs Right hand side vector
* @return True if their x and y are equal, false otherwise.
*/
[[maybe_unused]] static bool operator==(const Vector2f &lhs, const Vector2f &rhs) {
return lhs.x() == rhs.x() && lhs.y() == rhs.y();
}
} // namespace espp
#include "format.hpp"
// for allowing easy serialization/printing of the
// espp::Vector2d<type>
template <typename Value> struct fmt::formatter<espp::Vector2d<Value>> {
template <typename ParseContext> constexpr auto parse(ParseContext &ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(espp::Vector2d<Value> const &v, FormatContext &ctx) {
return fmt::format_to(ctx.out(), "({},{})", v.x(), v.y());
}
};