Euphoria
angle.h
Go to the documentation of this file.
1#pragma once
2
3#include "base/numeric.h"
4
5namespace eu
6{
12 struct An
13 {
14 [[nodiscard]] constexpr static An
15 from_degrees(float degrees)
16 {
17 return An(rad_from_deg(degrees));
18 }
19
20 [[nodiscard]] constexpr static An
21 from_radians(float radians)
22 {
23 return An(radians);
24 }
25
26 [[nodiscard]] constexpr static An
27 from_percent_of_360(float percent)
28 {
29 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 as_degrees() const
44 {
45 return deg_from_rad(radians);
46 }
47
48 [[nodiscard]] constexpr float
49 as_radians() const
50 {
51 return radians;
52 }
53
54 [[nodiscard]] constexpr float
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 constexpr explicit An(float r) : radians(r) {}
72
73 [[nodiscard]] static constexpr float
74 deg_from_rad(float radians)
75 {
76 return (180.0f / pi) * radians;
77 }
78
79 [[nodiscard]] static constexpr float
80 rad_from_deg(float degrees)
81 {
82 return pi / 180.0f * degrees;
83 }
84 };
85
86 constexpr An one_turn = An::from_radians(pi * 2.0f);
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
117}
118
119ADD_DEFAULT_FORMATTER(eu::An, std::string, eu::string_from);
120
121namespace eu::convert
122{
123 constexpr An operator""_deg(unsigned long long d)
124 {
125 return An::from_degrees(static_cast<float>(d));
126 }
127
128 constexpr An operator""_deg(long double d)
129 {
130 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}
#define ADD_CATCH_FORMATTER_DEF(TYPE)
Definition pch.public.h:22
constexpr An half_turn
Definition angle.h:87
An lerp_angle(const An &from, float v, const An &to)
constexpr An no_rotation
Definition angle.h:89
bool operator<(const An &lhs, const An &rhs)
float sin(const An &ang)
float tan(const An &ang)
An acos(float v)
An operator/(const An &lhs, float rhs)
constexpr An quarter_turn
Definition angle.h:88
An atan2(float y, float x)
bool operator>(const An &lhs, const An &rhs)
An operator-(const An &lhs, const An &rhs)
constexpr An one_turn
Definition angle.h:86
An asin(float v)
bool operator<=(const An &lhs, const An &rhs)
An operator+(const An &lhs, const An &rhs)
An operator*(const An &lhs, float rhs)
An atan(float v)
float cos(const An &ang)
bool operator>=(const An &lhs, const An &rhs)
std::string string_from(const An &a)
Definition assert.h:109
constexpr float pi
Definition numeric.h:13
An angle in both degrees and radians.
Definition angle.h:13
void operator-=(const An &rhs)
An get_wrapped() const
constexpr float as_degrees() const
Definition angle.h:43
void operator+=(const An &rhs)
constexpr float from_percent_of_360() const
Definition angle.h:55
static constexpr An from_degrees(float degrees)
Definition angle.h:15
An operator-() const
void operator/=(float rhs)
void wrap()
static constexpr An from_radians(float radians)
Definition angle.h:21
static constexpr An from_percent_of_180(float percent)
Definition angle.h:33
static constexpr An from_percent_of_360(float percent)
Definition angle.h:27
constexpr float as_radians() const
Definition angle.h:49
void operator*=(float rhs)