Use Angle type for Polar

This commit is contained in:
Pascal Serrarens 2024-05-10 18:18:33 +02:00
parent 8a2ce21abc
commit 9bea94f21c
2 changed files with 22 additions and 21 deletions

View File

@ -35,20 +35,20 @@ Polar::Polar(Spherical s) {
const Polar Polar::zero = Polar(0, 0); const Polar Polar::zero = Polar(0, 0);
float Polar::Distance(const Polar &v1, const Polar &v2) { float Polar::Distance(Polar &v1, Polar &v2) {
float d = float d = Angle::CosineRuleSide(v1.distance, v2.distance,
Angle::CosineRuleSide(v1.distance, v2.distance, v2.angle - v1.angle); (float)v2.angle - (float)v1.angle);
return d; return d;
} }
Polar Polar::operator+(const Polar &v2) const { Polar Polar::operator+(Polar &v2) {
if (v2.distance == 0) if (v2.distance == 0)
return Polar(this->distance, return Polar(this->distance,
this->angle); // Polar(this->angle, this->distance); this->angle); // Polar(this->angle, this->distance);
if (this->distance == 0) if (this->distance == 0)
return v2; return v2;
float deltaAngle = Angle::Normalize(v2.angle - this->angle); float deltaAngle = Angle::Normalize(v2.angle - (float)this->angle);
float rotation = deltaAngle < 0 ? 180 + deltaAngle : 180 - deltaAngle; float rotation = deltaAngle < 0 ? 180 + deltaAngle : 180 - deltaAngle;
if (rotation == 180 && v2.distance > 0) { if (rotation == 180 && v2.distance > 0) {
@ -64,24 +64,25 @@ Polar Polar::operator+(const Polar &v2) const {
float angle = float angle =
Angle::CosineRuleAngle(newDistance, this->distance, v2.distance); Angle::CosineRuleAngle(newDistance, this->distance, v2.distance);
float newAngle = deltaAngle < 0 ? Angle::Normalize(this->angle - angle) float newAngle =
: Angle::Normalize(this->angle + angle); deltaAngle < 0 ? (float)this->angle - angle : (float)this->angle + angle;
newAngle = Angle::Normalize(newAngle);
Polar vector = Polar(newDistance, newAngle); // Polar(newAngle, newDistance); Polar vector = Polar(newDistance, newAngle); // Polar(newAngle, newDistance);
return vector; return vector;
} }
Polar Polar::operator-() { Polar Polar::operator-() {
Polar vector = Polar vector = Polar(this->distance,
Polar(this->distance, (float)this->angle -
this->angle - 180); // Polar(this->angle - 180, this->distance); 180); // Polar(this->angle - 180, this->distance);
return vector; return vector;
} }
Polar Polar::operator-(const Polar &v2) const { Polar Polar::operator-(Polar &v2) {
Polar vector = // Polar vector = *this + Polar(v2.distance, (float)v2.angle - 180);
*this + Polar(v2.distance, //(Polar(v2.angle - 180, v2.distance));
v2.angle - 180); //(Polar(v2.angle - 180, v2.distance)); Polar vector = -v2;
return vector; return *this + v2;
} }
Polar Polar::operator*(float f) const { Polar Polar::operator*(float f) const {
@ -94,7 +95,7 @@ Polar Polar::operator/(const float &f) {
this->angle); // Polar(this->angle, this->distance / f); this->angle); // Polar(this->angle, this->distance / f);
} }
Polar Polar::Rotate(Polar v, float angle) { Polar Polar::Rotate(Polar v, Angle angle) {
v.angle = Angle::Normalize(v.angle + angle); v.angle = Angle::Normalize(v.angle + angle);
return v; return v;
} }

10
Polar.h
View File

@ -23,7 +23,7 @@ public:
/// The angle in degrees, clockwise rotation /// The angle in degrees, clockwise rotation
/// </summary> /// </summary>
/// The angle is normalized to -180 .. 180 /// The angle is normalized to -180 .. 180
float angle; Angle angle;
/// <summary> /// <summary>
/// The distance in meters /// The distance in meters
/// </summary> /// </summary>
@ -70,14 +70,14 @@ public:
/// </summary> /// </summary>
/// <param name="v">The vector to subtract from this vector</param> /// <param name="v">The vector to subtract from this vector</param>
/// <returns>The result of the subtraction</returns> /// <returns>The result of the subtraction</returns>
Polar operator-(const Polar &v) const; Polar operator-(Polar &v);
/// <summary> /// <summary>
/// Add another polar vector to this polar vector /// Add another polar vector to this polar vector
/// </summary> /// </summary>
/// <param name="v">The vector to add</param> /// <param name="v">The vector to add</param>
/// <returns>The result of adding the vector</returns> /// <returns>The result of adding the vector</returns>
Polar operator+(const Polar &v) const; Polar operator+(Polar &v);
/// <summary> /// <summary>
/// Scale the vector uniformly up /// Scale the vector uniformly up
@ -103,7 +103,7 @@ public:
/// <param name="v1">The first vector</param> /// <param name="v1">The first vector</param>
/// <param name="v2">The second vector</param> /// <param name="v2">The second vector</param>
/// <returns>The distance between the two vectors</returns> /// <returns>The distance between the two vectors</returns>
static float Distance(const Polar &v1, const Polar &v2); static float Distance(Polar &v1, Polar &v2);
/// <summary> /// <summary>
/// Rotate the vector /// Rotate the vector
@ -111,7 +111,7 @@ public:
/// <param name="v">The vector to rotate</param> /// <param name="v">The vector to rotate</param>
/// <param name="angle">Angle in radias to rotate</param> /// <param name="angle">Angle in radias to rotate</param>
/// <returns>The rotated vector</returns> /// <returns>The rotated vector</returns>
static Polar Rotate(Polar v, float angle); static Polar Rotate(Polar v, Angle angle);
}; };
} // namespace Passer } // namespace Passer
#include "Spherical.h" #include "Spherical.h"