Add convertsion from Vector2

This commit is contained in:
Pascal Serrarens 2024-04-17 15:19:49 +02:00
parent c01640429c
commit d01c5d9de5
2 changed files with 16 additions and 5 deletions

View File

@ -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) {

14
Polar.h
View File

@ -5,13 +5,15 @@
#ifndef POLAR_H
#define POLAR_H
class Vector2;
/// <summary>
/// A polar vector
/// </summary>
/// This will use the polar coordinate system consisting of a angle from a
/// reference direction and a distance.
struct Polar {
public:
public:
/// <summary>
/// The angle in degrees, clockwise rotation
/// </summary>
@ -34,6 +36,8 @@ struct Polar {
/// <param name="distance">The distance in meters</param>
Polar(float angle, float distance);
Polar(Vector2 v);
/// <summary>
/// A polar vector with zero degrees and distance
/// </summary>
@ -51,14 +55,14 @@ struct Polar {
/// </summary>
/// <param name="v">The vector to subtract from this vector</param>
/// <returns>The result of the subtraction</returns>
Polar operator-(const Polar& v) const;
Polar operator-(const Polar &v) const;
/// <summary>
/// Add another polar vector to this polar vector
/// </summary>
/// <param name="v">The vector to add</param>
/// <returns>The result of adding the vector</returns>
Polar operator+(const Polar& v) const;
Polar operator+(const Polar &v) const;
/// <summary>
/// Scale the vector uniformly up
@ -76,7 +80,7 @@ struct Polar {
/// <returns>The scaled vector</returns>
/// This operation will scale the distance of the vector. The angle will be
/// unaffected.
Polar operator/(const float& factor);
Polar operator/(const float &factor);
/// <summary>
/// The distance between two vectors
@ -84,7 +88,7 @@ struct Polar {
/// <param name="v1">The first vector</param>
/// <param name="v2">The second vector</param>
/// <returns>The distance between the two vectors</returns>
static float Distance(const Polar& v1, const Polar& v2);
static float Distance(const Polar &v1, const Polar &v2);
/// <summary>
/// Rotate the vector