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