Euphoria
vec2.cc
Go to the documentation of this file.
1 #include "base/vec2.h"
2 
3 #include "base/random.h"
4 #include "base/angle.h"
5 
6 
7 
8 namespace eu
9 {
10  vec2f::vec2f(const std::tuple<float, float>& a)
11  : x(std::get<0>(a))
12  , y(std::get<1>(a))
13  {
14  }
15 
16  vec2f::vec2f(const unit2f& u)
17  : x(u.x)
18  , y(u.y)
19  {
20  }
21 
22  [[nodiscard]] vec2i
23  vec2f::to_i() const
24  {
25  return {static_cast<int>(x), static_cast<int>(y)};
26  }
27 
28 
29  float*
31  {
32  return &x;
33  }
34 
35  const float*
37  {
38  return &x;
39  }
40 
41  vec2f
42  vec2f::get_rotated(const Angle& a) const
43  {
44  const float nx = x * cos(a) - y * sin(a);
45  const float ny = x * sin(a) + y * cos(a);
46  return {nx, ny};
47  }
48 
49  vec2f
51  {
52  return {x, -y};
53  }
54 
55  void
57  {
58  x = x + rhs.x;
59  y = y + rhs.y;
60  }
61 
62  void
64  {
65  x = x - rhs.x;
66  y = y - rhs.y;
67  }
68 
69  vec2f
71  {
72  return {-x, -y};
73  }
74 
75  float
77  {
78  return x * x + y * y;
79  }
80 
81 
82  [[nodiscard]] vec2f
83  vec2f::from_to(const vec2f& from, const vec2f& to)
84  {
85  return {to.x - from.x, to.y - from.y};
86  }
87 
88  void
89  vec2f::operator/=(float rhs)
90  {
91  x = x / rhs;
92  y = y / rhs;
93  }
94 
95  void
96  vec2f::operator*=(float rhs)
97  {
98  x = x * rhs;
99  y = y * rhs;
100  }
101 
102  float
104  {
105  return sqrt(get_length_squared());
106  }
107 
108  float
110  {
111  const auto l = get_length();
112  *this /= l;
113  return l;
114  }
115 
118  {
119  vec2f r = *this;
120  const auto l = r.normalize();
121  return {unit2f{r}, l};
122  }
123 
124  unit2f
126  {
127  return get_normalized_and_length().normalized;
128  }
129 
130  vec2i::vec2i(const std::tuple<int, int>& a)
131  : x(std::get<0>(a))
132  , y(std::get<1>(a))
133  {
134  }
135 
136  [[nodiscard]] vec2f
137  vec2i::to_f() const
138  {
139  return {static_cast<float>(x), static_cast<float>(y)};
140  }
141 
142  int*
144  {
145  return &x;
146  }
147 
148  const int*
150  {
151  return &x;
152  }
153 
154  vec2i
156  {
157  return {x, -y};
158  }
159 
160  void
162  {
163  x = x + rhs.x;
164  y = y + rhs.y;
165  }
166 
167  void
169  {
170  x = x - rhs.x;
171  y = y - rhs.y;
172  }
173 
174  vec2i
176  {
177  return {-x, -y};
178  }
179 
180 
181  [[nodiscard]] vec2i
182  vec2i::from_to(const vec2i& from, const vec2i& to)
183  {
184  return {to.x - from.x, to.y - from.y};
185  }
186 
187  void
189  {
190  x = x * rhs;
191  y = y * rhs;
192  }
193 
194 
195  float*
197  {
198  return &x;
199  }
200 
201  const float*
203  {
204  return &x;
205  }
206 
207  unit2f
208  unit2f::get_rotated(const Angle& a) const
209  {
210  const float nx = x * cos(a) - y * sin(a);
211  const float ny = x * sin(a) + y * cos(a);
212  return unit2f{nx, ny};
213  }
214 
215  unit2f
217  {
218  return unit2f{x, -y};
219  }
220 
221  unit2f
223  {
224  return unit2f{-x, -y};
225  }
226 
227  float
229  {
230  return x * x + y * y;
231  }
232 
233  vec2f
235  {
236  return {x, y};
237  }
238 
239  [[nodiscard]] bool
241  {
242  return is_equal(get_length_squared(), 1.0f);
243  }
244 
245  unit2f::unit2f(float ax, float ay) : x(ax), y(ay)
246  {
247  ASSERT(is_valid());
248  }
249 
250  unit2f::unit2f(const vec2f& v) : x(v.x), y(v.y)
251  {
252  ASSERT(is_valid());
253  }
254 
255 
256  Scale2f::Scale2f(float ax, float ay)
257  : x(ax)
258  , y(ay)
259  {
260  }
261 
262  Scale2f::Scale2f(const std::tuple<float, float>& a)
263  : x(std::get<0>(a))
264  , y(std::get<1>(a))
265  {
266  }
267 
268  float*
270  {
271  return &x;
272  }
273 
274  const float*
276  {
277  return &x;
278  }
279 
280  Scale2f
281  Scale2f::get_rotated(const Angle& a) const
282  {
283  const float nx = x * cos(a) - y * sin(a);
284  const float ny = x * sin(a) + y * cos(a);
285  return {nx, ny};
286  }
287 
288  Scale2f
290  {
291  return {x, -y};
292  }
293 
294  Scale2f
296  {
297  return {-x, -y};
298  }
299 
300 
301  vec2f operator+(const vec2f& lhs, const vec2f& rhs)
302  {
303  vec2f r = lhs;
304  r += rhs;
305  return r;
306  }
307 
308  vec2f operator-(const vec2f& lhs, const vec2f& rhs)
309  {
310  vec2f r = lhs;
311  r -= rhs;
312  return r;
313  }
314 
315  vec2f operator*(const vec2f& lhs, float rhs)
316  {
317  vec2f r = lhs;
318  r *= rhs;
319  return r;
320  }
321 
322  vec2f operator*(float lhs, const vec2f& rhs)
323  {
324  vec2f r = rhs;
325  r *= lhs;
326  return r;
327  }
328 
329  vec2f operator*(const unit2f& lhs, float rhs)
330  {
331  vec2f r = vec2f{lhs};
332  r *= rhs;
333  return r;
334  }
335 
336  vec2f operator*(float lhs, const unit2f& rhs)
337  {
338  vec2f r{rhs};
339  r *= lhs;
340  return r;
341  }
342 
343  vec2f
344  operator/(const vec2f& lhs, float rhs)
345  {
346  vec2f r = lhs;
347  r /= rhs;
348  return r;
349  }
350 
351 
352  vec2i
353  operator+(const vec2i& lhs, const vec2i& rhs)
354  {
355  vec2i r = lhs;
356  r += rhs;
357  return r;
358  }
359 
360 
361  vec2i
362  operator-(const vec2i& lhs, const vec2i& rhs)
363  {
364  vec2i r = lhs;
365  r -= rhs;
366  return r;
367  }
368 
369 
370  vec2i operator*(const vec2i& lhs, int rhs)
371  {
372  vec2i r = lhs;
373  r *= rhs;
374  return r;
375  }
376 
377 
378  vec2i operator*(int lhs, const vec2i& rhs)
379  {
380  vec2i r = rhs;
381  r *= lhs;
382  return r;
383  }
384 
385 
386  bool
387  operator==(const vec2i& lhs, const vec2i& rhs)
388  {
389  return lhs.x == rhs.x && lhs.y == rhs.y;
390  }
391 
392  bool
393  operator!=(const vec2i& lhs, const vec2i& rhs)
394  {
395  return !(lhs == rhs);
396  }
397 
398 
399  float
400  dot(const vec2f& lhs, const vec2f& rhs)
401  {
402  return lhs.x * rhs.x + lhs.y * rhs.y;
403  }
404 
405 
406  vec2f lerp_vec2f(const vec2f& from, float v, const vec2f& to)
407  {
408  return
409  {
410  lerp_float(from.x, v, to.x),
411  lerp_float(from.y, v, to.y)
412  };
413  }
414 
415  vec2i lerp_vec2i(const vec2i& from, float v, const vec2i& to)
416  {
417  return
418  {
419  static_cast<int>(lerp_float(static_cast<float>(from.x), v, static_cast<float>(to.x))),
420  static_cast<int>(lerp_float(static_cast<float>(from.y), v, static_cast<float>(to.y)))
421  };
422  }
423 
424 
425  std::string to_string(const vec2f& v)
426  { return fmt::format("({}, {})", v.x, v.y); }
427 
428  std::string to_string(const unit2f& v)
429  { return fmt::format("({}, {})", v.x, v.y); }
430 
431  std::string to_string(const vec2i& v)
432  { return fmt::format("({}, {})", v.x, v.y); }
433 
434 
436  {
437  const auto angle = get_random_angle(random);
438  const auto x = cos(angle);
439  const auto y = sin(angle);
440  return unit2f{x, y};
441  }
442 }
#define ASSERT(x)
Definition: assert.h:29
Definition: assert.h:90
float sqrt(float r)
Definition: numeric.cc:101
Angle operator+(const Angle &lhs, const Angle &rhs)
Definition: angle.cc:123
bool operator==(const Lrud< T > &lhs, const Lrud< T > &rhs)
Definition: lrud.h:75
Angle operator-(const Angle &lhs, const Angle &rhs)
Definition: angle.cc:132
Angle operator/(const Angle &lhs, float rhs)
Definition: angle.cc:141
float lerp_float(float f, float scale, float t)
Definition: numeric.cc:87
std::string to_string(const Aabb &a)
Definition: aabb.cc:110
bool is_equal(int lhs, int rhs)
Definition: numeric.cc:13
bool operator!=(const Lrud< T > &lhs, const Lrud< T > &rhs)
Definition: lrud.h:88
float cos(const Angle &ang)
Definition: angle.cc:68
unit2f create_random_unit(Random *random)
Definition: vec2.cc:435
Angle get_random_angle(::eu::Random *random)
Definition: angle.cc:12
float sin(const Angle &ang)
Definition: angle.cc:61
Angle operator*(const Angle &lhs, float rhs)
Definition: angle.cc:149
float dot(const quatf &lhs, const quatf &rhs)
Definition: quat.cc:363
vec2f lerp_vec2f(const vec2f &from, float v, const vec2f &to)
Transform.
Definition: vec2.cc:406
vec2i lerp_vec2i(const vec2i &from, float v, const vec2i &to)
Definition: vec2.cc:415
WEL512 Random Number Generator.
Definition: random.h:21
Scale2f(float ax, float ay)
Definition: vec2.cc:256
Scale2f get_flipped_y() const
Definition: vec2.cc:289
Scale2f get_rotated(const Angle &a) const
Definition: vec2.cc:281
Scale2f operator-() const
Definition: vec2.cc:295
float x
Definition: vec2.h:138
float * get_data_ptr()
Definition: vec2.cc:269
float y
Definition: vec2.h:139
unit2f get_flipped_y() const
Definition: vec2.cc:216
float x
Definition: vec2.h:107
unit2f get_rotated(const Angle &a) const
Definition: vec2.cc:208
float y
Definition: vec2.h:108
unit2f(float ax, float ay)
Definition: vec2.cc:245
float * get_data_ptr()
Definition: vec2.cc:196
vec2f to_vec() const
Definition: vec2.cc:234
unit2f operator-() const
Definition: vec2.cc:222
float get_length_squared() const
Definition: vec2.cc:228
bool is_valid() const
Definition: vec2.cc:240
Definition: vec2.h:33
void operator*=(float rhs)
Definition: vec2.cc:96
unit2f get_normalized() const
Definition: vec2.cc:125
void operator-=(const vec2f &rhs)
Definition: vec2.cc:63
vec2f get_rotated(const Angle &a) const
Definition: vec2.cc:42
void operator/=(float rhs)
Definition: vec2.cc:89
float get_length() const
Definition: vec2.cc:103
static vec2f from_to(const vec2f &from, const vec2f &to)
Definition: vec2.cc:83
vec2f get_flipped_y() const
Definition: vec2.cc:50
vec2i to_i() const
Definition: vec2.cc:23
vec2f operator-() const
Definition: vec2.cc:70
NormalizedAndLength< unit2f, float > get_normalized_and_length() const
Definition: vec2.cc:117
float x
Definition: vec2.h:34
float * get_data_ptr()
Definition: vec2.cc:30
void operator+=(const vec2f &rhs)
Definition: vec2.cc:56
float get_length_squared() const
Definition: vec2.cc:76
float normalize()
Definition: vec2.cc:109
vec2f()=default
float y
Definition: vec2.h:35
Definition: vec2.h:72
vec2i get_flipped_y() const
Definition: vec2.cc:155
int y
Definition: vec2.h:74
vec2i operator-() const
Definition: vec2.cc:175
void operator*=(int rhs)
Definition: vec2.cc:188
int * get_data_ptr()
Definition: vec2.cc:143
vec2f to_f() const
Definition: vec2.cc:137
static vec2i from_to(const vec2i &from, const vec2i &to)
Definition: vec2.cc:182
void operator-=(const vec2i &rhs)
Definition: vec2.cc:168
int x
Definition: vec2.h:73
void operator+=(const vec2i &rhs)
Definition: vec2.cc:161
vec2i()=default