| Line |
Branch |
Exec |
Source |
| 1 |
|
|
#pragma once |
| 2 |
|
|
|
| 3 |
|
|
#include "base/angle.h" |
| 4 |
|
|
|
| 5 |
|
|
|
| 6 |
|
|
namespace eu |
| 7 |
|
|
{ |
| 8 |
|
|
/** \addtogroup math |
| 9 |
|
|
* @{ |
| 10 |
|
|
*/ |
| 11 |
|
|
|
| 12 |
|
|
struct v2; |
| 13 |
|
|
struct n2; |
| 14 |
|
|
|
| 15 |
|
|
/// a 2d vector |
| 16 |
|
|
struct v2 |
| 17 |
|
|
{ |
| 18 |
|
|
float x; |
| 19 |
|
|
float y; |
| 20 |
|
|
|
| 21 |
|
|
v2() = default; |
| 22 |
|
28 |
constexpr v2(float ax, float ay) |
| 23 |
|
28 |
: x(ax) |
| 24 |
|
28 |
, y(ay) |
| 25 |
|
|
{ |
| 26 |
|
28 |
} |
| 27 |
|
|
explicit v2(const std::tuple<float, float>& a); |
| 28 |
|
|
explicit v2(const n2& u); |
| 29 |
|
|
|
| 30 |
|
|
[[nodiscard]] static v2 from_to(const v2& from, const v2& to); |
| 31 |
|
|
[[nodiscard]] static v2 from(const n2& n); |
| 32 |
|
|
|
| 33 |
|
|
/** Changes the length to 1. |
| 34 |
|
|
* If the calculated length is zero, the vector is changed to a known value and false is returned |
| 35 |
|
|
*/ |
| 36 |
|
|
bool normalize(); |
| 37 |
|
|
|
| 38 |
|
|
/// Assumes the vector is a point and returns it if it was rotated around `(0,0)` |
| 39 |
|
|
[[nodiscard]] v2 get_rotated(const An& a) const; |
| 40 |
|
|
|
| 41 |
|
|
/// Returns an array to the data. |
| 42 |
|
|
/// non const so it's useful for letting an API tweak the members. |
| 43 |
|
|
float* get_data_ptr(); |
| 44 |
|
|
|
| 45 |
|
|
/// Returns an array to the data |
| 46 |
|
|
[[nodiscard]] const float* get_data_ptr() const; |
| 47 |
|
|
|
| 48 |
|
|
/// Returns a new vector where the sign of the y component is flipped. |
| 49 |
|
|
[[nodiscard]] v2 get_flipped_y() const; |
| 50 |
|
|
|
| 51 |
|
|
/// Returns the squared length of the vector. |
| 52 |
|
|
/// This is useful if you want to compare to zero, sort by length or similar, otherwise see @ref get_length() |
| 53 |
|
|
[[nodiscard]] float get_length_squared() const; |
| 54 |
|
|
|
| 55 |
|
|
/// Returns the length of the vector |
| 56 |
|
|
[[nodiscard]] float get_length() const; |
| 57 |
|
|
|
| 58 |
|
|
/** Changes the length to 1. |
| 59 |
|
|
* If the calculated length is zero, the vector is changed to a known value and false is returned |
| 60 |
|
|
*/ |
| 61 |
|
|
[[nodiscard]] std::optional<n2> get_normalized() const; |
| 62 |
|
|
|
| 63 |
|
|
void operator+=(const v2& rhs); |
| 64 |
|
|
void operator-=(const v2& rhs); |
| 65 |
|
|
v2 operator-() const; |
| 66 |
|
|
void operator/=(float rhs); |
| 67 |
|
|
void operator*=(float rhs); |
| 68 |
|
|
}; |
| 69 |
|
|
|
| 70 |
|
|
constexpr v2 zero2f = v2{ 0, 0 }; |
| 71 |
|
|
|
| 72 |
|
|
/// a 2d unit (vector) |
| 73 |
|
|
struct n2 |
| 74 |
|
|
{ |
| 75 |
|
|
float x; |
| 76 |
|
|
float y; |
| 77 |
|
|
|
| 78 |
|
|
/// asserts that the length is 1 |
| 79 |
|
|
explicit n2(float ax, float ay); |
| 80 |
|
|
|
| 81 |
|
|
/// asserts that the length is 1 |
| 82 |
|
|
explicit n2(const v2& v); |
| 83 |
|
|
|
| 84 |
|
|
/// Assumes the unit vector is a point and returns it if it was rotated around `(0,0)` |
| 85 |
|
|
[[nodiscard]] n2 get_rotated(const An& a) const; |
| 86 |
|
|
|
| 87 |
|
|
/// Returns an array to the data |
| 88 |
|
|
[[nodiscard]] const float* get_data_ptr() const; |
| 89 |
|
|
|
| 90 |
|
|
/// Returns a new unit vector where the sign of the y component is flipped. |
| 91 |
|
|
[[nodiscard]] n2 get_flipped_y() const; |
| 92 |
|
|
|
| 93 |
|
|
/// returns false if the length isn't 1 |
| 94 |
|
|
[[nodiscard]] bool is_valid() const; |
| 95 |
|
|
|
| 96 |
|
|
n2 operator-() const; |
| 97 |
|
|
}; |
| 98 |
|
|
|
| 99 |
|
|
|
| 100 |
|
|
v2 operator+(const v2& lhs, const v2& rhs); |
| 101 |
|
|
v2 operator-(const v2& lhs, const v2& rhs); |
| 102 |
|
|
v2 operator*(const v2& lhs, float rhs); |
| 103 |
|
|
v2 operator*(float lhs, const v2& rhs); |
| 104 |
|
|
v2 operator*(const n2& lhs, float rhs); |
| 105 |
|
|
v2 operator*(float lhs, const n2& rhs); |
| 106 |
|
|
v2 operator/(const v2& lhs, float rhs); |
| 107 |
|
|
|
| 108 |
|
|
|
| 109 |
|
|
float dot(const v2& lhs, const v2& rhs); |
| 110 |
|
|
|
| 111 |
|
|
v2 lerp_vec2f(const v2& from, float v, const v2& to); |
| 112 |
|
|
|
| 113 |
|
|
/// converts a 2d vector to a string, prefer fmt |
| 114 |
|
|
std::string string_from(const v2& v); |
| 115 |
|
|
|
| 116 |
|
|
/// converts a 2d unit vector to a string, prefer fmt |
| 117 |
|
|
std::string string_from(const n2& v); |
| 118 |
|
|
|
| 119 |
|
|
/** @}*/ |
| 120 |
|
|
|
| 121 |
|
|
ADD_CATCH_FORMATTER_DEF(v2) |
| 122 |
|
|
ADD_CATCH_FORMATTER_DEF(n2) |
| 123 |
|
|
} |
| 124 |
|
|
|
| 125 |
1/2
✓ Branch 0 (2 → 3) taken 1 times.
✗ Branch 1 (2 → 17) not taken.
|
2 |
ADD_DEFAULT_FORMATTER(eu::v2, std::string, eu::string_from); |
| 126 |
1/2
✓ Branch 0 (2 → 3) taken 1 times.
✗ Branch 1 (2 → 17) not taken.
|
2 |
ADD_DEFAULT_FORMATTER(eu::n2, std::string, eu::string_from); |
| 127 |
|
|
|