Euphoria
angle.cc
Go to the documentation of this file.
1 #include "base/angle.h"
2 
3 #include "assert/assert.h"
4 #include "base/range.h"
5 #include "base/random.h"
6 
7 #include <cmath>
8 
9 namespace eu
10 {
11  Angle
13  {
14  return Angle::from_percent_of_360(random->get_next_float01());
15  }
16 
17 
18  void
20  {
21  radians = ::eu::wrap(make_range(pi * 2.0f), radians);
22  }
23 
24 
25  void
27  {
28  radians += rhs.radians;
29  }
30 
31 
32  void
34  {
35  radians -= rhs.radians;
36  }
37 
38 
39  void
40  Angle::operator*=(float rhs)
41  {
42  radians *= rhs;
43  }
44 
45 
46  void
47  Angle::operator/=(float rhs)
48  {
49  radians /= rhs;
50  }
51 
52 
53  Angle
55  {
56  return Angle::from_radians(-radians);
57  }
58 
59 
60  float
61  sin(const Angle& ang)
62  {
63  return std::sin(ang.as_radians());
64  }
65 
66 
67  float
68  cos(const Angle& ang)
69  {
70  return std::cos(ang.as_radians());
71  }
72 
73 
74  float
75  tan(const Angle& ang)
76  {
77  return std::tan(ang.as_radians());
78  }
79 
80 
81  Angle
82  asin(float v)
83  {
84  ASSERT(v <= 1 && "v must be smaller than 1");
85  ASSERT(v >= -1 && "v must be greater than -1");
86  return Angle::from_radians(std::asin(v));
87  }
88 
89 
90  Angle
91  acos(float v)
92  {
93  ASSERT(v <= 1 && "v must be smaller than 1");
94  ASSERT(v >= -1 && "v must be greater than -1");
95  return Angle::from_radians(std::acos(v));
96  }
97 
98 
99  Angle
100  atan(float v)
101  {
102  return Angle::from_radians(std::atan(v));
103  }
104 
105 
106  Angle
107  atan2(float y, float x)
108  {
109  return Angle::from_radians(std::atan2(y, x));
110  }
111 
112 
113  Angle
115  {
116  Angle temp = *this;
117  temp.wrap();
118  return temp;
119  }
120 
121 
122  Angle
123  operator+(const Angle& lhs, const Angle& rhs)
124  {
125  Angle temp(lhs);
126  temp += rhs;
127  return temp;
128  }
129 
130 
131  Angle
132  operator-(const Angle& lhs, const Angle& rhs)
133  {
134  Angle temp(lhs);
135  temp -= rhs;
136  return temp;
137  }
138 
139 
140  Angle
141  operator/(const Angle& lhs, float rhs)
142  {
143  Angle temp(lhs);
144  temp /= rhs;
145  return temp;
146  }
147 
148 
149  Angle operator*(const Angle& lhs, float rhs)
150  {
151  Angle temp(lhs);
152  temp *= rhs;
153  return temp;
154  }
155 
156  Angle operator*(float rhs, const Angle& lhs)
157  {
158  return lhs * rhs;
159  }
160 
161 
162  std::string to_string(const Angle& a)
163  { return fmt::format("{} deg", a.as_degrees()); }
164 
165 
166  bool
167  operator<(const Angle& lhs, const Angle& rhs)
168  {
169  return lhs.as_radians() < rhs.as_radians();
170  }
171 
172 
173  bool
174  operator<=(const Angle& lhs, const Angle& rhs)
175  {
176  return lhs.as_radians() <= rhs.as_radians();
177  }
178 
179 
180  bool
181  operator>(const Angle& lhs, const Angle& rhs)
182  {
183  return lhs.as_radians() > rhs.as_radians();
184  }
185 
186 
187  bool
188  operator>=(const Angle& lhs, const Angle& rhs)
189  {
190  return lhs.as_radians() >= rhs.as_radians();
191  }
192 
193 
194  Angle lerp_angle(const Angle& from, float v, const Angle& to)
195  {
196  // https://gamedev.stackexchange.com/a/72364
197  const auto dtheta = to - from;
198 
199  const auto new_from = dtheta > half_turn
200  ? from + one_turn
201  : dtheta < -half_turn
202  ? from - one_turn
203  : from
204  ;
205 
206  return from + ( to - new_from ) * v;
207  }
208 }
#define ASSERT(x)
Definition: assert.h:29
Definition: assert.h:90
Angle operator+(const Angle &lhs, const Angle &rhs)
Definition: angle.cc:123
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
Angle asin(float v)
Definition: angle.cc:82
Range< T > make_range(T min, T max)
Definition: range.h:39
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
T wrap(const Range< T > &range, T value)
Definition: range.h:130
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
void operator/=(float rhs)
Definition: angle.cc:47
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