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,11 +6,14 @@
#define ANGLE_H
class Angle {
public:
static float Normalize(float angle);
static float Clamp(float angle, float min, float max);
static float Difference(float a, float b);
static float MoveTowards(float fromAngle, float toAngle, float maxAngle);
public:
const static float Rad2Deg;
const static float Deg2Rad;
static float Normalize(float angle);
static float Clamp(float angle, float min, float max);
static float Difference(float a, float b);
static float MoveTowards(float fromAngle, float toAngle, float maxAngle);
};
#endif

View File

@ -6,22 +6,22 @@
#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>
/// The right axis of the vector
/// </summary>
float x;
/// <summary>
/// The upward/forward axis of the vector
/// </summary>
float y;
/// <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>
float x;
/// <summary>
/// The upward/forward axis of the vector
/// </summary>
float y;
} Vec2;
} Vec2;
}
/// <summary>
@ -29,195 +29,203 @@ extern "C" {
/// </summary>
/// This uses the right-handed coordinate system.
struct Vector2 : Vec2 {
public:
/// <summary>
/// Create a new 2-dimensinal zero vector
/// </summary>
Vector2();
/// <summary>
/// Create a new 2-dimensional vector
/// </summary>
/// <param name="x">x axis value</param>
/// <param name="y">y axis value</param>
Vector2(float x, float y);
/// <summary>
/// Create a vector from C-style Vec2
/// </summary>
/// <param name="v">The C-style Vec</param>
Vector2(Vec2 v);
public:
/// <summary>
/// Create a new 2-dimensinal zero vector
/// </summary>
Vector2();
/// <summary>
/// Create a new 2-dimensional vector
/// </summary>
/// <param name="x">x axis value</param>
/// <param name="y">y axis value</param>
Vector2(float x, float y);
/// <summary>
/// Create a vector from C-style Vec2
/// </summary>
/// <param name="v">The C-style Vec</param>
Vector2(Vec2 v);
~Vector2();
~Vector2();
/// <summary>
/// A vector with zero for all axis
/// </summary>
const static Vector2 zero;
/// <summary>
/// A vector with values (1, 0)
/// </summary>
const static Vector2 right;
/// <summary>
/// A vector3 with values (-1, 0)
/// </summary>
const static Vector2 left;
/// <summary>
/// A vector with values (0, 1)
/// </summary>
const static Vector2 up;
/// <summary>
/// A vector with values (0, -1)
/// </summary>
const static Vector2 down;
/// <summary>
/// A vector with values (0, 1)
/// </summary>
const static Vector2 forward;
/// <summary>
/// A vector with values (0, -1)
/// </summary>
const static Vector2 back;
/// <summary>
/// A vector with zero for all axis
/// </summary>
const static Vector2 zero;
/// <summary>
/// A vector with values (1, 0)
/// </summary>
const static Vector2 right;
/// <summary>
/// A vector3 with values (-1, 0)
/// </summary>
const static Vector2 left;
/// <summary>
/// A vector with values (0, 1)
/// </summary>
const static Vector2 up;
/// <summary>
/// A vector with values (0, -1)
/// </summary>
const static Vector2 down;
/// <summary>
/// A vector with values (0, 1)
/// </summary>
const static Vector2 forward;
/// <summary>
/// A vector with values (0, -1)
/// </summary>
const static Vector2 back;
/// <summary>
/// The length of a vector
/// </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);
/// <summary>
/// The length of this vector
/// </summary>
/// <returns>The length of this vector</returns>
float magnitude() const;
/// <summary>
/// The squared length of a vector
/// </summary>
/// <param name="vector">The vector for which you need the squared 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);
/// <summary>
/// The squared length of this vector
/// </summary>
/// <returns>The squared 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.
float sqrMagnitude() const;
/// <summary>
/// Connvert a vector to a length of 1
/// </summary>
/// <param name="vector">The vector to convert</param>
/// <returns>The vector with length 1</returns>
static Vector2 Normalize(Vector2 vector);
/// <summary>
/// Convert the vector to a length of a
/// </summary>
/// <returns>The vector with length 1</returns>
Vector2 normalized() const;
/// <summary>
/// The length of a vector
/// </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);
/// <summary>
/// The length of this vector
/// </summary>
/// <returns>The length of this vector</returns>
float magnitude() const;
/// <summary>
/// The squared length of a vector
/// </summary>
/// <param name="vector">The vector for which you need the squared
/// 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);
/// <summary>
/// The squared length of this vector
/// </summary>
/// <returns>The squared 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.
float sqrMagnitude() const;
/// <summary>
/// Connvert a vector to a length of 1
/// </summary>
/// <param name="vector">The vector to convert</param>
/// <returns>The vector with length 1</returns>
static Vector2 Normalize(Vector2 vector);
/// <summary>
/// Convert the vector to a length of a
/// </summary>
/// <returns>The vector with length 1</returns>
Vector2 normalized() const;
/// <summary>
/// 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>
/// 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>
/// 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;
/// <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;
/// <summary>
/// Scale a vector using another vector
/// </summary>
/// <param name="vector1">The vector to scale</param>
/// <param name="vector2">A vector with scaling factors</param>
/// <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);
/// <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>
/// Scale a vector using another vector
/// </summary>
/// <param name="vector1">The vector to scale</param>
/// <param name="vector2">A vector with scaling factors</param>
/// <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);
/// <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>
/// The dot product of two vectors
/// </summary>
/// <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);
/// <summary>
/// The dot product of two vectors
/// </summary>
/// <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);
/// <summary>
/// Check is this vector is equal to the given vector
/// </summary>
/// <param name="vector">The vector to check against</param>
/// <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);
/// <summary>
/// Check is this vector is equal to the given vector
/// </summary>
/// <param name="vector">The vector to check against</param>
/// <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);
/// <summary>
/// The distance between two vectors
/// </summary>
/// <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);
/// <summary>
/// The distance between two vectors
/// </summary>
/// <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);
/// <summary>
/// Calculate the angle between two vectors
/// </summary>
/// <param name="vector1">The first vector</param>
/// <param name="vector2">The second vector</param>
/// <returns></returns>
/// 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);
/// <summary>
/// Calculate the angle between two vectors
/// </summary>
/// <param name="vector1">The first vector</param>
/// <param name="vector2">The second vector</param>
/// <returns>The angle</returns>
/// 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);
/// <summary>
/// Calculate the angle between two vectors rotation around an axis.
/// </summary>
/// <param name="from">The starting vector</param>
/// <param name="to">The ending vector</param>
/// <param name="axis">The axis to rotate around</param>
/// <returns>The signed angle</returns>
static float SignedAngle(Vector2 from, Vector2 to);
/// <summary>
/// Calculate the angle between two vectors rotation around an axis.
/// </summary>
/// <param name="from">The starting vector</param>
/// <param name="to">The ending vector</param>
/// <param name="axis">The axis to rotate around</param>
/// <returns>The signed angle</returns>
static float SignedAngle(Vector2 from, Vector2 to);
/// <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);
/// <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);
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

View File

@ -2,33 +2,38 @@
// 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/.
#include <math.h>
#include "Angle.h"
#include <math.h>
#include "FloatSingle.h"
float Angle::Normalize(float angle) {
if (!isfinite(angle))
return angle;
const float Angle::Rad2Deg = 57.29578F;
const float Angle::Deg2Rad = 0.0174532924F;
while (angle <= -180) angle += 360;
while (angle > 180) angle -= 360;
return angle;
float Angle::Normalize(float angle) {
if (!isfinite(angle))
return angle;
while (angle <= -180)
angle += 360;
while (angle > 180)
angle -= 360;
return angle;
}
float Angle::Clamp(float angle, float min, float max) {
float normalizedAngle = Normalize(angle);
float r = Float::Clamp(normalizedAngle, min, max);
return r;
float normalizedAngle = Normalize(angle);
float r = Float::Clamp(normalizedAngle, min, max);
return r;
}
float Angle::Difference(float a, float b) {
float r = Normalize(b - a);
return r;
float r = Normalize(b - a);
return r;
}
float Angle::MoveTowards(float fromAngle, float toAngle, float maxAngle) {
float d = toAngle - fromAngle;
float sign = signbit(d) ? -1 : 1;
d = sign * Float::Clamp(fabs(d), 0, maxAngle);
return d;
float d = toAngle - fromAngle;
float sign = signbit(d) ? -1 : 1;
d = sign * Float::Clamp(fabs(d), 0, maxAngle);
return fromAngle + d;
}

View File

@ -2,30 +2,27 @@
// 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/.
#include <math.h>
#include "Vector2.h"
const float Deg2Rad = 0.0174532924F;
const float Rad2Deg = 57.29578F;
const float epsilon = 1E-05f;
#include <math.h>
#include "Angle.h"
#include "FloatSingle.h"
Vector2::Vector2() {
x = 0;
y = 0;
x = 0;
y = 0;
}
Vector2::Vector2(float _x, float _y) {
x = _x;
y = _y;
x = _x;
y = _y;
}
Vector2::Vector2(Vec2 v) {
x = v.x;
y = v.y;
x = v.x;
y = v.y;
}
Vector2::~Vector2() {
}
Vector2::~Vector2() {}
const Vector2 Vector2::zero = Vector2(0, 0);
const Vector2 Vector2::right = Vector2(1, 0);
@ -36,95 +33,107 @@ const Vector2 Vector2::forward = Vector2(0, 1);
const Vector2 Vector2::back = Vector2(0, -1);
float Vector2::Magnitude(const Vector2& a) {
return sqrtf(a.x * a.x + a.y * a.y);
return sqrtf(a.x * a.x + a.y * a.y);
}
float Vector2::magnitude() const {
return (float)sqrtf(x * x + y * y);
return (float)sqrtf(x * x + y * y);
}
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 {
return(x * x + y * y);
return (x * x + y * y);
}
Vector2 Vector2::Normalize(Vector2 v) {
float num = Vector2::Magnitude(v);
Vector2 result = Vector2::zero;
if (num > epsilon) {
result = v / num;
}
return result;
float num = Vector2::Magnitude(v);
Vector2 result = Vector2::zero;
if (num > Float::epsilon) {
result = v / num;
}
return result;
}
Vector2 Vector2::normalized() const {
float num = this->magnitude();
Vector2 result = Vector2::zero;
if (num > epsilon) {
result = ((Vector2)*this) / num;
}
return result;
float num = this->magnitude();
Vector2 result = Vector2::zero;
if (num > Float::epsilon) {
result = ((Vector2) * this) / num;
}
return result;
}
Vector2 Vector2::operator -(const Vector2& v2) const {
return Vector2(this->x - v2.x, this->y - v2.y);
Vector2 Vector2::operator-(const Vector2& v2) const {
return Vector2(this->x - v2.x, this->y - v2.y);
}
Vector2 Vector2::operator -() {
return Vector2(-this->x, -this->y);
Vector2 Vector2::operator-() {
return Vector2(-this->x, -this->y);
}
Vector2 Vector2::operator +(const Vector2& v2) const {
return Vector2(this->x + v2.x, this->y + v2.y);
Vector2 Vector2::operator+(const Vector2& v2) const {
return Vector2(this->x + v2.x, this->y + v2.y);
}
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 {
return Vector2(this->x * f, this->y * f);
Vector2 Vector2::operator*(float f) const {
return Vector2(this->x * f, this->y * f);
}
Vector2 Vector2::operator/(const float& d) {
return Vector2(this->x / d, this->y / d);
return Vector2(this->x / d, this->y / d);
}
float Vector2::Dot(const Vector2& v1, const Vector2& v2) {
return v1.x * v2.x + v1.y * v2.y;
return v1.x * v2.x + v1.y * v2.y;
}
bool Vector2::operator==(const Vector2& v) {
return (this->x == v.x && this->y == v.y);
return (this->x == v.x && this->y == v.y);
}
float Vector2::Distance(const Vector2& p1, const Vector2& p2) {
return Magnitude(p1 - p2);
return Magnitude(p1 - p2);
}
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 sqrMagFrom = from.sqrMagnitude();
float sqrMagTo = to.sqrMagnitude();
float sqrMagFrom = from.sqrMagnitude();
float sqrMagTo = to.sqrMagnitude();
if (sqrMagFrom == 0 || sqrMagTo == 0)
return 0;
if (!isfinite(sqrMagFrom) || !isfinite(sqrMagTo))
return nanf("");
if (sqrMagFrom == 0 || sqrMagTo == 0)
return 0;
if (!isfinite(sqrMagFrom) || !isfinite(sqrMagTo))
return nanf("");
float angleFrom = atan2(from.y, from.x);
float angleTo = atan2(to.y, to.x);
return (angleTo - angleFrom) * Rad2Deg;
float angleFrom = atan2(from.y, from.x);
float angleTo = atan2(to.y, to.x);
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 v = from + (to - from) * f;
return v;
Vector2 v = from + (to - from) * f;
return v;
}
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, DIABLED_ToFactor) {
TEST(Vector2, DISABLED_ToFactor) {
}
TEST(Vector2, DISABLED_Rotate) {
}
#endif