Skip to content

Commit ec38df6

Browse files
committed
mark vector functions as constexpr
1 parent 2c93ea3 commit ec38df6

File tree

2 files changed

+48
-48
lines changed

2 files changed

+48
-48
lines changed

include/units/Vector2D.hpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ template <isQuantity T> class Vector2D {
2222
* This constructor initializes x and y to 0
2323
*
2424
*/
25-
Vector2D() : x(0.0), y(0.0) {}
25+
constexpr Vector2D() : x(0.0), y(0.0) {}
2626

2727
/**
2828
* @brief Construct a new Vector2D object
@@ -32,7 +32,7 @@ template <isQuantity T> class Vector2D {
3232
* @param nx x component
3333
* @param ny y component
3434
*/
35-
Vector2D(T nx, T ny) : x(nx), y(ny) {}
35+
constexpr Vector2D(T nx, T ny) : x(nx), y(ny) {}
3636

3737
/**
3838
* @brief Create a new Vector2D object from polar coordinates
@@ -42,7 +42,7 @@ template <isQuantity T> class Vector2D {
4242
* @param t angle
4343
* @param m magnitude
4444
*/
45-
static Vector2D fromPolar(Angle t, T m) {
45+
constexpr static Vector2D fromPolar(Angle t, T m) {
4646
m = abs(m);
4747
t = constrainAngle360(t);
4848
return Vector2D<T>(m * cos(t), m * sin(t));
@@ -54,7 +54,7 @@ template <isQuantity T> class Vector2D {
5454
* @param t angle
5555
* @return Vector2D
5656
*/
57-
static Vector2D unitVector(Angle t) { return fromPolar(t, (T)1.0); }
57+
constexpr static Vector2D unitVector(Angle t) { return fromPolar(t, (T)1.0); }
5858

5959
/**
6060
* @brief + operator overload
@@ -65,7 +65,7 @@ template <isQuantity T> class Vector2D {
6565
* @param other vector to add
6666
* @return Vector2D<T>
6767
*/
68-
Vector2D<T> operator+(const Vector2D<T>& other) const { return Vector2D<T>(x + other.x, y + other.y); }
68+
constexpr Vector2D<T> operator+(const Vector2D<T>& other) const { return Vector2D<T>(x + other.x, y + other.y); }
6969

7070
/**
7171
* @brief - operator overload
@@ -76,7 +76,7 @@ template <isQuantity T> class Vector2D {
7676
* @param other vector to subtract
7777
* @return Vector2D<T>
7878
*/
79-
Vector2D<T> operator-(const Vector2D<T>& other) const { return Vector2D<T>(x - other.x, y - other.y); }
79+
constexpr Vector2D<T> operator-(const Vector2D<T>& other) const { return Vector2D<T>(x - other.x, y - other.y); }
8080

8181
/**
8282
* @brief * operator overload
@@ -87,7 +87,7 @@ template <isQuantity T> class Vector2D {
8787
* @param factor scalar to multiply by
8888
* @return Vector2D<T>
8989
*/
90-
Vector2D<T> operator*(double factor) const { return Vector2D<T>(x * factor, y * factor); }
90+
constexpr Vector2D<T> operator*(double factor) const { return Vector2D<T>(x * factor, y * factor); }
9191

9292
/**
9393
* @brief / operator overload
@@ -98,7 +98,7 @@ template <isQuantity T> class Vector2D {
9898
* @param factor scalar to divide by
9999
* @return Vector2D<T>
100100
*/
101-
Vector2D<T> operator/(double factor) const { return Vector2D<T>(x / factor, y / factor); }
101+
constexpr Vector2D<T> operator/(double factor) const { return Vector2D<T>(x / factor, y / factor); }
102102

103103
/**
104104
* @brief += operator overload
@@ -109,7 +109,7 @@ template <isQuantity T> class Vector2D {
109109
* @param other vector to add
110110
* @return Vector2D<T>&
111111
*/
112-
Vector2D<T>& operator+=(const Vector2D<T>& other) {
112+
constexpr Vector2D<T>& operator+=(const Vector2D<T>& other) {
113113
x += other.x;
114114
y += other.y;
115115
return (*this);
@@ -124,7 +124,7 @@ template <isQuantity T> class Vector2D {
124124
* @param other vector to subtract
125125
* @return Vector2D<T>&
126126
*/
127-
Vector2D<T>& operator-=(const Vector2D<T>& other) {
127+
constexpr Vector2D<T>& operator-=(const Vector2D<T>& other) {
128128
x -= other.x;
129129
y -= other.y;
130130
return (*this);
@@ -140,7 +140,7 @@ template <isQuantity T> class Vector2D {
140140
* @param factor scalar to multiply by
141141
* @return Vector2D<T>&
142142
*/
143-
Vector2D<T>& operator*=(double factor) {
143+
constexpr Vector2D<T>& operator*=(double factor) {
144144
x *= factor;
145145
y *= factor;
146146
return (*this);
@@ -156,7 +156,7 @@ template <isQuantity T> class Vector2D {
156156
* @param factor scalar to divide by
157157
* @return Vector2D<T>&
158158
*/
159-
Vector2D<T>& operator/=(double factor) {
159+
constexpr Vector2D<T>& operator/=(double factor) {
160160
x /= factor;
161161
y /= factor;
162162
return (*this);
@@ -173,7 +173,7 @@ template <isQuantity T> class Vector2D {
173173
* @param other the vector to calculate the dot product with
174174
* @return R the dot product
175175
*/
176-
template <isQuantity Q, isQuantity R = Multiplied<T, Q>> R dot(const Vector2D<Q>& other) const {
176+
template <isQuantity Q, isQuantity R = Multiplied<T, Q>> constexpr R dot(const Vector2D<Q>& other) const {
177177
return (x * other.x) + (y * other.y);
178178
}
179179

@@ -188,7 +188,7 @@ template <isQuantity T> class Vector2D {
188188
* @param other the vector to calculate the cross product with
189189
* @return R the cross product
190190
*/
191-
template <isQuantity Q, isQuantity R = Multiplied<T, Q>> R cross(const Vector2D<Q>& other) const {
191+
template <isQuantity Q, isQuantity R = Multiplied<T, Q>> constexpr R cross(const Vector2D<Q>& other) const {
192192
return (x * other.y) - (y * other.x);
193193
}
194194

@@ -197,14 +197,14 @@ template <isQuantity T> class Vector2D {
197197
*
198198
* @return Angle
199199
*/
200-
Angle theta() const { return atan2(y, x); }
200+
constexpr Angle theta() const { return atan2(y, x); }
201201

202202
/**
203203
* @brief magnitude of the vector
204204
*
205205
* @return T
206206
*/
207-
T magnitude() const { return sqrt(square(x) + square(y)); }
207+
constexpr T magnitude() const { return sqrt(square(x) + square(y)); }
208208

209209
/**
210210
* @brief difference between two vectors
@@ -215,23 +215,23 @@ template <isQuantity T> class Vector2D {
215215
* @param other the other vector
216216
* @return Vector2D<T>
217217
*/
218-
Vector2D<T> vectorTo(const Vector2D<T>& other) const { return Vector2D<T>(other.x - x, other.y - y); }
218+
constexpr Vector2D<T> vectorTo(const Vector2D<T>& other) const { return Vector2D<T>(other.x - x, other.y - y); }
219219

220220
/**
221221
* @brief the angle between two vectors
222222
*
223223
* @param other the other vector
224224
* @return Angle
225225
*/
226-
Angle angleTo(const Vector2D<T>& other) const { return atan2(other.y - y, other.x - x); }
226+
constexpr Angle angleTo(const Vector2D<T>& other) const { return atan2(other.y - y, other.x - x); }
227227

228228
/**
229229
* @brief get the distance between two vectors
230230
*
231231
* @param other the other vector
232232
* @return T
233233
*/
234-
T distanceTo(const Vector2D<T>& other) const { return sqrt(square(x - other.x, 2) + square(y - other.y, 2)); }
234+
constexpr T distanceTo(const Vector2D<T>& other) const { return sqrt(square(x - other.x, 2) + square(y - other.y, 2)); }
235235

236236
/**
237237
* @brief normalize the vector
@@ -240,7 +240,7 @@ template <isQuantity T> class Vector2D {
240240
*
241241
* @return Vector2D<T>
242242
*/
243-
Vector2D<T> normalize() const {
243+
constexpr Vector2D<T> normalize() const {
244244
T m = magnitude();
245245
return Vector2D<T>(x / m, y / m);
246246
}
@@ -250,7 +250,7 @@ template <isQuantity T> class Vector2D {
250250
*
251251
* @param angle
252252
*/
253-
void rotateBy(Angle angle) {
253+
constexpr void rotateBy(Angle angle) {
254254
T m = magnitude();
255255
Angle t = theta() + angle;
256256
x = m * cos(t);
@@ -262,7 +262,7 @@ template <isQuantity T> class Vector2D {
262262
*
263263
* @param angle
264264
*/
265-
void rotateTo(Angle angle) {
265+
constexpr void rotateTo(Angle angle) {
266266
T m = magnitude();
267267
x = m * cos(angle);
268268
y = m * sin(angle);
@@ -274,7 +274,7 @@ template <isQuantity T> class Vector2D {
274274
* @param angle
275275
* @return Vector2D<T>
276276
*/
277-
Vector2D<T> rotatedBy(Angle angle) const {
277+
constexpr Vector2D<T> rotatedBy(Angle angle) const {
278278
T m = magnitude();
279279
Angle t = theta() + angle;
280280
return fromPolar(t, m);
@@ -286,7 +286,7 @@ template <isQuantity T> class Vector2D {
286286
* @param angle
287287
* @return Vector2D<T>
288288
*/
289-
Vector2D<T> rotatedTo(Angle angle) const {
289+
constexpr Vector2D<T> rotatedTo(Angle angle) const {
290290
T m = magnitude();
291291
return fromPolar(angle, m);
292292
}

0 commit comments

Comments
 (0)