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

Commit ac15bcb

Browse files
authored
Merge pull request #203 from cortex-command-community/vector-setmagnitude
Update Vector::SetMagnitude
2 parents 40eab02 + c3fef2e commit ac15bcb

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

System/Vector.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ namespace RTE {
3535
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3636

3737
Vector & Vector::SetMagnitude(float newMag) {
38-
Vector temp(*this);
39-
SetXY(newMag, 0);
40-
AbsRotateTo(temp);
38+
if (IsZero()) {
39+
SetXY(newMag, 0.0F);
40+
} else {
41+
*this *= (newMag / GetMagnitude());
42+
}
4143
return *this;
4244
}
4345

@@ -52,16 +54,16 @@ namespace RTE {
5254
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
5355

5456
float Vector::GetAbsRadAngle() const {
55-
float radAngle = -std::atan2(m_Y, m_X);
57+
const float radAngle = -std::atan2(m_Y, m_X);
5658
return (radAngle < -c_HalfPI) ? (radAngle + c_TwoPI) : radAngle;
5759
}
5860

5961
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
6062

6163
Vector & Vector::RadRotate(float angle) {
6264
angle = -angle;
63-
float tempX = m_X * std::cos(angle) - m_Y * std::sin(angle);
64-
float tempY = m_X * std::sin(angle) + m_Y * std::cos(angle);
65+
const float tempX = m_X * std::cos(angle) - m_Y * std::sin(angle);
66+
const float tempY = m_X * std::sin(angle) + m_Y * std::cos(angle);
6567
m_X = tempX;
6668
m_Y = tempY;
6769

@@ -86,7 +88,7 @@ namespace RTE {
8688
for (const Vector &vector : rhs) {
8789
*this += vector;
8890
}
89-
*this /= rhs.size();
91+
*this /= static_cast<float>(rhs.size());
9092
}
9193
return *this;
9294
}

System/Vector.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ namespace RTE {
190190
float GetMagnitude() const { return std::sqrt(std::pow(m_X, 2.0F) + std::pow(m_Y, 2.0F)); }
191191

192192
/// <summary>
193-
/// Sets the magnitude of this Vector and keeps its angle intact.
193+
/// Sets the magnitude of this Vector. A negative magnitude will invert the Vector's direction.
194194
/// </summary>
195195
/// <param name="newMag">A float value that the magnitude will be set to.</param>
196196
/// <returns>A reference to this after the change.</returns>
@@ -312,13 +312,13 @@ namespace RTE {
312312
/// Returns the greatest integer that is not greater than the X value of this Vector.
313313
/// </summary>
314314
/// <returns>An int value that represents the X value of this Vector.</returns>
315-
int GetFloorIntX() const { return static_cast<int>(std::floor(m_X)); }
315+
int GetFloorIntX() const { return static_cast<int>(m_X); }
316316

317317
/// <summary>
318318
/// Returns the greatest integer that is not greater than the Y value of this Vector.
319319
/// </summary>
320320
/// <returns>An int value that represents the Y value of this Vector.</returns>
321-
int GetFloorIntY() const { return static_cast<int>(std::floor(m_Y)); }
321+
int GetFloorIntY() const { return static_cast<int>(m_Y); }
322322

323323
/// <summary>
324324
/// Returns a ceilinged copy of this Vector. Does not alter this Vector.

0 commit comments

Comments
 (0)