Added Vector2::Rotate, Moved Rad2Deg/Deg2Rad to Angle class

This commit is contained in:
Pascal Serrarens 2022-03-29 13:54:08 +02:00
parent 307a94c567
commit c1a926582e
5 changed files with 296 additions and 268 deletions

View File

@ -6,7 +6,10 @@
#define ANGLE_H #define ANGLE_H
class Angle { class Angle {
public: public:
const static float Rad2Deg;
const static float Deg2Rad;
static float Normalize(float angle); static float Normalize(float angle);
static float Clamp(float angle, float min, float max); static float Clamp(float angle, float min, float max);
static float Difference(float a, float b); static float Difference(float a, float b);

View File

@ -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>
@ -29,7 +29,7 @@ extern "C" {
/// </summary> /// </summary>
/// 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>
@ -91,11 +91,10 @@ public:
/// <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 length</param> /// <param name="vector">The vector for which you need the squared
/// <returns>The squatred length</returns> /// length</param> <returns>The squatred length</returns> The squared length
/// The squared length is computationally simpler than the real length. /// is computationally simpler than the real length. Think of Pythagoras A^2 +
/// Think of Pythagoras A^2 + B^2 = C^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.
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
@ -122,20 +121,20 @@ public:
/// </summary> /// </summary>
/// <returns>The negated vector</returns> /// <returns>The negated vector</returns>
/// This will result in a vector pointing in the opposite direction /// This will result in a vector pointing in the opposite direction
Vector2 operator -(); Vector2 operator-();
/// <summary> /// <summary>
/// Subtract a vector from this vector /// Subtract a vector from this vector
/// </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
@ -152,14 +151,14 @@ 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 component of the vector will be multipled with the same factor. /// Each component of the vector will be multipled with the same factor.
Vector2 operator *(float factor) const; Vector2 operator*(float factor) const;
/// <summary> /// <summary>
/// Scale a vector uniformy down /// Scale a vector uniformy down
/// </summary> /// </summary>
/// <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);
/// <summary> /// <summary>
/// The dot product of two vectors /// The dot product of two vectors
@ -176,7 +175,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
@ -191,7 +190,7 @@ public:
/// </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 vector</param>
/// <returns></returns> /// <returns>The angle</returns>
/// This reterns an unsigned angle which is the shortest distance /// This reterns an unsigned angle which is the shortest distance
/// between the two vectors. Use Vector3::SignedAngle if a /// between the two vectors. Use Vector3::SignedAngle if a
/// signed angle is needed. /// signed angle is needed.
@ -206,6 +205,14 @@ public:
/// <returns>The signed angle</returns> /// <returns>The signed angle</returns>
static float SignedAngle(Vector2 from, Vector2 to); static float SignedAngle(Vector2 from, Vector2 to);
/// <summary>
/// Rotate the vector
/// </summary>
/// <param name="v">The vector to rotate</param>
/// <param name="angle">Angle in radias to rotate</param>
/// <returns>The rotated vector</returns>
static Vector2 Rotate(Vector2 v, float angle);
/// <summary> /// <summary>
/// Lerp between two vectors /// Lerp between two vectors
/// </summary> /// </summary>
@ -213,8 +220,9 @@ public:
/// <param name="to">The to vector</param> /// <param name="to">The to vector</param>
/// <param name="f">The interpolation distance (0..1)</param> /// <param name="f">The interpolation distance (0..1)</param>
/// <returns>The lerped vector</returns> /// <returns>The lerped vector</returns>
/// The factor f is unclamped. Value 0 matches the *from* vector, Value 1 matches the *to* vector /// The factor f is unclamped. Value 0 matches the *from* vector, Value 1
/// Value -1 is *from* vector minus the difference between *from* and *to* etc. /// 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 Vector2 Lerp(Vector2 from, Vector2 to, float f);
static float ToFactor(Vector2 a, Vector2 b); static float ToFactor(Vector2 a, Vector2 b);

View File

@ -2,16 +2,21 @@
// License, v. 2.0.If a copy of the MPL was not distributed with this // License, v. 2.0.If a copy of the MPL was not distributed with this
// file, You can obtain one at https ://mozilla.org/MPL/2.0/. // file, You can obtain one at https ://mozilla.org/MPL/2.0/.
#include <math.h>
#include "Angle.h" #include "Angle.h"
#include <math.h>
#include "FloatSingle.h" #include "FloatSingle.h"
const float Angle::Rad2Deg = 57.29578F;
const float Angle::Deg2Rad = 0.0174532924F;
float Angle::Normalize(float angle) { float Angle::Normalize(float angle) {
if (!isfinite(angle)) if (!isfinite(angle))
return angle; return angle;
while (angle <= -180) angle += 360; while (angle <= -180)
while (angle > 180) angle -= 360; angle += 360;
while (angle > 180)
angle -= 360;
return angle; return angle;
} }
@ -30,5 +35,5 @@ float Angle::MoveTowards(float fromAngle, float toAngle, float maxAngle) {
float d = toAngle - fromAngle; float d = toAngle - fromAngle;
float sign = signbit(d) ? -1 : 1; float sign = signbit(d) ? -1 : 1;
d = sign * Float::Clamp(fabs(d), 0, maxAngle); d = sign * Float::Clamp(fabs(d), 0, maxAngle);
return d; return fromAngle + d;
} }

View File

@ -2,12 +2,10 @@
// License, v. 2.0.If a copy of the MPL was not distributed with this // License, v. 2.0.If a copy of the MPL was not distributed with this
// file, You can obtain one at https ://mozilla.org/MPL/2.0/. // file, You can obtain one at https ://mozilla.org/MPL/2.0/.
#include <math.h>
#include "Vector2.h" #include "Vector2.h"
#include <math.h>
const float Deg2Rad = 0.0174532924F; #include "Angle.h"
const float Rad2Deg = 57.29578F; #include "FloatSingle.h"
const float epsilon = 1E-05f;
Vector2::Vector2() { Vector2::Vector2() {
x = 0; x = 0;
@ -24,8 +22,7 @@ Vector2::Vector2(Vec2 v) {
y = v.y; y = v.y;
} }
Vector2::~Vector2() { Vector2::~Vector2() {}
}
const Vector2 Vector2::zero = Vector2(0, 0); const Vector2 Vector2::zero = Vector2(0, 0);
const Vector2 Vector2::right = Vector2(1, 0); const Vector2 Vector2::right = Vector2(1, 0);
@ -46,13 +43,13 @@ float Vector2::SqrMagnitude(const Vector2& a) {
return a.x * a.x + a.y * a.y; return a.x * a.x + a.y * a.y;
} }
float Vector2::sqrMagnitude() const { float Vector2::sqrMagnitude() const {
return(x * x + y * y); return (x * x + y * y);
} }
Vector2 Vector2::Normalize(Vector2 v) { Vector2 Vector2::Normalize(Vector2 v) {
float num = Vector2::Magnitude(v); float num = Vector2::Magnitude(v);
Vector2 result = Vector2::zero; Vector2 result = Vector2::zero;
if (num > epsilon) { if (num > Float::epsilon) {
result = v / num; result = v / num;
} }
return result; return result;
@ -60,21 +57,21 @@ Vector2 Vector2::Normalize(Vector2 v) {
Vector2 Vector2::normalized() const { Vector2 Vector2::normalized() const {
float num = this->magnitude(); float num = this->magnitude();
Vector2 result = Vector2::zero; Vector2 result = Vector2::zero;
if (num > epsilon) { if (num > Float::epsilon) {
result = ((Vector2)*this) / num; result = ((Vector2) * this) / num;
} }
return result; return result;
} }
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);
} }
Vector2 Vector2::operator -() { Vector2 Vector2::operator-() {
return Vector2(-this->x, -this->y); return Vector2(-this->x, -this->y);
} }
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);
} }
@ -82,7 +79,7 @@ Vector2 Vector2::Scale(const Vector2& p1, const Vector2& p2) {
return Vector2(p1.x * p2.x, p1.y * p2.y); return Vector2(p1.x * p2.x, p1.y * p2.y);
} }
Vector2 Vector2::operator *(float f) const { Vector2 Vector2::operator*(float f) const {
return Vector2(this->x * f, this->y * f); return Vector2(this->x * f, this->y * f);
} }
@ -103,7 +100,7 @@ float Vector2::Distance(const Vector2& p1, const Vector2& p2) {
} }
float Vector2::Angle(Vector2 from, Vector2 to) { float Vector2::Angle(Vector2 from, Vector2 to) {
return (float) fabs(SignedAngle(from, to)); return (float)fabs(SignedAngle(from, to));
} }
float Vector2::SignedAngle(Vector2 from, Vector2 to) { float Vector2::SignedAngle(Vector2 from, Vector2 to) {
@ -117,7 +114,18 @@ float Vector2::SignedAngle(Vector2 from, Vector2 to) {
float angleFrom = atan2(from.y, from.x); float angleFrom = atan2(from.y, from.x);
float angleTo = atan2(to.y, to.x); float angleTo = atan2(to.y, to.x);
return (angleTo - angleFrom) * Rad2Deg; return (angleTo - angleFrom) * Angle::Rad2Deg;
}
Vector2 Rotate(Vector2 v, float angle) {
float sin = (float)sinf(angle * Angle::Deg2Rad);
float cos = (float)cosf(angle * Angle::Deg2Rad);
float tx = v.x;
float ty = v.y;
v.x = (cos * tx) - (sin * ty);
v.y = (sin * tx) + (cos * ty);
return v;
} }
Vector2 Vector2::Lerp(Vector2 from, Vector2 to, float f) { Vector2 Vector2::Lerp(Vector2 from, Vector2 to, float f) {
@ -128,3 +136,4 @@ Vector2 Vector2::Lerp(Vector2 from, Vector2 to, float f) {
float Vector2::ToFactor(Vector2 a, Vector2 b) { float Vector2::ToFactor(Vector2 a, Vector2 b) {
return (1 - Vector2::Dot(a, b)) / 2; return (1 - Vector2::Dot(a, b)) / 2;
} }

View File

@ -420,7 +420,10 @@ TEST(Vector2, SignedAngle) {
TEST(Vector2, DISABLED_Lerp) { TEST(Vector2, DISABLED_Lerp) {
} }
TEST(Vector2, DIABLED_ToFactor) { TEST(Vector2, DISABLED_ToFactor) {
}
TEST(Vector2, DISABLED_Rotate) {
} }
#endif #endif