2024-05-13 19:01:21 +02:00

130 lines
4.5 KiB
C++

// This Source Code Form is subject to the terms of the Mozilla Public
// 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/.
#ifndef POLAR_H
#define POLAR_H
#include "Angle.h"
namespace Passer {
struct Vector2;
struct Spherical;
/// @brief A polar vector
/// @details This will use the polar coordinate system consisting of a angle
/// from a reference direction and a distance.
struct Polar {
public:
/// @brief The distance in meters
/// @remark The distance shall never be negative
float distance;
/// @brief The angle in degrees clockwise rotation
/// @remark The angle shall be between -180 .. 180
Angle angle;
/// @brief A new vector with polar coordinates with zero degrees and distance
Polar();
/// @brief A new vector with polar coordinates
/// @param distance The distance in meters
/// @param angle The angle in degrees, clockwise rotation
/// @note The distance is automatically converted to a positive value.
/// @note The angle is automatically normalized to -180 .. 180
Polar(float distance, Angle angle);
/// @brief Convert a vector from 2D carthesian coordinates to polar
/// coordinates
/// @param v The vector to convert
Polar(Vector2 v);
/// @brief Convert a vector from spherical coordinates to polar coordinates
/// @param s The vector to convert
/// @note The resulting vector will be projected on the horizontal plane
Polar(Spherical s);
/// @brief A polar vector with zero degrees and distance
const static Polar zero;
/// @brief A normalized forward-oriented vector
const static Polar forward;
/// @brief A normalized back-oriented vector
const static Polar back;
/// @brief A normalized right-oriented vector
const static Polar right;
/// @brief A normalized left-oriented vector
const static Polar left;
/// @brief Equality test to another vector
/// @param v The vector to check against
/// @return true: if it is identical to the given vector
/// @note This uses float comparison to check equality which may have strange
/// effects. Equality on floats should be avoided.
bool operator==(const Polar &v) const;
/// @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.
Polar operator-() const;
/// @brief Subtract a polar vector from this vector
/// @param v The vector to subtract
/// @return The result of the subtraction
Polar operator-(const Polar &v) const;
Polar operator-=(const Polar &v);
/// @brief Add a polar vector to this vector
/// @param v The vector to add
/// @return The result of the addition
Polar operator+(const Polar &v) const;
Polar operator+=(const Polar &v);
/// @brief Scale the vector uniformly up
/// @param f The scaling factor
/// @return The scaled vector
/// @remark This operation will scale the distance of the vector. The angle
/// will be unaffected.
friend Polar operator*(const Polar &v, float f);
friend Polar operator*(float f, const Polar &v);
Polar operator*=(float f);
/// @brief Scale the vector uniformly down
/// @param f The scaling factor
/// @return The scaled factor
/// @remark This operation will scale the distance of the vector. The angle
/// will be unaffected.
friend Polar operator/(const Polar &v, float f);
friend Polar operator/(float f, const Polar &v);
Polar operator/=(float f);
/// @brief The distance between two vectors
/// @param v1 The first vector
/// @param v2 The second vector
/// @return The distance between the two vectors
static float Distance(const Polar &v1, const Polar &v2);
/// @brief Rotate a vector
/// @param v The vector to rotate
/// @param a The angle in degreesto rotate
/// @return The rotated vector
static Polar Rotate(const Polar &v, Angle a);
};
} // namespace Passer
using namespace Passer;
#include "Spherical.h"
#include "Vector2.h"
#endif