Euphoria
angle.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 
5 #include "base/numeric.h"
7 
8 
9 namespace eu
10 {
11  struct Random;
12 
13  struct Angle
14  {
15  [[nodiscard]] constexpr static Angle
16  from_degrees(float degrees)
17  {
18  return Angle(c_degrees_to_radian(degrees));
19  }
20 
21  [[nodiscard]] constexpr static Angle
22  from_radians(float radians)
23  {
24  return Angle(radians);
25  }
26 
27  [[nodiscard]] constexpr static Angle
28  from_percent_of_360(float percent)
29  {
30  return Angle::from_radians(percent * pi * 2.0f);
31  }
32 
33  [[nodiscard]] constexpr static Angle
34  from_percent_of_180(float percent)
35  {
36  return Angle::from_radians(percent * pi);
37  }
38 
39 
40  void wrap();
41 
42 
43  [[nodiscard]] constexpr float
44  as_degrees() const
45  {
46  return c_radian_to_degrees(radians);
47  }
48 
49  [[nodiscard]] constexpr float
50  as_radians() const
51  {
52  return radians;
53  }
54 
55  [[nodiscard]] constexpr float
57  {
58  return as_radians() / (pi * 2.0f);
59  }
60 
61  [[nodiscard]] Angle get_wrapped() const;
62 
63  void operator+=(const Angle& rhs);
64  void operator-=(const Angle& rhs);
65  void operator*=(float rhs);
66  void operator/=(float rhs);
67  Angle operator-() const;
68 
69  private:
70  float radians;
71 
72  constexpr explicit Angle(float r) : radians(r) {}
73 
74  [[nodiscard]] static constexpr float
75  c_radian_to_degrees(float radians)
76  {
77  return (180.0f / pi) * radians;
78  }
79 
80  [[nodiscard]] static constexpr float
81  c_degrees_to_radian(float degrees)
82  {
83  return pi / 180.0f * degrees;
84  }
85  };
86 
87  constexpr Angle one_turn = Angle::from_radians(pi * 2.0f);
91 
92  [[nodiscard]] Angle get_random_angle(::eu::Random* random);
93 
94 
95  float sin(const Angle& ang);
96  float cos(const Angle& ang);
97  float tan(const Angle& ang);
98  Angle asin(float v);
99  Angle acos(float v);
100  Angle atan(float v);
101  Angle atan2(float y, float x);
102 
103  Angle operator+(const Angle& lhs, const Angle& rhs);
104  Angle operator-(const Angle& lhs, const Angle& rhs);
105  Angle operator*(const Angle& lhs, float rhs);
106  Angle operator/(const Angle& lhs, float rhs);
107  Angle operator*(float rhs, const Angle& lhs);
108 
109  std::string to_string(const Angle& a);
110 
111  bool operator<(const Angle& lhs, const Angle& rhs);
112  bool operator<=(const Angle& lhs, const Angle& rhs);
113  bool operator>(const Angle& lhs, const Angle& rhs);
114  bool operator>=(const Angle& lhs, const Angle& rhs);
115 
116  Angle lerp_angle(const Angle& from, float v, const Angle& to);
117 
119 }
120 
122 
123 namespace eu::convert
124 {
125  constexpr Angle operator"" _deg(long double d)
126  {
127  return Angle::from_degrees(static_cast<float>(d));
128  }
129 
130 
131  constexpr Angle operator"" _rad(long double r)
132  {
133  return Angle::from_radians(static_cast<float>(r));
134  }
135 }
ADD_DEFAULT_FORMATTER(eu::Angle, std::string, eu::to_string)
Definition: assert.h:90
Angle operator+(const Angle &lhs, const Angle &rhs)
Definition: angle.cc:123
constexpr Angle quarter_turn
Definition: angle.h:89
Angle acos(float v)
Definition: angle.cc:91
Angle atan2(float y, float x)
Definition: angle.cc:107
constexpr float pi
Definition: numeric.h:127
constexpr Angle half_turn
Definition: angle.h:88
Angle operator-(const Angle &lhs, const Angle &rhs)
Definition: angle.cc:132
Angle operator/(const Angle &lhs, float rhs)
Definition: angle.cc:141
constexpr Angle one_turn
Definition: angle.h:87
constexpr Angle no_rotation
Definition: angle.h:90
Angle asin(float v)
Definition: angle.cc:82
float tan(const Angle &ang)
Definition: angle.cc:75
Angle lerp_angle(const Angle &from, float v, const Angle &to)
Definition: angle.cc:194
std::string to_string(const Aabb &a)
Definition: aabb.cc:110
DEFAULT_INTERPOLATE(Angle, lerp_angle)
float cos(const Angle &ang)
Definition: angle.cc:68
bool operator>=(const Angle &lhs, const Angle &rhs)
Definition: angle.cc:188
Angle atan(float v)
Definition: angle.cc:100
Angle get_random_angle(::eu::Random *random)
Definition: angle.cc:12
float sin(const Angle &ang)
Definition: angle.cc:61
bool operator<(const Angle &lhs, const Angle &rhs)
Definition: angle.cc:167
Angle operator*(const Angle &lhs, float rhs)
Definition: angle.cc:149
bool operator>(const Angle &lhs, const Angle &rhs)
Definition: angle.cc:181
bool operator<=(const Angle &lhs, const Angle &rhs)
Definition: angle.cc:174
Angle operator-() const
Definition: angle.cc:54
constexpr float as_radians() const
Definition: angle.h:50
void operator-=(const Angle &rhs)
Definition: angle.cc:33
void wrap()
Definition: angle.cc:19
void operator+=(const Angle &rhs)
Definition: angle.cc:26
constexpr float as_degrees() const
Definition: angle.h:44
void operator/=(float rhs)
Definition: angle.cc:47
constexpr static Angle from_percent_of_180(float percent)
Definition: angle.h:34
constexpr static Angle from_percent_of_360(float percent)
Definition: angle.h:28
constexpr static Angle from_degrees(float degrees)
Definition: angle.h:16
constexpr static Angle from_radians(float radians)
Definition: angle.h:22
Angle get_wrapped() const
Definition: angle.cc:114
constexpr float from_percent_of_360() const
Definition: angle.h:56
void operator*=(float rhs)
Definition: angle.cc:40
WEL512 Random Number Generator.
Definition: random.h:21