From d01c5d9de5fbb295ef5e7b64d8cd0640c925ae20 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Wed, 17 Apr 2024 15:19:49 +0200 Subject: [PATCH] Add convertsion from Vector2 --- Polar.cpp | 7 +++++++ Polar.h | 14 +++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Polar.cpp b/Polar.cpp index aa28e9a..f72a692 100644 --- a/Polar.cpp +++ b/Polar.cpp @@ -2,6 +2,7 @@ #include "Angle.h" #include "Polar.h" +#include "Vector2.h" Polar::Polar() { angle = 0.0F; @@ -19,6 +20,12 @@ Polar::Polar(float newAngle, float newDistance) { } } +Polar::Polar(Vector2 v) { + float signY = (v.y >= 0) - (v.y < 0); + angle = atan2(v.y, signY * sqrt(v.y * v.y + v.x * v.x)) * Angle::Rad2Deg; + distance = v.magnitude(); +} + const Polar Polar::zero = Polar(0, 0); float Polar::Distance(const Polar &v1, const Polar &v2) { diff --git a/Polar.h b/Polar.h index cbb161f..6060364 100644 --- a/Polar.h +++ b/Polar.h @@ -5,13 +5,15 @@ #ifndef POLAR_H #define POLAR_H +class Vector2; + /// /// A polar vector /// /// This will use the polar coordinate system consisting of a angle from a /// reference direction and a distance. struct Polar { - public: +public: /// /// The angle in degrees, clockwise rotation /// @@ -34,6 +36,8 @@ struct Polar { /// The distance in meters Polar(float angle, float distance); + Polar(Vector2 v); + /// /// A polar vector with zero degrees and distance /// @@ -51,14 +55,14 @@ struct Polar { /// /// The vector to subtract from this vector /// The result of the subtraction - Polar operator-(const Polar& v) const; + Polar operator-(const Polar &v) const; /// /// Add another polar vector to this polar vector /// /// The vector to add /// The result of adding the vector - Polar operator+(const Polar& v) const; + Polar operator+(const Polar &v) const; /// /// Scale the vector uniformly up @@ -76,7 +80,7 @@ struct Polar { /// The scaled vector /// This operation will scale the distance of the vector. The angle will be /// unaffected. - Polar operator/(const float& factor); + Polar operator/(const float &factor); /// /// The distance between two vectors @@ -84,7 +88,7 @@ struct Polar { /// The first vector /// The second vector /// The distance between the two vectors - static float Distance(const Polar& v1, const Polar& v2); + static float Distance(const Polar &v1, const Polar &v2); /// /// Rotate the vector