@@ -22,7 +22,7 @@ template <isQuantity T> class Vector2D {
22
22
* This constructor initializes x and y to 0
23
23
*
24
24
*/
25
- Vector2D () : x(0.0 ), y(0.0 ) {}
25
+ constexpr Vector2D () : x(0.0 ), y(0.0 ) {}
26
26
27
27
/* *
28
28
* @brief Construct a new Vector2D object
@@ -32,7 +32,7 @@ template <isQuantity T> class Vector2D {
32
32
* @param nx x component
33
33
* @param ny y component
34
34
*/
35
- Vector2D (T nx, T ny) : x(nx), y(ny) {}
35
+ constexpr Vector2D (T nx, T ny) : x(nx), y(ny) {}
36
36
37
37
/* *
38
38
* @brief Create a new Vector2D object from polar coordinates
@@ -42,7 +42,7 @@ template <isQuantity T> class Vector2D {
42
42
* @param t angle
43
43
* @param m magnitude
44
44
*/
45
- static Vector2D fromPolar (Angle t, T m) {
45
+ constexpr static Vector2D fromPolar (Angle t, T m) {
46
46
m = abs (m);
47
47
t = constrainAngle360 (t);
48
48
return Vector2D<T>(m * cos (t), m * sin (t));
@@ -54,7 +54,7 @@ template <isQuantity T> class Vector2D {
54
54
* @param t angle
55
55
* @return Vector2D
56
56
*/
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 ); }
58
58
59
59
/* *
60
60
* @brief + operator overload
@@ -65,7 +65,7 @@ template <isQuantity T> class Vector2D {
65
65
* @param other vector to add
66
66
* @return Vector2D<T>
67
67
*/
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 ); }
69
69
70
70
/* *
71
71
* @brief - operator overload
@@ -76,7 +76,7 @@ template <isQuantity T> class Vector2D {
76
76
* @param other vector to subtract
77
77
* @return Vector2D<T>
78
78
*/
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 ); }
80
80
81
81
/* *
82
82
* @brief * operator overload
@@ -87,7 +87,7 @@ template <isQuantity T> class Vector2D {
87
87
* @param factor scalar to multiply by
88
88
* @return Vector2D<T>
89
89
*/
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); }
91
91
92
92
/* *
93
93
* @brief / operator overload
@@ -98,7 +98,7 @@ template <isQuantity T> class Vector2D {
98
98
* @param factor scalar to divide by
99
99
* @return Vector2D<T>
100
100
*/
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); }
102
102
103
103
/* *
104
104
* @brief += operator overload
@@ -109,7 +109,7 @@ template <isQuantity T> class Vector2D {
109
109
* @param other vector to add
110
110
* @return Vector2D<T>&
111
111
*/
112
- Vector2D<T>& operator +=(const Vector2D<T>& other) {
112
+ constexpr Vector2D<T>& operator +=(const Vector2D<T>& other) {
113
113
x += other.x ;
114
114
y += other.y ;
115
115
return (*this );
@@ -124,7 +124,7 @@ template <isQuantity T> class Vector2D {
124
124
* @param other vector to subtract
125
125
* @return Vector2D<T>&
126
126
*/
127
- Vector2D<T>& operator -=(const Vector2D<T>& other) {
127
+ constexpr Vector2D<T>& operator -=(const Vector2D<T>& other) {
128
128
x -= other.x ;
129
129
y -= other.y ;
130
130
return (*this );
@@ -140,7 +140,7 @@ template <isQuantity T> class Vector2D {
140
140
* @param factor scalar to multiply by
141
141
* @return Vector2D<T>&
142
142
*/
143
- Vector2D<T>& operator *=(double factor) {
143
+ constexpr Vector2D<T>& operator *=(double factor) {
144
144
x *= factor;
145
145
y *= factor;
146
146
return (*this );
@@ -156,7 +156,7 @@ template <isQuantity T> class Vector2D {
156
156
* @param factor scalar to divide by
157
157
* @return Vector2D<T>&
158
158
*/
159
- Vector2D<T>& operator /=(double factor) {
159
+ constexpr Vector2D<T>& operator /=(double factor) {
160
160
x /= factor;
161
161
y /= factor;
162
162
return (*this );
@@ -173,7 +173,7 @@ template <isQuantity T> class Vector2D {
173
173
* @param other the vector to calculate the dot product with
174
174
* @return R the dot product
175
175
*/
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 {
177
177
return (x * other.x ) + (y * other.y );
178
178
}
179
179
@@ -188,7 +188,7 @@ template <isQuantity T> class Vector2D {
188
188
* @param other the vector to calculate the cross product with
189
189
* @return R the cross product
190
190
*/
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 {
192
192
return (x * other.y ) - (y * other.x );
193
193
}
194
194
@@ -197,14 +197,14 @@ template <isQuantity T> class Vector2D {
197
197
*
198
198
* @return Angle
199
199
*/
200
- Angle theta () const { return atan2 (y, x); }
200
+ constexpr Angle theta () const { return atan2 (y, x); }
201
201
202
202
/* *
203
203
* @brief magnitude of the vector
204
204
*
205
205
* @return T
206
206
*/
207
- T magnitude () const { return sqrt (square (x) + square (y)); }
207
+ constexpr T magnitude () const { return sqrt (square (x) + square (y)); }
208
208
209
209
/* *
210
210
* @brief difference between two vectors
@@ -215,23 +215,23 @@ template <isQuantity T> class Vector2D {
215
215
* @param other the other vector
216
216
* @return Vector2D<T>
217
217
*/
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); }
219
219
220
220
/* *
221
221
* @brief the angle between two vectors
222
222
*
223
223
* @param other the other vector
224
224
* @return Angle
225
225
*/
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); }
227
227
228
228
/* *
229
229
* @brief get the distance between two vectors
230
230
*
231
231
* @param other the other vector
232
232
* @return T
233
233
*/
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 )); }
235
235
236
236
/* *
237
237
* @brief normalize the vector
@@ -240,7 +240,7 @@ template <isQuantity T> class Vector2D {
240
240
*
241
241
* @return Vector2D<T>
242
242
*/
243
- Vector2D<T> normalize () const {
243
+ constexpr Vector2D<T> normalize () const {
244
244
T m = magnitude ();
245
245
return Vector2D<T>(x / m, y / m);
246
246
}
@@ -250,7 +250,7 @@ template <isQuantity T> class Vector2D {
250
250
*
251
251
* @param angle
252
252
*/
253
- void rotateBy (Angle angle) {
253
+ constexpr void rotateBy (Angle angle) {
254
254
T m = magnitude ();
255
255
Angle t = theta () + angle;
256
256
x = m * cos (t);
@@ -262,7 +262,7 @@ template <isQuantity T> class Vector2D {
262
262
*
263
263
* @param angle
264
264
*/
265
- void rotateTo (Angle angle) {
265
+ constexpr void rotateTo (Angle angle) {
266
266
T m = magnitude ();
267
267
x = m * cos (angle);
268
268
y = m * sin (angle);
@@ -274,7 +274,7 @@ template <isQuantity T> class Vector2D {
274
274
* @param angle
275
275
* @return Vector2D<T>
276
276
*/
277
- Vector2D<T> rotatedBy (Angle angle) const {
277
+ constexpr Vector2D<T> rotatedBy (Angle angle) const {
278
278
T m = magnitude ();
279
279
Angle t = theta () + angle;
280
280
return fromPolar (t, m);
@@ -286,7 +286,7 @@ template <isQuantity T> class Vector2D {
286
286
* @param angle
287
287
* @return Vector2D<T>
288
288
*/
289
- Vector2D<T> rotatedTo (Angle angle) const {
289
+ constexpr Vector2D<T> rotatedTo (Angle angle) const {
290
290
T m = magnitude ();
291
291
return fromPolar (angle, m);
292
292
}
0 commit comments