Euphoria
vec2.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tuple>
4 
5 #include "assert/assert.h"
6 
8 #include "base/angle.h"
9 #include "base/numeric.h"
10 #include "base/cint.h"
11 #include "base/vec.base.h"
12 
13 
14 namespace eu
15 {
16  struct Random;
17 
20 
21  // a 2d vector
22  struct vec2f; struct vec2i;
23 
24 
25  // a 2d unit (vector)
26  struct unit2f; struct unit2i;
27 
28 
30 
31 
32  struct vec2f
33  {
34  float x;
35  float y;
36 
37  vec2f() = default;
38  constexpr vec2f(float ax, float ay)
39  : x(ax)
40  , y(ay)
41  {
42  }
43  explicit vec2f(const std::tuple<float, float>& a);
44  explicit vec2f(const unit2f& u);
45 
46  [[nodiscard]] static vec2f from_to(const vec2f& from, const vec2f& to);
47 
48  float* get_data_ptr();
49  float normalize();
50 
51  [[nodiscard]] vec2f get_rotated(const Angle& a) const;
52 
53  [[nodiscard]] const float* get_data_ptr() const;
54  [[nodiscard]] vec2i to_i() const;
55  [[nodiscard]] vec2f get_flipped_y() const;
56  [[nodiscard]] float get_length_squared() const;
57  [[nodiscard]] float get_length() const;
59  [[nodiscard]] unit2f get_normalized() const;
60 
61  void operator+=(const vec2f& rhs);
62  void operator-=(const vec2f& rhs);
63  vec2f operator-() const;
64  void operator/=(float rhs);
65  void operator*=(float rhs);
66  };
67 
68  constexpr vec2f zero2f = vec2f{ 0, 0 };
69 
70 
71  struct vec2i
72  {
73  int x;
74  int y;
75 
76  vec2i() = default;
77  constexpr vec2i(int ax, int ay)
78  : x(ax)
79  , y(ay)
80  {
81  }
82  explicit vec2i(const std::tuple<int, int>& a);
83  explicit vec2i(const unit2i& u);
84 
85  [[nodiscard]] static vec2i from_to(const vec2i& from, const vec2i& to);
86 
87  int* get_data_ptr();
88  [[nodiscard]] const int* get_data_ptr() const;
89 
90  [[nodiscard]] vec2f to_f() const;
91  [[nodiscard]] vec2i get_flipped_y() const;
92 
93  void operator+=(const vec2i& rhs);
94  void operator-=(const vec2i& rhs);
95  vec2i operator-() const;
96  void operator*=(int rhs);
97  };
98 
99  constexpr vec2i zero2i = vec2i{ 0, 0 };
100 
101 
103 
104 
105  struct unit2f
106  {
107  float x;
108  float y;
109 
110  explicit unit2f(float ax, float ay);
111 
112  float* get_data_ptr();
113 
114  [[nodiscard]] unit2f get_rotated(const Angle& a) const;
115 
116  [[nodiscard]] const float* get_data_ptr() const;
117  [[nodiscard]] vec2f to_vec() const;
118  [[nodiscard]] unit2f get_flipped_y() const;
119 
120  // todo(Gustav): remove this...
121  [[nodiscard]] float get_length_squared() const;
122  [[nodiscard]] bool is_valid() const;
123 
124  unit2f operator-() const;
125 
126  private:
127  friend struct vec2f;
128 
129  explicit unit2f(const vec2f& v);
130  };
131 
132 
134 
135 
136  struct Scale2f
137  {
138  float x;
139  float y;
140 
141  Scale2f(float ax, float ay);
142  explicit Scale2f(const std::tuple<float, float>& a);
143  bool operator==(const Scale2f& rhs) = delete;
144 
145  float* get_data_ptr();
146 
147  [[nodiscard]] Scale2f get_rotated(const Angle& a) const;
148 
149  [[nodiscard]] const float* get_data_ptr() const;
150  [[nodiscard]] Scale2f get_flipped_y() const;
151 
152  Scale2f operator-() const;
153  };
154 
155 
158 
159  vec2f operator+(const vec2f& lhs, const vec2f& rhs);
160  vec2f operator-(const vec2f& lhs, const vec2f& rhs);
161  vec2f operator*(const vec2f& lhs, float rhs);
162  vec2f operator*(float lhs, const vec2f& rhs);
163  vec2f operator*(const unit2f& lhs, float rhs);
164  vec2f operator*(float lhs, const unit2f& rhs);
165  vec2f operator/(const vec2f& lhs, float rhs);
166 
167  vec2i operator+(const vec2i& lhs, const vec2i& rhs);
168  vec2i operator-(const vec2i& lhs, const vec2i& rhs);
169  vec2i operator*(const vec2i& lhs, int rhs);
170  vec2i operator*(int lhs, const vec2i& rhs);
171 
174 
175  bool operator==(const vec2i& lhs, const vec2i& rhs);
176  bool operator!=(const vec2i& lhs, const vec2i& rhs);
177 
178 
181 
182  float dot(const vec2f& lhs, const vec2f& rhs);
183 
186 
187  vec2f lerp_vec2f(const vec2f& from, float v, const vec2f& to);
188  vec2i lerp_vec2i(const vec2i& from, float v, const vec2i& to);
189 
192 
193  // todo(Gustav): add one lerp and one slerp unit transform?
194 
195 
198 
199  std::string to_string(const vec2f& v);
200  std::string to_string(const unit2f& v);
201  std::string to_string(const vec2i& v);
202 
203  // util functions
205 }
206 
Definition: assert.h:90
constexpr vec2f zero2f
Definition: vec2.h:68
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
constexpr vec2i zero2i
Definition: vec2.h:99
Angle operator-(const Angle &lhs, const Angle &rhs)
Definition: angle.cc:132
Angle operator/(const Angle &lhs, float rhs)
Definition: angle.cc:141
std::string to_string(const Aabb &a)
Definition: aabb.cc:110
bool operator!=(const Lrud< T > &lhs, const Lrud< T > &rhs)
Definition: lrud.h:88
DEFAULT_INTERPOLATE(Angle, lerp_angle)
unit2f create_random_unit(Random *random)
Definition: vec2.cc:435
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
bool operator==(const Scale2f &rhs)=delete
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
constexpr vec2f(float ax, float ay)
Definition: vec2.h:38
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
constexpr vec2i(int ax, int ay)
Definition: vec2.h:77
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(const unit2i &u)
vec2i()=default
ADD_DEFAULT_FORMATTER(eu::vec2f, std::string, eu::to_string)