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; 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);
} }

406
Vector2.h
View File

@ -6,22 +6,22 @@
#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>
float x; float x;
/// <summary> /// <summary>
/// The upward/forward axis of the vector /// The upward/forward axis of the vector
/// </summary> /// </summary>
float y; float y;
} Vec2; } Vec2;
} }
/// <summary> /// <summary>
@ -30,207 +30,209 @@ extern "C" {
/// This uses the right-handed coordinate system. /// This uses the right-handed coordinate system.
struct Vector2 : Vec2 { struct Vector2 : Vec2 {
public: public:
/// <summary> /// <summary>
/// Create a new 2-dimensinal zero vector /// Create a new 2-dimensinal zero vector
/// </summary> /// </summary>
Vector2(); Vector2();
/// <summary> /// <summary>
/// Create a new 2-dimensional vector /// Create a new 2-dimensional vector
/// </summary> /// </summary>
/// <param name="x">x axis value</param> /// <param name="x">x axis value</param>
/// <param name="y">y axis value</param> /// <param name="y">y axis value</param>
Vector2(float x, float y); Vector2(float x, float y);
/// <summary> /// <summary>
/// Create a vector from C-style Vec2 /// Create a vector from C-style Vec2
/// </summary> /// </summary>
/// <param name="v">The C-style Vec</param> /// <param name="v">The C-style Vec</param>
Vector2(Vec2 v); Vector2(Vec2 v);
~Vector2(); ~Vector2();
/// <summary> /// <summary>
/// A vector with zero for all axis /// A vector with zero for all axis
/// </summary> /// </summary>
const static Vector2 zero; const static Vector2 zero;
/// <summary> /// <summary>
/// A vector with values (1, 1) /// A vector with values (1, 1)
/// </summary> /// </summary>
const static Vector2 one; const static Vector2 one;
/// <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)
/// </summary> /// </summary>
const static Vector2 left; const static Vector2 left;
/// <summary> /// <summary>
/// A vector with values (0, 1) /// A vector with values (0, 1)
/// </summary> /// </summary>
const static Vector2 up; const static Vector2 up;
/// <summary> /// <summary>
/// A vector with values (0, -1) /// A vector with values (0, -1)
/// </summary> /// </summary>
const static Vector2 down; const static Vector2 down;
/// <summary> /// <summary>
/// A vector with values (0, 1) /// A vector with values (0, 1)
/// </summary> /// </summary>
const static Vector2 forward; const static Vector2 forward;
/// <summary> /// <summary>
/// A vector with values (0, -1) /// A vector with values (0, -1)
/// </summary> /// </summary>
const static Vector2 back; const static Vector2 back;
/// <summary> /// <summary>
/// The length of a vector /// The length of a vector
/// </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>
/// <returns>The length of this vector</returns> /// <returns>The length of this vector</returns>
float magnitude() const; float magnitude() const;
/// <summary> /// <summary>
/// The squared length of a vector /// The squared length of a vector
/// </summary> /// </summary>
/// <param name="vector">The vector for which you need the squared /// <param name="vector">The vector for which you need the squared
/// 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>
/// <returns>The squared length</returns> /// <returns>The squared length</returns>
/// The squared length is computationally simpler than the real length. /// The squared length is computationally simpler than the real length.
/// Think of Pythagoras A^2 + B^2 = C^2. /// Think of Pythagoras A^2 + B^2 = C^2.
/// This leaves out the calculation of the squared root of C. /// This leaves out the calculation of the squared root of C.
float sqrMagnitude() const; float sqrMagnitude() const;
/// <summary> /// <summary>
/// Connvert a vector to a length of 1 /// Connvert a vector to a length of 1
/// </summary> /// </summary>
/// <param name="vector">The vector to convert</param> /// <param name="vector">The vector to convert</param>
/// <returns>The vector with length 1</returns> /// <returns>The vector with length 1</returns>
static Vector2 Normalize(Vector2 vector); static Vector2 Normalize(Vector2 vector);
/// <summary> /// <summary>
/// Convert the vector to a length of a /// Convert the vector to a length of a
/// </summary> /// </summary>
/// <returns>The vector with length 1</returns> /// <returns>The vector with length 1</returns>
Vector2 normalized() const; Vector2 normalized() const;
/// <summary> static Vector2 ClampMagnitude(const Vector2 &v, float magnitude);
/// Negate the vector
/// </summary>
/// <returns>The negated vector</returns>
/// This will result in a vector pointing in the opposite direction
Vector2 operator-();
/// <summary>
/// Subtract a vector from this vector
/// </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;
/// <summary> /// <summary>
/// Add another vector to this vector /// Negate the vector
/// </summary> /// </summary>
/// <param name="vector2">The vector to add</param> /// <returns>The negated vector</returns>
/// <returns>The result of adding the vector</returns> /// This will result in a vector pointing in the opposite direction
Vector2 operator+(const Vector2& vector2) const; Vector2 operator-();
/// <summary>
/// Subtract a vector from this vector
/// </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;
/// <summary> /// <summary>
/// Scale a vector using another vector /// Add another vector to this vector
/// </summary> /// </summary>
/// <param name="vector1">The vector to scale</param> /// <param name="vector2">The vector to add</param>
/// <param name="vector2">A vector with scaling factors</param> /// <returns>The result of adding the vector</returns>
/// <returns>The scaled vector</returns> Vector2 operator+(const Vector2 &vector2) const;
/// 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);
/// <summary>
/// Scale a vector uniformly up
/// </summary>
/// <param name="factor">The scaling factor</param>
/// <returns>The scaled vector</returns>
/// Each component of the vector will be multipled with the same factor.
Vector2 operator*(float factor) const;
/// <summary>
/// Scale a vector uniformy down
/// </summary>
/// <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);
/// <summary> /// <summary>
/// The dot product of two vectors /// Scale a vector using another vector
/// </summary> /// </summary>
/// <param name="vector1">The first vector</param> /// <param name="vector1">The vector to scale</param>
/// <param name="vector2">The second vector</param> /// <param name="vector2">A vector with scaling factors</param>
/// <returns>The dot product of the two vectors</returns> /// <returns>The scaled vector</returns>
static float Dot(const Vector2& vector1, const Vector2& vector2); /// 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);
/// <summary>
/// Scale a vector uniformly up
/// </summary>
/// <param name="factor">The scaling factor</param>
/// <returns>The scaled vector</returns>
/// Each component of the vector will be multipled with the same factor.
Vector2 operator*(float factor) const;
/// <summary>
/// Scale a vector uniformy down
/// </summary>
/// <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) const;
/// <summary> /// <summary>
/// Check is this vector is equal to the given vector /// The dot product of two vectors
/// </summary> /// </summary>
/// <param name="vector">The vector to check against</param> /// <param name="vector1">The first vector</param>
/// <returns>True if it is identical to the given vector</returns> /// <param name="vector2">The second vector</param>
/// Note this uses float comparison to check equality which /// <returns>The dot product of the two vectors</returns>
/// may have strange effects. Equality on float should be avoided. static float Dot(const Vector2 &vector1, const Vector2 &vector2);
bool operator==(const Vector2& vector);
/// <summary> /// <summary>
/// The distance between two vectors /// Check is this vector is equal to the given vector
/// </summary> /// </summary>
/// <param name="vector1">The first vector</param> /// <param name="vector">The vector to check against</param>
/// <param name="vector2">The second vectors</param> /// <returns>True if it is identical to the given vector</returns>
/// <returns>The distance between the two vectors</returns> /// Note this uses float comparison to check equality which
static float Distance(const Vector2& vector1, const Vector2& vector2); /// may have strange effects. Equality on float should be avoided.
bool operator==(const Vector2 &vector);
/// <summary> /// <summary>
/// Calculate the angle between two vectors /// The distance between two vectors
/// </summary> /// </summary>
/// <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 vectors</param>
/// <returns>The angle</returns> /// <returns>The distance between the two vectors</returns>
/// This reterns an unsigned angle which is the shortest distance static float Distance(const Vector2 &vector1, const Vector2 &vector2);
/// between the two vectors. Use Vector3::SignedAngle if a
/// signed angle is needed.
static float Angle(Vector2 vector1, Vector2 vector2);
/// <summary> /// <summary>
/// Calculate the angle between two vectors rotation around an axis. /// Calculate the angle between two vectors
/// </summary> /// </summary>
/// <param name="from">The starting vector</param> /// <param name="vector1">The first vector</param>
/// <param name="to">The ending vector</param> /// <param name="vector2">The second vector</param>
/// <param name="axis">The axis to rotate around</param> /// <returns>The angle</returns>
/// <returns>The signed angle</returns> /// This reterns an unsigned angle which is the shortest distance
static float SignedAngle(Vector2 from, Vector2 to); /// between the two vectors. Use Vector3::SignedAngle if a
/// signed angle is needed.
static float Angle(Vector2 vector1, Vector2 vector2);
/// <summary> /// <summary>
/// Rotate the vector /// Calculate the angle between two vectors rotation around an axis.
/// </summary> /// </summary>
/// <param name="v">The vector to rotate</param> /// <param name="from">The starting vector</param>
/// <param name="angle">Angle in radias to rotate</param> /// <param name="to">The ending vector</param>
/// <returns>The rotated vector</returns> /// <param name="axis">The axis to rotate around</param>
static Vector2 Rotate(Vector2 v, float angle); /// <returns>The signed angle</returns>
static float SignedAngle(Vector2 from, Vector2 to);
/// <summary> /// <summary>
/// Lerp between two vectors /// Rotate the vector
/// </summary> /// </summary>
/// <param name="from">The from vector</param> /// <param name="v">The vector to rotate</param>
/// <param name="to">The to vector</param> /// <param name="angle">Angle in radias to rotate</param>
/// <param name="f">The interpolation distance (0..1)</param> /// <returns>The rotated vector</returns>
/// <returns>The lerped vector</returns> static Vector2 Rotate(Vector2 v, float angle);
/// The factor f is unclamped. Value 0 matches the *from* vector, Value 1
/// matches the *to* vector Value -1 is *from* vector minus the difference
/// between *from* and *to* etc.
static Vector2 Lerp(Vector2 from, Vector2 to, float f);
static float ToFactor(Vector2 a, Vector2 b); /// <summary>
/// Lerp between two vectors
/// </summary>
/// <param name="from">The from vector</param>
/// <param name="to">The to vector</param>
/// <param name="f">The interpolation distance (0..1)</param>
/// <returns>The lerped vector</returns>
/// The factor f is unclamped. Value 0 matches the *from* vector, Value 1
/// matches the *to* vector Value -1 is *from* vector minus the difference
/// between *from* and *to* etc.
static Vector2 Lerp(Vector2 from, Vector2 to, float f);
static float ToFactor(Vector2 a, Vector2 b);
}; };
#endif #endif