libs/base/src/base/vec2.h
| 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 | 60 | constexpr v2(float ax, float ay) | |
| 23 | 60 | : x(ax) | |
| 24 | 60 | , y(ay) | |
| 25 | { | ||
| 26 | 60 | } | |
| 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 | void operator*=(const v2& rhs); | ||
| 69 | }; | ||
| 70 | |||
| 71 | constexpr v2 zero2f = v2{ 0, 0 }; | ||
| 72 | |||
| 73 | /// a 2d unit (vector) | ||
| 74 | struct n2 | ||
| 75 | { | ||
| 76 | float x; | ||
| 77 | float y; | ||
| 78 | |||
| 79 | /// asserts that the length is 1 | ||
| 80 | explicit n2(float ax, float ay); | ||
| 81 | |||
| 82 | /// asserts that the length is 1 | ||
| 83 | explicit n2(const v2& v); | ||
| 84 | |||
| 85 | /// Assumes the unit vector is a point and returns it if it was rotated around `(0,0)` | ||
| 86 | [[nodiscard]] n2 get_rotated(const An& a) const; | ||
| 87 | |||
| 88 | /// Returns an array to the data | ||
| 89 | [[nodiscard]] const float* get_data_ptr() const; | ||
| 90 | |||
| 91 | /// Returns a new unit vector where the sign of the y component is flipped. | ||
| 92 | [[nodiscard]] n2 get_flipped_y() const; | ||
| 93 | |||
| 94 | /// returns false if the length isn't 1 | ||
| 95 | [[nodiscard]] bool is_valid() const; | ||
| 96 | |||
| 97 | n2 operator-() const; | ||
| 98 | }; | ||
| 99 | |||
| 100 | |||
| 101 | v2 operator+(const v2& lhs, const v2& rhs); | ||
| 102 | v2 operator-(const v2& lhs, const v2& rhs); | ||
| 103 | v2 operator*(const v2& lhs, float rhs); | ||
| 104 | v2 operator*(const v2& lhs, const v2& rhs); | ||
| 105 | v2 operator*(float lhs, const v2& rhs); | ||
| 106 | v2 operator*(const n2& lhs, float rhs); | ||
| 107 | v2 operator*(float lhs, const n2& rhs); | ||
| 108 | v2 operator/(const v2& lhs, float rhs); | ||
| 109 | |||
| 110 | |||
| 111 | float dot(const v2& lhs, const v2& rhs); | ||
| 112 | |||
| 113 | v2 lerp_vec2f(const v2& from, float v, const v2& to); | ||
| 114 | |||
| 115 | /// converts a 2d vector to a string, prefer fmt | ||
| 116 | std::string string_from(const v2& v); | ||
| 117 | |||
| 118 | /// converts a 2d unit vector to a string, prefer fmt | ||
| 119 | std::string string_from(const n2& v); | ||
| 120 | |||
| 121 | /** @}*/ | ||
| 122 | |||
| 123 | ADD_CATCH_FORMATTER_DEF(v2) | ||
| 124 | ADD_CATCH_FORMATTER_DEF(n2) | ||
| 125 | } | ||
| 126 | |||
| 127 | 2 | ADD_DEFAULT_FORMATTER(eu::v2, std::string, eu::string_from); | |
| 128 | 2 | ADD_DEFAULT_FORMATTER(eu::n2, std::string, eu::string_from); | |
| 129 |