libs/base/src/base/angle.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "base/numeric.h" | ||
| 4 | |||
| 5 | namespace eu | ||
| 6 | { | ||
| 7 | /** \addtogroup math | ||
| 8 | * @{ | ||
| 9 | */ | ||
| 10 | |||
| 11 | /// An angle in both degrees and radians. | ||
| 12 | struct An | ||
| 13 | { | ||
| 14 | [[nodiscard]] constexpr static An | ||
| 15 | 88 | from_degrees(float degrees) | |
| 16 | { | ||
| 17 | 88 | return An(rad_from_deg(degrees)); | |
| 18 | } | ||
| 19 | |||
| 20 | [[nodiscard]] constexpr static An | ||
| 21 | 140 | from_radians(float radians) | |
| 22 | { | ||
| 23 | 140 | return An(radians); | |
| 24 | } | ||
| 25 | |||
| 26 | [[nodiscard]] constexpr static An | ||
| 27 | 5 | from_percent_of_360(float percent) | |
| 28 | { | ||
| 29 | 5 | return An::from_radians(percent * pi * 2.0f); | |
| 30 | } | ||
| 31 | |||
| 32 | [[nodiscard]] constexpr static An | ||
| 33 | from_percent_of_180(float percent) | ||
| 34 | { | ||
| 35 | return An::from_radians(percent * pi); | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | void wrap(); | ||
| 40 | |||
| 41 | |||
| 42 | [[nodiscard]] constexpr float | ||
| 43 | 72 | as_degrees() const | |
| 44 | { | ||
| 45 | 72 | return deg_from_rad(radians); | |
| 46 | } | ||
| 47 | |||
| 48 | [[nodiscard]] constexpr float | ||
| 49 | 289 | as_radians() const | |
| 50 | { | ||
| 51 | 289 | return radians; | |
| 52 | } | ||
| 53 | |||
| 54 | [[nodiscard]] constexpr float | ||
| 55 | from_percent_of_360() const | ||
| 56 | { | ||
| 57 | return as_radians() / (pi * 2.0f); | ||
| 58 | } | ||
| 59 | |||
| 60 | [[nodiscard]] An get_wrapped() const; | ||
| 61 | |||
| 62 | void operator+=(const An& rhs); | ||
| 63 | void operator-=(const An& rhs); | ||
| 64 | void operator*=(float rhs); | ||
| 65 | void operator/=(float rhs); | ||
| 66 | An operator-() const; | ||
| 67 | |||
| 68 | private: | ||
| 69 | float radians; | ||
| 70 | |||
| 71 | 228 | constexpr explicit An(float r) : radians(r) {} | |
| 72 | |||
| 73 | [[nodiscard]] static constexpr float | ||
| 74 | 72 | deg_from_rad(float radians) | |
| 75 | { | ||
| 76 | 72 | return (180.0f / pi) * radians; | |
| 77 | } | ||
| 78 | |||
| 79 | [[nodiscard]] static constexpr float | ||
| 80 | 88 | rad_from_deg(float degrees) | |
| 81 | { | ||
| 82 | 88 | return pi / 180.0f * degrees; | |
| 83 | } | ||
| 84 | }; | ||
| 85 | |||
| 86 | constexpr An one_turn = An::from_radians(pi * 2.0f); | ||
| 87 | constexpr An half_turn = An::from_radians(pi); | ||
| 88 | constexpr An quarter_turn = An::from_radians(pi / 2.0f); | ||
| 89 | constexpr An no_rotation = An::from_radians(0.0f); | ||
| 90 | |||
| 91 | |||
| 92 | float sin(const An& ang); | ||
| 93 | float cos(const An& ang); | ||
| 94 | float tan(const An& ang); | ||
| 95 | An asin(float v); | ||
| 96 | An acos(float v); | ||
| 97 | An atan(float v); | ||
| 98 | An atan2(float y, float x); | ||
| 99 | |||
| 100 | An operator+(const An& lhs, const An& rhs); | ||
| 101 | An operator-(const An& lhs, const An& rhs); | ||
| 102 | An operator*(const An& lhs, float rhs); | ||
| 103 | An operator/(const An& lhs, float rhs); | ||
| 104 | An operator*(float rhs, const An& lhs); | ||
| 105 | |||
| 106 | std::string string_from(const An& a); | ||
| 107 | |||
| 108 | bool operator<(const An& lhs, const An& rhs); | ||
| 109 | bool operator<=(const An& lhs, const An& rhs); | ||
| 110 | bool operator>(const An& lhs, const An& rhs); | ||
| 111 | bool operator>=(const An& lhs, const An& rhs); | ||
| 112 | |||
| 113 | An lerp_angle(const An& from, float v, const An& to); | ||
| 114 | |||
| 115 | /** @}*/ | ||
| 116 | ADD_CATCH_FORMATTER_DEF(An) | ||
| 117 | } | ||
| 118 | |||
| 119 | 24 | ADD_DEFAULT_FORMATTER(eu::An, std::string, eu::string_from); | |
| 120 | |||
| 121 | namespace eu::convert | ||
| 122 | { | ||
| 123 | 45 | constexpr An operator""_deg(unsigned long long d) | |
| 124 | { | ||
| 125 | 45 | return An::from_degrees(static_cast<float>(d)); | |
| 126 | } | ||
| 127 | |||
| 128 | 4 | constexpr An operator""_deg(long double d) | |
| 129 | { | ||
| 130 | 4 | return An::from_degrees(static_cast<float>(d)); | |
| 131 | } | ||
| 132 | |||
| 133 | |||
| 134 | constexpr An operator""_rad(long double r) | ||
| 135 | { | ||
| 136 | return An::from_radians(static_cast<float>(r)); | ||
| 137 | } | ||
| 138 | } | ||
| 139 |