Add ClampMagnitude

This commit is contained in:
Pascal Serrarens 2024-04-17 15:20:12 +02:00
parent d01c5d9de5
commit bfa1123994
2 changed files with 214 additions and 203 deletions

View File

@ -63,6 +63,15 @@ Vector2 Vector2::normalized() const {
return result;
}
Vector2 Vector2::ClampMagnitude(const Vector2 &v, float magnitude) {
float length = Vector2::Magnitude(v);
Vector2 r = v;
if (length > magnitude)
r = v / (length * magnitude);
return r;
}
Vector2 Vector2::operator-(const Vector2 &v2) const {
return Vector2(this->x - v2.x, this->y - v2.y);
}
@ -81,7 +90,7 @@ Vector2 Vector2::operator*(float f) const {
return Vector2(this->x * f, this->y * f);
}
Vector2 Vector2::operator/(const float &d) {
Vector2 Vector2::operator/(const float &d) const {
return Vector2(this->x / d, this->y / d);
}

View File

@ -6,12 +6,12 @@
#define VECTOR2_H
extern "C" {
/// <summary>
/// 2-dimensional Vector representation
/// </summary>
/// This is a C-style implementation
/// This uses the right-handed coordinate system.
typedef struct Vec2 {
/// <summary>
/// 2-dimensional Vector representation
/// </summary>
/// This is a C-style implementation
/// This uses the right-handed coordinate system.
typedef struct Vec2 {
/// <summary>
/// The right axis of the vector
/// </summary>
@ -21,7 +21,7 @@ extern "C" {
/// </summary>
float y;
} Vec2;
} Vec2;
}
/// <summary>
@ -59,7 +59,7 @@ public:
/// <summary>
/// A vector with values (1, 0)
/// </summary>
///
///
const static Vector2 right;
/// <summary>
/// A vector3 with values (-1, 0)
@ -87,7 +87,7 @@ public:
/// </summary>
/// <param name="vector">The vector for which you need the length</param>
/// <returns>The length of the given vector</returns>
static float Magnitude(const Vector2& vector);
static float Magnitude(const Vector2 &vector);
/// <summary>
/// The length of this vector
/// </summary>
@ -100,7 +100,7 @@ public:
/// length</param> <returns>The squatred length</returns> The squared length
/// is computationally simpler than the real length. Think of Pythagoras A^2 +
/// B^2 = C^2. This leaves out the calculation of the squared root of C.
static float SqrMagnitude(const Vector2& vector);
static float SqrMagnitude(const Vector2 &vector);
/// <summary>
/// The squared length of this vector
/// </summary>
@ -121,6 +121,8 @@ public:
/// <returns>The vector with length 1</returns>
Vector2 normalized() const;
static Vector2 ClampMagnitude(const Vector2 &v, float magnitude);
/// <summary>
/// Negate the vector
/// </summary>
@ -132,14 +134,14 @@ public:
/// </summary>
/// <param name="vector">The vector to subtract from this vector</param>
/// <returns>The result of the subtraction</returns>
Vector2 operator-(const Vector2& vector) const;
Vector2 operator-(const Vector2 &vector) const;
/// <summary>
/// Add another vector to this vector
/// </summary>
/// <param name="vector2">The vector to add</param>
/// <returns>The result of adding the vector</returns>
Vector2 operator+(const Vector2& vector2) const;
Vector2 operator+(const Vector2 &vector2) const;
/// <summary>
/// Scale a vector using another vector
@ -149,7 +151,7 @@ public:
/// <returns>The scaled vector</returns>
/// Each component of the vector v1 will be multiplied with the
/// component from the scaling vector v2.
static Vector2 Scale(const Vector2& vector1, const Vector2& vector2);
static Vector2 Scale(const Vector2 &vector1, const Vector2 &vector2);
/// <summary>
/// Scale a vector uniformly up
/// </summary>
@ -163,7 +165,7 @@ public:
/// <param name="factor">The scaling factor</param>
/// <returns>The scaled vector</returns>
/// Each componet of the vector will be divided by the same factor.
Vector2 operator/(const float& factor);
Vector2 operator/(const float &factor) const;
/// <summary>
/// The dot product of two vectors
@ -171,7 +173,7 @@ public:
/// <param name="vector1">The first vector</param>
/// <param name="vector2">The second vector</param>
/// <returns>The dot product of the two vectors</returns>
static float Dot(const Vector2& vector1, const Vector2& vector2);
static float Dot(const Vector2 &vector1, const Vector2 &vector2);
/// <summary>
/// Check is this vector is equal to the given vector
@ -180,7 +182,7 @@ public:
/// <returns>True if it is identical to the given vector</returns>
/// Note this uses float comparison to check equality which
/// may have strange effects. Equality on float should be avoided.
bool operator==(const Vector2& vector);
bool operator==(const Vector2 &vector);
/// <summary>
/// The distance between two vectors
@ -188,7 +190,7 @@ public:
/// <param name="vector1">The first vector</param>
/// <param name="vector2">The second vectors</param>
/// <returns>The distance between the two vectors</returns>
static float Distance(const Vector2& vector1, const Vector2& vector2);
static float Distance(const Vector2 &vector1, const Vector2 &vector2);
/// <summary>
/// Calculate the angle between two vectors