6
6
7
7
namespace RTE {
8
8
9
- enum { X = 0 , Y = 1 };
9
+ enum Axes { X = 0 , Y = 1 };
10
10
11
11
// / <summary>
12
12
// / A useful 2D float vector.
@@ -21,41 +21,22 @@ namespace RTE {
21
21
float m_Y; // !< Y value of this vector.
22
22
23
23
#pragma region Creation
24
- // / <summary>
25
- // / Constructor method used to instantiate a Vector object.
26
- // / </summary>
27
- Vector () { Clear (); }
28
-
29
24
// / <summary>
30
25
// / Constructor method used to instantiate a Vector object from X and Y values.
31
26
// / </summary>
32
27
// / <param name="inputX">Float defining the initial X value of this Vector.</param>
33
28
// / <param name="inputY">Float defining the initial Y value of this Vector.</param>
34
- Vector (float inputX, float inputY) { Create (inputX, inputY); }
35
-
36
- // / <summary>
37
- // / Copy constructor method used to instantiate a Vector object identical to an already existing one.
38
- // / </summary>
39
- // / <param name="reference">A Vector object which is passed in by reference.</param>
40
- Vector (const Vector &reference) { Create (reference.m_X , reference.m_Y ); }
41
-
42
- // / <summary>
43
- // / Makes the Vector object ready for use.
44
- // / </summary>
45
- // / <param name="inputX">Float defining the initial X value of this Vector.</param>
46
- // / <param name="inputY">Float defining the initial Y value of this Vector.</param>
47
- // / <returns>An error return value signaling success or any particular failure. Anything below 0 is an error signal.</returns>
48
- int Create (float inputX, float inputY) { m_X = inputX; m_Y = inputY; return 0 ; }
29
+ Vector (const float inputX = 0 .0F , const float inputY = 0 .0F ) :
30
+ m_X (inputX),
31
+ m_Y (inputY) {};
49
32
#pragma endregion
50
33
51
- #pragma region Destruction
34
+ #pragma region Getters and Setters
52
35
// / <summary>
53
36
// / Sets both the X and Y of this Vector to zero.
54
37
// / </summary>
55
- void Reset () { m_X = m_Y = 0 .0F ; }
56
- #pragma endregion
38
+ void Reset () { m_X = 0 .0F ; m_Y = 0 .0F ; }
57
39
58
- #pragma region Getters and Setters
59
40
// / <summary>
60
41
// / Gets the X value of this Vector.
61
42
// / </summary>
@@ -110,39 +91,39 @@ namespace RTE {
110
91
// / Gets the absolute largest of the two elements. Will always be positive.
111
92
// / </summary>
112
93
// / <returns>A float describing the largest value of the two, but not the magnitude.</returns>
113
- float GetLargest () const { return std::fabs (( std::fabs (m_X) > std::fabs (m_Y)) ? m_X : m_Y ); }
94
+ float GetLargest () const { return std::max ( std::abs (m_X), std::abs (m_Y)); }
114
95
115
96
// / <summary>
116
97
// / Gets the absolute smallest of the two elements. Will always be positive.
117
98
// / </summary>
118
99
// / <returns>A float describing the smallest value of the two, but not the magnitude.</returns>
119
- float GetSmallest () const { return std::fabs (( std::fabs (m_X) > std::fabs (m_Y)) ? m_Y : m_X ); }
100
+ float GetSmallest () const { return std::min ( std::abs (m_X), std::abs (m_Y)); }
120
101
121
102
// / <summary>
122
103
// / Gets a Vector identical to this except that its X component is flipped.
123
104
// / </summary>
124
105
// / <param name="xFlip">Whether to flip the X axis of the return vector or not.</param>
125
106
// / <returns>A copy of this vector with flipped X axis.</returns>
126
- Vector GetXFlipped (bool xFlip = true ) const { Vector retVec (( xFlip ? -m_X : m_X) , m_Y); return retVec ; }
107
+ Vector GetXFlipped (const bool xFlip = true ) const { return Vector ( xFlip ? -m_X : m_X, m_Y); }
127
108
128
109
// / <summary>
129
110
// / Flips the X element of this Vector.
130
111
// / </summary>
131
112
// / <param name="flipX">Whether or not to flip the X element or not.</param>
132
- void FlipX (bool flipX = true ) { m_X = flipX ? -m_X : m_X ; }
113
+ void FlipX (const bool flipX = true ) { * this = GetYFlipped ( flipX) ; }
133
114
134
115
// / <summary>
135
116
// / Gets a Vector identical to this except that its Y component is flipped.
136
117
// / </summary>
137
118
// / <param name="yFlip">Whether to flip the Y axis of the return vector or not.</param>
138
119
// / <returns>A copy of this vector with flipped Y axis.</returns>
139
- Vector GetYFlipped (bool yFlip = true ) const { Vector retVec (m_X, ( yFlip ? -m_Y : m_Y)); return retVec ; }
120
+ Vector GetYFlipped (const bool yFlip = true ) const { return Vector (m_X, yFlip ? -m_Y : m_Y); }
140
121
141
122
// / <summary>
142
123
// / Flips the Y element of this Vector.
143
124
// / </summary>
144
125
// / <param name="flipY">Whether or not to flip the Y element or not.</param>
145
- void FlipY (bool flipY = true ) { m_Y = flipY ? -m_Y : m_Y ; }
126
+ void FlipY (const bool flipY = true ) { * this = GetYFlipped ( flipY) ; }
146
127
147
128
// / <summary>
148
129
// / Indicates whether the X component of this Vector is 0.
@@ -167,7 +148,7 @@ namespace RTE {
167
148
// / </summary>
168
149
// / <param name="opp">The Vector to compare with.</param>
169
150
// / <returns>Whether the X and Y components of this Vector each have opposite signs to their corresponding components of a passed in Vector.</returns>
170
- bool IsOpposedTo (const Vector &opp) { return ((XIsZero () && opp.XIsZero ()) || (std::signbit (m_X) != std::signbit (opp.m_X ))) && ((YIsZero () && opp.YIsZero ()) || (std::signbit (m_Y) != std::signbit (opp.m_Y ))); }
151
+ bool IsOpposedTo (const Vector &opp) const { return ((XIsZero () && opp.XIsZero ()) || (std::signbit (m_X) != std::signbit (opp.m_X ))) && ((YIsZero () && opp.YIsZero ()) || (std::signbit (m_Y) != std::signbit (opp.m_Y ))); }
171
152
#pragma endregion
172
153
173
154
#pragma region Magnitude
@@ -182,26 +163,26 @@ namespace RTE {
182
163
// / </summary>
183
164
// / <param name="newMag">A float value that the magnitude will be set to.</param>
184
165
// / <returns>A reference to this after the change.</returns>
185
- Vector & SetMagnitude (float newMag);
166
+ Vector & SetMagnitude (const float newMag);
186
167
187
168
// / <summary>
188
169
// / Caps the magnitude of this Vector to a max value and keeps its angle intact.
189
170
// / </summary>
190
171
// / <param name="capMag">A float value that the magnitude will be capped by.</param>
191
172
// / <returns>A reference to this after the change.</returns>
192
- Vector & CapMagnitude (float capMag);
173
+ Vector & CapMagnitude (const float capMag);
193
174
194
175
// / <summary>
195
176
// / Returns a Vector that has the same direction as this but with a magnitude of 1.0.
196
177
// / </summary>
197
- // / <returns></returns>
178
+ // / <returns>A normalized copy of this vector. </returns>
198
179
Vector GetNormalized () const { return *this / GetMagnitude (); }
199
180
200
181
// / <summary>
201
182
// / Scales this vector to have the same direction but a magnitude of 1.0.
202
183
// / </summary>
203
184
// / <returns>Vector reference to this after the operation.</returns>
204
- Vector & Normalize () { return *this /= GetMagnitude () ; }
185
+ Vector & Normalize () { *this = GetNormalized (); return * this ; }
205
186
#pragma endregion
206
187
207
188
#pragma region Rotation
@@ -222,14 +203,14 @@ namespace RTE {
222
203
// / </summary>
223
204
// / <param name="angle">The angle in radians to rotate by. Positive angles rotate counter-clockwise, and negative angles clockwise.</param>
224
205
// / <returns>This vector, rotated.</returns>
225
- Vector & RadRotate (float angle);
206
+ Vector & RadRotate (const float angle);
226
207
227
208
// / <summary>
228
209
// / Rotate this Vector relatively by an angle in degrees.
229
210
// / </summary>
230
211
// / <param name="angle">The angle in degrees to rotate by. Positive angles rotate counter-clockwise, and negative angles clockwise.</param>
231
212
// / <returns>This vector, rotated.</returns>
232
- Vector & DegRotate (float angle) { return RadRotate (angle * c_PI / 180 .0F ); }
213
+ Vector & DegRotate (const float angle) { return RadRotate (angle * c_PI / 180 .0F ); }
233
214
234
215
// / <summary>
235
216
// / Set this Vector to an absolute rotation based on the absolute rotation of another Vector.
@@ -255,46 +236,46 @@ namespace RTE {
255
236
// / <summary>
256
237
// / Rounds the X and Y values of this Vector upwards. E.g. 0.49 -> 0.0 and 0.5 -> 1.0.
257
238
// / </summary>
258
- void Round () { m_X = std::roundf (m_X); m_Y = std::roundf (m_Y ); }
239
+ void Round () { * this = GetRounded ( ); }
259
240
260
241
// / <summary>
261
242
// / Sets the X and Y of this Vector to the nearest half value. E.g. 1.0 -> 1.5 and 0.9 -> 0.5.
262
243
// / </summary>
263
- void ToHalf () { m_X = std::roundf (m_X * 2 ) / 2 ; m_Y = std::roundf (m_Y * 2 ) / 2 ; }
244
+ void ToHalf () { m_X = std::round (m_X * 2 ) / 2 ; m_Y = std::round (m_Y * 2 ) / 2 ; }
264
245
265
246
// / <summary>
266
247
// / Sets the X and Y of this Vector to the greatest integers that are not greater than their original values. E.g. -1.02 becomes -2.0.
267
248
// / </summary>
268
- void Floor () { m_X = std::floor (m_X); m_Y = std::floor (m_Y ); }
249
+ void Floor () { * this = GetFloored ( ); }
269
250
270
251
// / <summary>
271
252
// / Sets the X and Y of this Vector to the lowest integers that are not less than their original values. E.g. -1.02 becomes -1.0.
272
253
// / </summary>
273
- void Ceiling () { m_X = std::ceil (m_X); m_Y = std::ceil (m_Y ); }
254
+ void Ceiling () { * this = GetCeilinged ( ); }
274
255
275
256
// / <summary>
276
257
// / Returns a rounded copy of this Vector. Does not alter this Vector.
277
258
// / </summary>
278
259
// / <returns>A rounded copy of this Vector.</returns>
279
- Vector GetRounded () const { Vector returnVector = * this ; returnVector. Round (); return returnVector ; }
260
+ Vector GetRounded () const { return Vector ( std::round (m_X), std::round (m_Y)) ; }
280
261
281
262
// / <summary>
282
263
// / Returns the rounded integer X value of this Vector.
283
264
// / </summary>
284
265
// / <returns>An int value that represents the X value of this Vector.</returns>
285
- int GetRoundIntX () const { return static_cast <int >(std::roundf (m_X)); }
266
+ int GetRoundIntX () const { return static_cast <int >(std::round (m_X)); }
286
267
287
268
// / <summary>
288
269
// / Returns the rounded integer Y value of this Vector.
289
270
// / </summary>
290
271
// / <returns>An int value that represents the Y value of this Vector.</returns>
291
- int GetRoundIntY () const { return static_cast <int >(std::roundf (m_Y)); }
272
+ int GetRoundIntY () const { return static_cast <int >(std::round (m_Y)); }
292
273
293
274
// / <summary>
294
275
// / Returns a floored copy of this Vector. Does not alter this Vector.
295
276
// / </summary>
296
277
// / <returns>A floored copy of this Vector.</returns>
297
- Vector GetFloored () const { Vector returnVector = * this ; returnVector. Floor (); return returnVector ; }
278
+ Vector GetFloored () const { return Vector ( std::floor (m_X), std::floor (m_Y)) ; }
298
279
299
280
// / <summary>
300
281
// / Returns the greatest integer that is not greater than the X value of this Vector.
@@ -312,7 +293,7 @@ namespace RTE {
312
293
// / Returns a ceilinged copy of this Vector. Does not alter this Vector.
313
294
// / </summary>
314
295
// / <returns>A ceilinged copy of this Vector.</returns>
315
- Vector GetCeilinged () const { Vector returnVector = * this ; returnVector. Ceiling (); return returnVector ; }
296
+ Vector GetCeilinged () const { return Vector ( std::ceil (m_X), std::ceil (m_Y)) ; }
316
297
317
298
// / <summary>
318
299
// / Returns the lowest integer that is not less than the X value of this Vector.
@@ -333,24 +314,17 @@ namespace RTE {
333
314
// / </summary>
334
315
// / <param name="rhs">The Vector which will be the right hand side operand of the dot product operation.</param>
335
316
// / <returns>The resulting dot product scalar float.</returns>
336
- float Dot (const Vector &rhs) { return (m_X * rhs.m_X ) + (m_Y * rhs.m_Y ); }
317
+ float Dot (const Vector &rhs) const { return (m_X * rhs.m_X ) + (m_Y * rhs.m_Y ); }
337
318
338
319
// / <summary>
339
320
// / Returns the 2D cross product of this Vector and the passed in Vector. This is really the area of the parallelogram that the two vectors form.
340
321
// / </summary>
341
322
// / <param name="rhs">The Vector which will be the right hand side operand of the cross product operation.</param>
342
323
// / <returns>The resulting 2D cross product parallelogram area.</returns>
343
- float Cross (const Vector &rhs) { return (m_X * rhs.m_Y ) - (rhs.m_X * m_Y); }
324
+ float Cross (const Vector &rhs) const { return (m_X * rhs.m_Y ) - (rhs.m_X * m_Y); }
344
325
#pragma endregion
345
326
346
327
#pragma region Operator Overloads
347
- // / <summary>
348
- // / An assignment operator for setting one Vector equal to another.
349
- // / </summary>
350
- // / <param name="rhs">A Vector reference.</param>
351
- // / <returns>A reference to the changed Vector.</returns>
352
- Vector & operator =(const Vector &rhs);
353
-
354
328
// / <summary>
355
329
// / An assignment operator for setting this Vector equal to the average of an std::deque of Vectors.
356
330
// / </summary>
@@ -362,7 +336,7 @@ namespace RTE {
362
336
// / Unary negation overload for single Vectors.
363
337
// / </summary>
364
338
// / <returns>The resulting Vector.</returns>
365
- Vector operator -() { Vector returnVector (-m_X, -m_Y); return returnVector ; }
339
+ Vector operator -() { return Vector (-m_X, -m_Y); }
366
340
367
341
// / <summary>
368
342
// / An equality operator for testing if any two Vectors are equal.
@@ -378,7 +352,7 @@ namespace RTE {
378
352
// / <param name="lhs">A Vector reference as the left hand side operand.</param>
379
353
// / <param name="rhs">A Vector reference as the right hand side operand.</param>
380
354
// / <returns>A boolean indicating whether the two operands are unequal or not.</returns>
381
- friend bool operator !=(const Vector &lhs, const Vector &rhs) { return lhs. m_X != rhs. m_X || lhs. m_Y != rhs. m_Y ; }
355
+ friend bool operator !=(const Vector &lhs, const Vector &rhs) { return !( lhs== rhs) ; }
382
356
383
357
// / <summary>
384
358
// / A stream insertion operator for sending a Vector to an output stream.
@@ -388,13 +362,6 @@ namespace RTE {
388
362
// / <returns>An ostream reference for further use in an expression.</returns>
389
363
friend std::ostream & operator <<(std::ostream &stream, const Vector &operand) { stream << " {" << operand.m_X << " , " << operand.m_Y << " }" ; return stream; }
390
364
391
- // / <summary>
392
- // / Addition operator overload for a Vector and a float.
393
- // / </summary>
394
- // / <param name="rhs">A float reference as the right hand side operand.</param>
395
- // / <returns>The resulting Vector.</returns>
396
- Vector operator +(const float &rhs) const { Vector returnVector (m_X + rhs, m_Y + rhs); return returnVector; }
397
-
398
365
// / <summary>
399
366
// / Addition operator overload for Vectors.
400
367
// / </summary>
@@ -440,7 +407,7 @@ namespace RTE {
440
407
// / <returns>The resulting Vector.</returns>
441
408
Vector operator /(const float &rhs) const {
442
409
Vector returnVector (0 , 0 );
443
- if (rhs) { returnVector.SetXY (m_X / rhs, m_Y / rhs); }
410
+ if (rhs != 0 ) { returnVector.SetXY (m_X / rhs, m_Y / rhs); }
444
411
return returnVector;
445
412
}
446
413
@@ -452,7 +419,7 @@ namespace RTE {
452
419
// / <returns>The resulting Vector.</returns>
453
420
friend Vector operator /(const Vector &lhs, const Vector &rhs) {
454
421
Vector returnVector (0 , 0 );
455
- if (rhs.m_X && rhs.m_Y ) { returnVector.SetXY (lhs.m_X / rhs.m_X , lhs.m_Y / rhs.m_Y ); }
422
+ if (rhs.m_X != 0 && rhs.m_Y != 0 ) { returnVector.SetXY (lhs.m_X / rhs.m_X , lhs.m_Y / rhs.m_Y ); }
456
423
return returnVector;
457
424
}
458
425
@@ -506,7 +473,7 @@ namespace RTE {
506
473
// / </summary>
507
474
// / <param name="rhs">A float reference as the right hand side operand.</param>
508
475
// / <returns>A reference to the resulting Vector.</returns>
509
- Vector & operator /=(const float &rhs) { if (rhs) { m_X /= rhs; m_Y /= rhs; } return *this ; }
476
+ Vector & operator /=(const float &rhs) { if (rhs != 0 ) { m_X /= rhs; m_Y /= rhs; } return *this ; }
510
477
511
478
// / <summary>
512
479
// / Self-division operator overload for Vectors.
@@ -515,8 +482,8 @@ namespace RTE {
515
482
// / <param name="rhs">A Vector reference as the right hand side operand.</param>
516
483
// / <returns>A reference to the resulting Vector (the left one).</returns>
517
484
friend Vector & operator /=(Vector &lhs, const Vector &rhs) {
518
- if (rhs.m_X ) { lhs.m_X /= rhs.m_X ; }
519
- if (rhs.m_Y ) { lhs.m_Y /= rhs.m_Y ; }
485
+ if (rhs.m_X != 0 ) { lhs.m_X /= rhs.m_X ; }
486
+ if (rhs.m_Y != 0 ) { lhs.m_Y /= rhs.m_Y ; }
520
487
return lhs;
521
488
}
522
489
@@ -546,13 +513,6 @@ namespace RTE {
546
513
protected:
547
514
548
515
static const std::string c_ClassName; // !< A string with the friendly-formatted type name of this.
549
-
550
- private:
551
-
552
- // / <summary>
553
- // / Clears all the member variables of this Vector, effectively resetting the members of this abstraction level only.
554
- // / </summary>
555
- void Clear () { m_X = m_Y = 0 ; }
556
516
};
557
517
}
558
518
#endif
0 commit comments