This commit is contained in:
Pascal Serrarens 2024-05-12 21:56:49 +02:00
parent 66e47d736f
commit f7d9d976fc
4 changed files with 42 additions and 14 deletions

View File

@ -40,6 +40,15 @@ bool Polar::operator==(const Polar &v) {
return (this->distance == v.distance && this->angle == v.angle);
}
Polar Polar::Normalize(const Polar &v) {
Polar r = Polar(1, v.angle);
return r;
}
Polar Polar::normalized() const {
Polar r = Polar(1, this->angle);
return r;
}
Polar Polar::operator+(Polar &v2) {
if (v2.distance == 0)
return Polar(this->distance, this->angle);
@ -96,7 +105,8 @@ float Polar::Distance(const Polar &v1, const Polar &v2) {
return d;
}
Polar Polar::Rotate(Polar v, Angle angle) {
v.angle = Angle::Normalize(v.angle + angle);
return v;
Polar Polar::Rotate(const Polar &v, Angle angle) {
Angle a = Angle::Normalize(v.angle + angle);
Polar r = Polar(v.distance, a);
return r;
}

18
Polar.h
View File

@ -52,6 +52,22 @@ public:
/// effects. Equality on floats should be avoided.
bool operator==(const Polar &v);
/// @brief The vector length
/// @param v The vector for which you need the length
/// @return The vector length;
inline static float Magnitude(const Polar &v) { return v.distance; }
/// @brief The vector length
/// @return The vector length
inline float magnitude() const { return this->distance; }
/// @brief Convert the vector to a length of 1
/// @param v The vector to convert
/// @return The vector normalized to a length of 1
static Polar Normalize(const Polar &v);
/// @brief Convert the vector to a length of a
/// @return The vector normalized to a length of 1
Polar normalized() const;
/// @brief Negate the vector
/// @return The negated vector
/// This will rotate the vector by 180 degrees. Distance will stay the same.
@ -87,7 +103,7 @@ public:
/// @param v The vector to rotate
/// @param a The angle in degreesto rotate
/// @return The rotated vector
static Polar Rotate(Polar v, Angle a);
static Polar Rotate(const Polar &v, Angle a);
};
} // namespace Passer

View File

@ -60,7 +60,7 @@ float Vector2::magnitude() const { return (float)sqrtf(x * x + y * y); }
float Vector2::SqrMagnitude(const Vector2 &a) { return a.x * a.x + a.y * a.y; }
float Vector2::sqrMagnitude() const { return (x * x + y * y); }
Vector2 Vector2::Normalize(Vector2 v) {
Vector2 Vector2::Normalize(const Vector2 &v) {
float num = Vector2::Magnitude(v);
Vector2 result = Vector2::zero;
if (num > Float::epsilon) {
@ -89,10 +89,10 @@ Vector2 Vector2::operator+(const Vector2 &v2) const {
Vector2 Vector2::Scale(const Vector2 &p1, const Vector2 &p2) {
return Vector2(p1.x * p2.x, p1.y * p2.y);
}
Vector2 Vector2::operator*(const float &f) const {
Vector2 Vector2::operator*(float f) const {
return Vector2(this->x * f, this->y * f);
}
Vector2 Vector2::operator/(const float &f) const {
Vector2 Vector2::operator/(float f) const {
return Vector2(this->x / f, this->y / f);
}
@ -125,7 +125,7 @@ float Vector2::SignedAngle(const Vector2 &v1, const Vector2 &v2) {
return -(angleTo - angleFrom) * Angle::Rad2Deg;
}
Vector2 Vector2::Rotate(const Vector2 &v, const float &a) {
Vector2 Vector2::Rotate(const Vector2 &v, Passer::Angle a) {
float angleRad = a * Angle::Deg2Rad;
#if defined(AVR)
float sinValue = sin(angleRad);
@ -142,7 +142,7 @@ Vector2 Vector2::Rotate(const Vector2 &v, const float &a) {
return r;
}
Vector2 Vector2::Lerp(const Vector2 &v1, const Vector2 &v2, const float f) {
Vector2 Vector2::Lerp(const Vector2 &v1, const Vector2 &v2, float f) {
Vector2 v = v1 + (v2 - v1) * f;
return v;
}

View File

@ -5,6 +5,8 @@
#ifndef VECTOR2_H
#define VECTOR2_H
#include "Angle.h"
extern "C" {
/// <summary>
/// 2-dimensional Vector representation
@ -106,7 +108,7 @@ public:
/// @brief Convert the vector to a length of 1
/// @param v The vector to convert
/// @return The vector normalized to a length of 1
static Vector2 Normalize(Vector2 v);
static Vector2 Normalize(const Vector2 &v);
/// @brief Convert the vector to a length 1
/// @return The vector normalized to a length of 1
Vector2 normalized() const;
@ -136,12 +138,12 @@ public:
/// @return The scaled vector
/// @remark Each component of the vector will be multipled with the same
/// factor f.
Vector2 operator*(const float &f) const;
Vector2 operator*(float f) const;
/// @brief Scale the vector uniformly down
/// @param f The scaling factor
/// @return The scaled vector
/// @remark Each componet of the vector will be divided by the same factor.
Vector2 operator/(const float &f) const;
Vector2 operator/(float f) const;
/// @brief The dot product of two vectors
/// @param v1 The first vector
@ -173,7 +175,7 @@ public:
/// @param v The vector to rotate
/// @param a The angle in degrees to rotate
/// @return The rotated vector
static Vector2 Rotate(const Vector2 &v, const float &a);
static Vector2 Rotate(const Vector2 &v, Passer::Angle a);
/// @brief Lerp (linear interpolation) between two vectors
/// @param v1 The starting vector
@ -183,7 +185,7 @@ public:
/// @remark The factor f is unclamped. Value 0 matches the vector *v1*, Value
/// 1 matches vector *v2*. Value -1 is vector *v1* minus the difference
/// between *v1* and *v2* etc.
static Vector2 Lerp(const Vector2 &v1, const Vector2 &v2, const float f);
static Vector2 Lerp(const Vector2 &v1, const Vector2 &v2, float f);
};
} // namespace Passer