Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 021b2cd

Browse files
committed
Update Vector header
Named the X, Y enum: Axes. Removed unnecessary constructors, left only one. Removed Clear(), it was a duplicate of Reset(). And this isn't memory pool land. Rewrote a bunch of methods to be shorter or more readable. Const all the things. Removed unnecessary copy-assignment. Default does the same thing.
1 parent 5337b51 commit 021b2cd

File tree

2 files changed

+38
-88
lines changed

2 files changed

+38
-88
lines changed

System/Vector.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,10 @@ namespace RTE {
7070
return *this;
7171
}
7272

73-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
74-
75-
Vector & Vector::operator=(const Vector &rhs) {
76-
if (*this != rhs) {
77-
m_X = rhs.m_X;
78-
m_Y = rhs.m_Y;
79-
}
80-
return *this;
81-
}
82-
8373
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
8474

8575
Vector & Vector::operator=(const std::deque<Vector> &rhs) {
86-
Clear();
76+
Reset();
8777
if (!rhs.empty()) {
8878
for (const Vector &vector : rhs) {
8979
*this += vector;

System/Vector.h

Lines changed: 37 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace RTE {
88

9-
enum { X = 0, Y = 1 };
9+
enum Axes { X = 0, Y = 1 };
1010

1111
/// <summary>
1212
/// A useful 2D float vector.
@@ -21,41 +21,22 @@ namespace RTE {
2121
float m_Y; //!< Y value of this vector.
2222

2323
#pragma region Creation
24-
/// <summary>
25-
/// Constructor method used to instantiate a Vector object.
26-
/// </summary>
27-
Vector() { Clear(); }
28-
2924
/// <summary>
3025
/// Constructor method used to instantiate a Vector object from X and Y values.
3126
/// </summary>
3227
/// <param name="inputX">Float defining the initial X value of this Vector.</param>
3328
/// <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) {};
4932
#pragma endregion
5033

51-
#pragma region Destruction
34+
#pragma region Getters and Setters
5235
/// <summary>
5336
/// Sets both the X and Y of this Vector to zero.
5437
/// </summary>
55-
void Reset() { m_X = m_Y = 0.0F; }
56-
#pragma endregion
38+
void Reset() { m_X = 0.0F; m_Y = 0.0F; }
5739

58-
#pragma region Getters and Setters
5940
/// <summary>
6041
/// Gets the X value of this Vector.
6142
/// </summary>
@@ -110,39 +91,39 @@ namespace RTE {
11091
/// Gets the absolute largest of the two elements. Will always be positive.
11192
/// </summary>
11293
/// <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)); }
11495

11596
/// <summary>
11697
/// Gets the absolute smallest of the two elements. Will always be positive.
11798
/// </summary>
11899
/// <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)); }
120101

121102
/// <summary>
122103
/// Gets a Vector identical to this except that its X component is flipped.
123104
/// </summary>
124105
/// <param name="xFlip">Whether to flip the X axis of the return vector or not.</param>
125106
/// <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); }
127108

128109
/// <summary>
129110
/// Flips the X element of this Vector.
130111
/// </summary>
131112
/// <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); }
133114

134115
/// <summary>
135116
/// Gets a Vector identical to this except that its Y component is flipped.
136117
/// </summary>
137118
/// <param name="yFlip">Whether to flip the Y axis of the return vector or not.</param>
138119
/// <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); }
140121

141122
/// <summary>
142123
/// Flips the Y element of this Vector.
143124
/// </summary>
144125
/// <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); }
146127

147128
/// <summary>
148129
/// Indicates whether the X component of this Vector is 0.
@@ -167,7 +148,7 @@ namespace RTE {
167148
/// </summary>
168149
/// <param name="opp">The Vector to compare with.</param>
169150
/// <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))); }
171152
#pragma endregion
172153

173154
#pragma region Magnitude
@@ -182,26 +163,26 @@ namespace RTE {
182163
/// </summary>
183164
/// <param name="newMag">A float value that the magnitude will be set to.</param>
184165
/// <returns>A reference to this after the change.</returns>
185-
Vector & SetMagnitude(float newMag);
166+
Vector & SetMagnitude(const float newMag);
186167

187168
/// <summary>
188169
/// Caps the magnitude of this Vector to a max value and keeps its angle intact.
189170
/// </summary>
190171
/// <param name="capMag">A float value that the magnitude will be capped by.</param>
191172
/// <returns>A reference to this after the change.</returns>
192-
Vector & CapMagnitude(float capMag);
173+
Vector & CapMagnitude(const float capMag);
193174

194175
/// <summary>
195176
/// Returns a Vector that has the same direction as this but with a magnitude of 1.0.
196177
/// </summary>
197-
/// <returns></returns>
178+
/// <returns>A normalized copy of this vector.</returns>
198179
Vector GetNormalized() const { return *this / GetMagnitude(); }
199180

200181
/// <summary>
201182
/// Scales this vector to have the same direction but a magnitude of 1.0.
202183
/// </summary>
203184
/// <returns>Vector reference to this after the operation.</returns>
204-
Vector & Normalize() { return *this /= GetMagnitude(); }
185+
Vector & Normalize() { *this = GetNormalized(); return *this; }
205186
#pragma endregion
206187

207188
#pragma region Rotation
@@ -222,14 +203,14 @@ namespace RTE {
222203
/// </summary>
223204
/// <param name="angle">The angle in radians to rotate by. Positive angles rotate counter-clockwise, and negative angles clockwise.</param>
224205
/// <returns>This vector, rotated.</returns>
225-
Vector & RadRotate(float angle);
206+
Vector & RadRotate(const float angle);
226207

227208
/// <summary>
228209
/// Rotate this Vector relatively by an angle in degrees.
229210
/// </summary>
230211
/// <param name="angle">The angle in degrees to rotate by. Positive angles rotate counter-clockwise, and negative angles clockwise.</param>
231212
/// <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); }
233214

234215
/// <summary>
235216
/// Set this Vector to an absolute rotation based on the absolute rotation of another Vector.
@@ -255,46 +236,46 @@ namespace RTE {
255236
/// <summary>
256237
/// Rounds the X and Y values of this Vector upwards. E.g. 0.49 -> 0.0 and 0.5 -> 1.0.
257238
/// </summary>
258-
void Round() { m_X = std::roundf(m_X); m_Y = std::roundf(m_Y); }
239+
void Round() { *this = GetRounded(); }
259240

260241
/// <summary>
261242
/// Sets the X and Y of this Vector to the nearest half value. E.g. 1.0 -> 1.5 and 0.9 -> 0.5.
262243
/// </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; }
264245

265246
/// <summary>
266247
/// 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.
267248
/// </summary>
268-
void Floor() { m_X = std::floor(m_X); m_Y = std::floor(m_Y); }
249+
void Floor() { *this = GetFloored(); }
269250

270251
/// <summary>
271252
/// 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.
272253
/// </summary>
273-
void Ceiling() { m_X = std::ceil(m_X); m_Y = std::ceil(m_Y); }
254+
void Ceiling() { *this = GetCeilinged(); }
274255

275256
/// <summary>
276257
/// Returns a rounded copy of this Vector. Does not alter this Vector.
277258
/// </summary>
278259
/// <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)); }
280261

281262
/// <summary>
282263
/// Returns the rounded integer X value of this Vector.
283264
/// </summary>
284265
/// <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)); }
286267

287268
/// <summary>
288269
/// Returns the rounded integer Y value of this Vector.
289270
/// </summary>
290271
/// <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)); }
292273

293274
/// <summary>
294275
/// Returns a floored copy of this Vector. Does not alter this Vector.
295276
/// </summary>
296277
/// <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)); }
298279

299280
/// <summary>
300281
/// Returns the greatest integer that is not greater than the X value of this Vector.
@@ -312,7 +293,7 @@ namespace RTE {
312293
/// Returns a ceilinged copy of this Vector. Does not alter this Vector.
313294
/// </summary>
314295
/// <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)); }
316297

317298
/// <summary>
318299
/// Returns the lowest integer that is not less than the X value of this Vector.
@@ -333,24 +314,17 @@ namespace RTE {
333314
/// </summary>
334315
/// <param name="rhs">The Vector which will be the right hand side operand of the dot product operation.</param>
335316
/// <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); }
337318

338319
/// <summary>
339320
/// 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.
340321
/// </summary>
341322
/// <param name="rhs">The Vector which will be the right hand side operand of the cross product operation.</param>
342323
/// <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); }
344325
#pragma endregion
345326

346327
#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-
354328
/// <summary>
355329
/// An assignment operator for setting this Vector equal to the average of an std::deque of Vectors.
356330
/// </summary>
@@ -362,7 +336,7 @@ namespace RTE {
362336
/// Unary negation overload for single Vectors.
363337
/// </summary>
364338
/// <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); }
366340

367341
/// <summary>
368342
/// An equality operator for testing if any two Vectors are equal.
@@ -378,7 +352,7 @@ namespace RTE {
378352
/// <param name="lhs">A Vector reference as the left hand side operand.</param>
379353
/// <param name="rhs">A Vector reference as the right hand side operand.</param>
380354
/// <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); }
382356

383357
/// <summary>
384358
/// A stream insertion operator for sending a Vector to an output stream.
@@ -388,13 +362,6 @@ namespace RTE {
388362
/// <returns>An ostream reference for further use in an expression.</returns>
389363
friend std::ostream & operator<<(std::ostream &stream, const Vector &operand) { stream << "{" << operand.m_X << ", " << operand.m_Y << "}"; return stream; }
390364

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-
398365
/// <summary>
399366
/// Addition operator overload for Vectors.
400367
/// </summary>
@@ -440,7 +407,7 @@ namespace RTE {
440407
/// <returns>The resulting Vector.</returns>
441408
Vector operator/(const float &rhs) const {
442409
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); }
444411
return returnVector;
445412
}
446413

@@ -452,7 +419,7 @@ namespace RTE {
452419
/// <returns>The resulting Vector.</returns>
453420
friend Vector operator/(const Vector &lhs, const Vector &rhs) {
454421
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); }
456423
return returnVector;
457424
}
458425

@@ -506,7 +473,7 @@ namespace RTE {
506473
/// </summary>
507474
/// <param name="rhs">A float reference as the right hand side operand.</param>
508475
/// <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; }
510477

511478
/// <summary>
512479
/// Self-division operator overload for Vectors.
@@ -515,8 +482,8 @@ namespace RTE {
515482
/// <param name="rhs">A Vector reference as the right hand side operand.</param>
516483
/// <returns>A reference to the resulting Vector (the left one).</returns>
517484
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; }
520487
return lhs;
521488
}
522489

@@ -546,13 +513,6 @@ namespace RTE {
546513
protected:
547514

548515
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; }
556516
};
557517
}
558518
#endif

0 commit comments

Comments
 (0)