Added Vector2::Rotate, Moved Rad2Deg/Deg2Rad to Angle class
This commit is contained in:
parent
307a94c567
commit
c1a926582e
@ -6,7 +6,10 @@
|
||||
#define ANGLE_H
|
||||
|
||||
class Angle {
|
||||
public:
|
||||
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);
|
||||
|
@ -6,12 +6,12 @@
|
||||
#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>
|
||||
/// 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>
|
||||
@ -21,7 +21,7 @@ extern "C" {
|
||||
/// </summary>
|
||||
float y;
|
||||
|
||||
} Vec2;
|
||||
} Vec2;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -29,7 +29,7 @@ extern "C" {
|
||||
/// </summary>
|
||||
/// This uses the right-handed coordinate system.
|
||||
struct Vector2 : Vec2 {
|
||||
public:
|
||||
public:
|
||||
/// <summary>
|
||||
/// Create a new 2-dimensinal zero vector
|
||||
/// </summary>
|
||||
@ -91,11 +91,10 @@ public:
|
||||
/// <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.
|
||||
/// <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
|
||||
@ -122,20 +121,20 @@ public:
|
||||
/// </summary>
|
||||
/// <returns>The negated vector</returns>
|
||||
/// This will result in a vector pointing in the opposite direction
|
||||
Vector2 operator -();
|
||||
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;
|
||||
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;
|
||||
Vector2 operator+(const Vector2& vector2) const;
|
||||
|
||||
/// <summary>
|
||||
/// Scale a vector using another vector
|
||||
@ -152,14 +151,14 @@ public:
|
||||
/// <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;
|
||||
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);
|
||||
Vector2 operator/(const float& factor);
|
||||
|
||||
/// <summary>
|
||||
/// The dot product of two vectors
|
||||
@ -176,7 +175,7 @@ public:
|
||||
/// <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);
|
||||
bool operator==(const Vector2& vector);
|
||||
|
||||
/// <summary>
|
||||
/// The distance between two vectors
|
||||
@ -191,7 +190,7 @@ public:
|
||||
/// </summary>
|
||||
/// <param name="vector1">The first 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
|
||||
/// between the two vectors. Use Vector3::SignedAngle if a
|
||||
/// signed angle is needed.
|
||||
@ -206,6 +205,14 @@ public:
|
||||
/// <returns>The signed angle</returns>
|
||||
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>
|
||||
/// Lerp between two vectors
|
||||
/// </summary>
|
||||
@ -213,8 +220,9 @@ public:
|
||||
/// <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.
|
||||
/// 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);
|
||||
|
@ -2,16 +2,21 @@
|
||||
// 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"
|
||||
|
||||
const float Angle::Rad2Deg = 57.29578F;
|
||||
const float Angle::Deg2Rad = 0.0174532924F;
|
||||
|
||||
float Angle::Normalize(float angle) {
|
||||
if (!isfinite(angle))
|
||||
return angle;
|
||||
|
||||
while (angle <= -180) angle += 360;
|
||||
while (angle > 180) angle -= 360;
|
||||
while (angle <= -180)
|
||||
angle += 360;
|
||||
while (angle > 180)
|
||||
angle -= 360;
|
||||
return angle;
|
||||
}
|
||||
|
||||
@ -30,5 +35,5 @@ 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;
|
||||
return fromAngle + d;
|
||||
}
|
||||
|
@ -2,12 +2,10 @@
|
||||
// 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;
|
||||
@ -24,8 +22,7 @@ Vector2::Vector2(Vec2 v) {
|
||||
y = v.y;
|
||||
}
|
||||
|
||||
Vector2::~Vector2() {
|
||||
}
|
||||
Vector2::~Vector2() {}
|
||||
|
||||
const Vector2 Vector2::zero = Vector2(0, 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;
|
||||
}
|
||||
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) {
|
||||
if (num > Float::epsilon) {
|
||||
result = v / num;
|
||||
}
|
||||
return result;
|
||||
@ -60,21 +57,21 @@ Vector2 Vector2::Normalize(Vector2 v) {
|
||||
Vector2 Vector2::normalized() const {
|
||||
float num = this->magnitude();
|
||||
Vector2 result = Vector2::zero;
|
||||
if (num > epsilon) {
|
||||
result = ((Vector2)*this) / num;
|
||||
if (num > Float::epsilon) {
|
||||
result = ((Vector2) * this) / num;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
Vector2 Vector2::operator -() {
|
||||
Vector2 Vector2::operator-() {
|
||||
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);
|
||||
}
|
||||
|
||||
@ -82,7 +79,7 @@ Vector2 Vector2::Scale(const Vector2& p1, const Vector2& p2) {
|
||||
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);
|
||||
}
|
||||
|
||||
@ -103,7 +100,7 @@ float Vector2::Distance(const Vector2& p1, const Vector2& 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) {
|
||||
@ -117,7 +114,18 @@ float Vector2::SignedAngle(Vector2 from, Vector2 to) {
|
||||
|
||||
float angleFrom = atan2(from.y, from.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) {
|
||||
@ -128,3 +136,4 @@ Vector2 Vector2::Lerp(Vector2 from, Vector2 to, float f) {
|
||||
float Vector2::ToFactor(Vector2 a, Vector2 b) {
|
||||
return (1 - Vector2::Dot(a, b)) / 2;
|
||||
}
|
||||
|
||||
|
@ -420,7 +420,10 @@ TEST(Vector2, SignedAngle) {
|
||||
TEST(Vector2, DISABLED_Lerp) {
|
||||
}
|
||||
|
||||
TEST(Vector2, DIABLED_ToFactor) {
|
||||
TEST(Vector2, DISABLED_ToFactor) {
|
||||
}
|
||||
|
||||
TEST(Vector2, DISABLED_Rotate) {
|
||||
}
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user