From d3e2ded5d7c68a4b2fcead09163e5ebd4d59abe5 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 15 Apr 2022 16:03:18 +0200 Subject: [PATCH] Added Vector2::one and Vector3::one --- include/Vector2.h | 400 +++++++++++++++++++++++----------------------- include/Vector3.h | 4 + src/Vector2.cpp | 1 + src/Vector3.cpp | 1 + 4 files changed, 208 insertions(+), 198 deletions(-) diff --git a/include/Vector2.h b/include/Vector2.h index cc835dd..34e062f 100644 --- a/include/Vector2.h +++ b/include/Vector2.h @@ -6,22 +6,22 @@ #define VECTOR2_H extern "C" { -/// -/// 2-dimensional Vector representation -/// -/// This is a C-style implementation -/// This uses the right-handed coordinate system. -typedef struct Vec2 { - /// - /// The right axis of the vector - /// - float x; - /// - /// The upward/forward axis of the vector - /// - float y; + /// + /// 2-dimensional Vector representation + /// + /// This is a C-style implementation + /// This uses the right-handed coordinate system. + typedef struct Vec2 { + /// + /// The right axis of the vector + /// + float x; + /// + /// The upward/forward axis of the vector + /// + float y; -} Vec2; + } Vec2; } /// @@ -29,203 +29,207 @@ typedef struct Vec2 { /// /// This uses the right-handed coordinate system. struct Vector2 : Vec2 { - public: - /// - /// Create a new 2-dimensinal zero vector - /// - Vector2(); - /// - /// Create a new 2-dimensional vector - /// - /// x axis value - /// y axis value - Vector2(float x, float y); - /// - /// Create a vector from C-style Vec2 - /// - /// The C-style Vec - Vector2(Vec2 v); +public: + /// + /// Create a new 2-dimensinal zero vector + /// + Vector2(); + /// + /// Create a new 2-dimensional vector + /// + /// x axis value + /// y axis value + Vector2(float x, float y); + /// + /// Create a vector from C-style Vec2 + /// + /// The C-style Vec + Vector2(Vec2 v); - ~Vector2(); + ~Vector2(); - /// - /// A vector with zero for all axis - /// - const static Vector2 zero; - /// - /// A vector with values (1, 0) - /// - const static Vector2 right; - /// - /// A vector3 with values (-1, 0) - /// - const static Vector2 left; - /// - /// A vector with values (0, 1) - /// - const static Vector2 up; - /// - /// A vector with values (0, -1) - /// - const static Vector2 down; - /// - /// A vector with values (0, 1) - /// - const static Vector2 forward; - /// - /// A vector with values (0, -1) - /// - const static Vector2 back; + /// + /// A vector with zero for all axis + /// + const static Vector2 zero; + /// + /// A vector with values (1, 1) + /// + const static Vector2 one; + /// + /// A vector with values (1, 0) + /// + const static Vector2 right; + /// + /// A vector3 with values (-1, 0) + /// + const static Vector2 left; + /// + /// A vector with values (0, 1) + /// + const static Vector2 up; + /// + /// A vector with values (0, -1) + /// + const static Vector2 down; + /// + /// A vector with values (0, 1) + /// + const static Vector2 forward; + /// + /// A vector with values (0, -1) + /// + const static Vector2 back; - /// - /// The length of a vector - /// - /// The vector for which you need the length - /// The length of the given vector - static float Magnitude(const Vector2& vector); - /// - /// The length of this vector - /// - /// The length of this vector - float magnitude() const; - /// - /// The squared length of a vector - /// - /// The vector for which you need the squared - /// length The squatred length 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); - /// - /// The squared length of this vector - /// - /// The squared length - /// 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. - float sqrMagnitude() const; - /// - /// Connvert a vector to a length of 1 - /// - /// The vector to convert - /// The vector with length 1 - static Vector2 Normalize(Vector2 vector); - /// - /// Convert the vector to a length of a - /// - /// The vector with length 1 - Vector2 normalized() const; + /// + /// The length of a vector + /// + /// The vector for which you need the length + /// The length of the given vector + static float Magnitude(const Vector2& vector); + /// + /// The length of this vector + /// + /// The length of this vector + float magnitude() const; + /// + /// The squared length of a vector + /// + /// The vector for which you need the squared + /// length The squatred length 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); + /// + /// The squared length of this vector + /// + /// The squared length + /// 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. + float sqrMagnitude() const; + /// + /// Connvert a vector to a length of 1 + /// + /// The vector to convert + /// The vector with length 1 + static Vector2 Normalize(Vector2 vector); + /// + /// Convert the vector to a length of a + /// + /// The vector with length 1 + Vector2 normalized() const; - /// - /// Negate the vector - /// - /// The negated vector - /// This will result in a vector pointing in the opposite direction - Vector2 operator-(); - /// - /// Subtract a vector from this vector - /// - /// The vector to subtract from this vector - /// The result of the subtraction - Vector2 operator-(const Vector2& vector) const; + /// + /// Negate the vector + /// + /// The negated vector + /// This will result in a vector pointing in the opposite direction + Vector2 operator-(); + /// + /// Subtract a vector from this vector + /// + /// The vector to subtract from this vector + /// The result of the subtraction + Vector2 operator-(const Vector2& vector) const; - /// - /// Add another vector to this vector - /// - /// The vector to add - /// The result of adding the vector - Vector2 operator+(const Vector2& vector2) const; + /// + /// Add another vector to this vector + /// + /// The vector to add + /// The result of adding the vector + Vector2 operator+(const Vector2& vector2) const; - /// - /// Scale a vector using another vector - /// - /// The vector to scale - /// A vector with scaling factors - /// The scaled vector - /// 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); - /// - /// Scale a vector uniformly up - /// - /// The scaling factor - /// The scaled vector - /// Each component of the vector will be multipled with the same factor. - Vector2 operator*(float factor) const; - /// - /// Scale a vector uniformy down - /// - /// The scaling factor - /// The scaled vector - /// Each componet of the vector will be divided by the same factor. - Vector2 operator/(const float& factor); + /// + /// Scale a vector using another vector + /// + /// The vector to scale + /// A vector with scaling factors + /// The scaled vector + /// 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); + /// + /// Scale a vector uniformly up + /// + /// The scaling factor + /// The scaled vector + /// Each component of the vector will be multipled with the same factor. + Vector2 operator*(float factor) const; + /// + /// Scale a vector uniformy down + /// + /// The scaling factor + /// The scaled vector + /// Each componet of the vector will be divided by the same factor. + Vector2 operator/(const float& factor); - /// - /// The dot product of two vectors - /// - /// The first vector - /// The second vector - /// The dot product of the two vectors - static float Dot(const Vector2& vector1, const Vector2& vector2); + /// + /// The dot product of two vectors + /// + /// The first vector + /// The second vector + /// The dot product of the two vectors + static float Dot(const Vector2& vector1, const Vector2& vector2); - /// - /// Check is this vector is equal to the given vector - /// - /// The vector to check against - /// True if it is identical to the given vector - /// Note this uses float comparison to check equality which - /// may have strange effects. Equality on float should be avoided. - bool operator==(const Vector2& vector); + /// + /// Check is this vector is equal to the given vector + /// + /// The vector to check against + /// True if it is identical to the given vector + /// Note this uses float comparison to check equality which + /// may have strange effects. Equality on float should be avoided. + bool operator==(const Vector2& vector); - /// - /// The distance between two vectors - /// - /// The first vector - /// The second vectors - /// The distance between the two vectors - static float Distance(const Vector2& vector1, const Vector2& vector2); + /// + /// The distance between two vectors + /// + /// The first vector + /// The second vectors + /// The distance between the two vectors + static float Distance(const Vector2& vector1, const Vector2& vector2); - /// - /// Calculate the angle between two vectors - /// - /// The first vector - /// The second vector - /// The angle - /// This reterns an unsigned angle which is the shortest distance - /// between the two vectors. Use Vector3::SignedAngle if a - /// signed angle is needed. - static float Angle(Vector2 vector1, Vector2 vector2); + /// + /// Calculate the angle between two vectors + /// + /// The first vector + /// The second vector + /// The angle + /// This reterns an unsigned angle which is the shortest distance + /// between the two vectors. Use Vector3::SignedAngle if a + /// signed angle is needed. + static float Angle(Vector2 vector1, Vector2 vector2); - /// - /// Calculate the angle between two vectors rotation around an axis. - /// - /// The starting vector - /// The ending vector - /// The axis to rotate around - /// The signed angle - static float SignedAngle(Vector2 from, Vector2 to); + /// + /// Calculate the angle between two vectors rotation around an axis. + /// + /// The starting vector + /// The ending vector + /// The axis to rotate around + /// The signed angle + static float SignedAngle(Vector2 from, Vector2 to); - /// - /// Rotate the vector - /// - /// The vector to rotate - /// Angle in radias to rotate - /// The rotated vector - static Vector2 Rotate(Vector2 v, float angle); + /// + /// Rotate the vector + /// + /// The vector to rotate + /// Angle in radias to rotate + /// The rotated vector + static Vector2 Rotate(Vector2 v, float angle); - /// - /// Lerp between two vectors - /// - /// The from vector - /// The to vector - /// The interpolation distance (0..1) - /// The lerped vector - /// 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); + /// + /// Lerp between two vectors + /// + /// The from vector + /// The to vector + /// The interpolation distance (0..1) + /// The lerped vector + /// 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); + static float ToFactor(Vector2 a, Vector2 b); }; #endif \ No newline at end of file diff --git a/include/Vector3.h b/include/Vector3.h index 8f4d31d..7001f90 100644 --- a/include/Vector3.h +++ b/include/Vector3.h @@ -57,6 +57,10 @@ public: /// const static Vector3 zero; /// + /// A vector with one for all axis + /// + const static Vector3 one; + /// /// A vector with values (1, 0, 0) /// const static Vector3 right; diff --git a/src/Vector2.cpp b/src/Vector2.cpp index c65c40d..63479c9 100644 --- a/src/Vector2.cpp +++ b/src/Vector2.cpp @@ -25,6 +25,7 @@ Vector2::Vector2(Vec2 v) { Vector2::~Vector2() {} const Vector2 Vector2::zero = Vector2(0, 0); +const Vector2 Vector2::one = Vector2(1, 1); const Vector2 Vector2::right = Vector2(1, 0); const Vector2 Vector2::left = Vector2(-1, 0); const Vector2 Vector2::up = Vector2(0, 1); diff --git a/src/Vector3.cpp b/src/Vector3.cpp index cda2a6d..69e0414 100644 --- a/src/Vector3.cpp +++ b/src/Vector3.cpp @@ -31,6 +31,7 @@ Vector3::~Vector3() { } const Vector3 Vector3::zero = Vector3(0, 0, 0); +const Vector3 Vector3::one = Vector3(1, 1, 1); const Vector3 Vector3::right = Vector3(1, 0, 0); const Vector3 Vector3::left = Vector3(-1, 0, 0); const Vector3 Vector3::up = Vector3(0, 1, 0);