Euphoria
rect.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "assert/assert.h"
4 #include "base/vec2.h"
5 #include "base/size2.h"
6 #include "base/range.h"
7 
8 // Bottom, Left of screen is (0,0)
9 // X-axis is positive right, Y-axis is positive up
10 
11 
12 namespace eu
13 {
14  struct Random;
15 }
16 
17 // todo(Gustav): look into better names/easier discoverability for functions
18 
19 namespace eu
20 {
21 
22  struct Rectf;
23  struct Recti;
24 
25  // todo(Gustav): remove overloads and prefer vec2f or other relevant types
26  struct Rectf
27  {
28  float left;
29  float right;
30  float top;
31  float bottom;
32 
33  Rectf();
34 
35  bool operator==(const Rectf& rhs) = delete;
36 
37 
38  [[nodiscard]] static Rectf from_left_right_bottom_top(float left_side, float right_side, float bottom_side, float top_side);
39  [[nodiscard]] static Rectf from_left_right_top_bottom(float left_side, float right_side, float top_side, float bottom_side);
40  [[nodiscard]] static Rectf from_position_anchor_width_and_height
41  (
42  const vec2f& pos,
43  const Scale2f& anchor,
44  float width,
45  float height
46  );
47  [[nodiscard]] static Rectf from_bottom_left_width_height(const vec2f& bl, float width, float height);
48  [[nodiscard]] static Rectf from_top_left_width_height(const vec2f& topleft, float width, float height);
49  [[nodiscard]] static Rectf from_width_height(float width, float height);
50  [[nodiscard]] static Rectf from_width_height(const size2f& s);
51  [[nodiscard]] static Rectf from_point(const vec2f& point);
52 
53 
54  void translate(float dx, float dy);
55  void inset(float dx, float dy);
56  void inset(float l, float r, float t, float b);
57  void extend(float dx, float dy);
58  void include(const Rectf& o);
59  void expand(float expand);
60  void scale(float dx, float dy);
61  void set_empty();
62 
63 
64  // centers this rectangle inside the other rectangle and returns it without
65  // modifying this
66  [[nodiscard]] Rectf center_inside_other(const Rectf& other) const ;
67  [[nodiscard]] vec2f get_position_from_bottom_left(const vec2f& v) const ;
68  // does this contains the argument?
69  [[nodiscard]] bool contains_exclusive(const Rectf& r) const;
70 
71  // on the border is NOT considered included
72  [[nodiscard]] bool contains_exclusive(const vec2f& p) const;
73  [[nodiscard]] bool contains_exclusive(float x, float y) const;
74 
75  // on the border is considered included
76  [[nodiscard]] bool contains_inclusive(const vec2f& p) const;
77  [[nodiscard]] bool contains_inclusive(float x, float y) const;
78  [[nodiscard]] Rectf get_scaled_around_center_copy(float scale) const;
79  [[nodiscard]] Rectf scale_copy(float dx, float dy) const;
80  [[nodiscard]] Rectf inset_copy(float dx, float dy) const;
81  [[nodiscard]] Rectf inset_copy(float l, float r, float t, float b) const;
82  [[nodiscard]] Rectf extend_copy(float dx, float dy) const;
83  [[nodiscard]] Rectf extend_copy(float d) const;
84  [[nodiscard]] Rectf expand_copy(float expand) const;
85  [[nodiscard]] Rectf translate_copy(float dx, float dy) const;
86  [[nodiscard]] Rectf translate_copy(const vec2f& d) const;
87  [[nodiscard]] Rectf set_top_left_to_copy(float new_left, float new_top) const;
88  [[nodiscard]] Rectf set_top_left_to_copy(const vec2f& v) const;
89  [[nodiscard]] Rectf set_bottom_left_to_copy(float new_left, float new_bottom) const;
90  [[nodiscard]] Rectf set_bottom_left_to_copy(const vec2f& v) const;
91 
92 
93  [[nodiscard]] Recti to_i() const;
94  [[nodiscard]] vec2f get_bottom_left() const;
95  [[nodiscard]] float get_relative_center_x_from_bottom_left() const ;
96  [[nodiscard]] float get_relative_center_y_from_bottom_left() const ;
97  [[nodiscard]] vec2f get_relative_center_pos_from_bottom_left() const;
98  [[nodiscard]] float get_absolute_center_x() const;
99  [[nodiscard]] float get_absolute_center_y() const;
100  [[nodiscard]] vec2f get_absolute_center_pos() const;
101  // Returns true if the rectangle is empty (left >= right or top <= bottom)
102  [[nodiscard]] bool is_empty() const;
103 
104  // does this represent a rectangle? A 0 width/height is also considered valid
105  [[nodiscard]] bool is_valid() const;
106 
107  [[nodiscard]] float get_height() const;
108  [[nodiscard]] float get_width() const;
109 
110  [[nodiscard]] Range<float> get_range_y() const;
111  [[nodiscard]] Range<float> get_range_x() const;
112  [[nodiscard]] size2f get_size() const;
113  [[nodiscard]] vec2f get_top_left() const;
114  [[nodiscard]] vec2f get_top_right() const;
115  [[nodiscard]] vec2f get_bottom_right() const;
116 
117  private:
118  Rectf(float left_side, float right_side, float top_side, float bottom_side);
119  };
120 
121 
122 
123  // todo(Gustav): remove overloads and prefer vec2i or other relevant types
124  struct Recti
125  {
126  int left;
127  int right;
128  int top;
129  int bottom;
130 
131  Recti();
132 
133 
134  [[nodiscard]] static Recti from_position_anchor_width_and_height
135  (
136  const vec2i& pos,
137  const Scale2f& anchor,
138  int width,
139  int height
140  );
141 
142  [[nodiscard]] static Recti from_left_right_bottom_top(int left_side, int right_side, int bottom_side, int top_side);
143  [[nodiscard]] static Recti from_left_right_top_bottom(int left_side, int right_side, int top_side, int bottom_side);
144  [[nodiscard]] static Recti from_bottom_left_width_height(const vec2i& bl, int width, int height);
145  [[nodiscard]] static Recti from_top_left_width_height(const vec2i& topleft, int width, int height);
146  [[nodiscard]] static Recti from_width_height(int width, int height);
147  [[nodiscard]] static Recti from_width_height(const size2i& s);
148  [[nodiscard]] static Recti from_point(const vec2i& point);
149 
150  void translate(int dx, int dy);
151  void scale(int dx, int dy);
152  void inset(int dx, int dy);
153  void inset(int l, int r, int t, int b);
154  void expand(int expand);
155  void include(const Recti& o);
156  void extend(int dx, int dy);
157  void set_empty();
158 
159  // centers this rectangle inside the other rectangle and returns it without
160  // modifying this
161  [[nodiscard]] Recti center_inside_other(const Recti& other) const;
162  [[nodiscard]] vec2i get_position_from_bottom_left(const vec2i& v) const;
163 
164  // does this contains the argument?
165  [[nodiscard]] bool contains_exclusive(const Recti& r) const;
166 
167  // on the border is NOT considered included
168  [[nodiscard]] bool contains_exclusive(const vec2i& p) const;
169  [[nodiscard]] bool contains_exclusive(int x, int y) const;
170 
171  // on the border is considered included
172  [[nodiscard]] bool contains_inclusive(const vec2i& p) const;
173  [[nodiscard]] bool contains_inclusive(int x, int y) const;
174  [[nodiscard]] Recti get_scaled_around_center_copy(int scale) const;
175 
176  [[nodiscard]] Recti expand_copy(int expand) const;
177  [[nodiscard]] Recti translate_copy(int dx, int dy) const;
178  [[nodiscard]] Recti translate_copy(const vec2i& d) const;
179  [[nodiscard]] Recti extend_copy(int dx, int dy) const;
180  [[nodiscard]] Recti extend_copy(int d) const;
181  [[nodiscard]] Recti scale_copy(int dx, int dy) const;
182  [[nodiscard]] Recti inset_copy(int dx, int dy) const;
183  [[nodiscard]] Recti inset_copy(int l, int r, int t, int b) const;
184 
185  [[nodiscard]] Recti set_top_left_to_copy(int new_left, int new_top) const;
186  [[nodiscard]] Recti set_top_left_to_copy(const vec2i& v) const;
187  [[nodiscard]] Recti set_bottom_left_to_copy(int new_left, int new_bottom) const;
188  [[nodiscard]] Recti set_bottom_left_to_copy(const vec2i& v) const;
189 
190  [[nodiscard]] Rectf to_f() const;
191  [[nodiscard]] int get_relative_center_x_from_bottom_left() const;
192  [[nodiscard]] int get_relative_center_y_from_bottom_left() const;
193  [[nodiscard]] vec2i get_relative_center_pos_from_bottom_left() const;
194  [[nodiscard]] int get_absolute_center_x() const;
195  [[nodiscard]] int get_absolute_center_y() const;
196  [[nodiscard]] vec2i get_absolute_center_pos() const;
197 
198  // Returns true if the rectangle is empty (left >= right or top <= bottom)
199  [[nodiscard]] bool is_empty() const;
200 
201  // does this represent a rectangle? A 0 width/height is also considered valid
202  [[nodiscard]] bool is_valid() const;
203 
204  [[nodiscard]] vec2i get_top_left() const;
205  [[nodiscard]] vec2i get_top_right() const;
206  [[nodiscard]] vec2i get_bottom_left() const;
207  [[nodiscard]] vec2i get_bottom_right() const;
208 
209  [[nodiscard]] int get_height() const;
210  [[nodiscard]] int get_width() const;
211  [[nodiscard]] size2i get_size() const;
212 
213  [[nodiscard]] Range<int> get_range_y() const;
214  [[nodiscard]] Range<int> get_range_x() const;
215 
216 
217  private:
218  Recti(int left_side, int right_side, int top_side, int bottom_side);
219  };
220 
221 
222  [[nodiscard]] vec2f to01(const Rectf& r, const vec2f& from);
223  [[nodiscard]] vec2f to01(const Recti& r, const vec2i& from);
224  [[nodiscard]] vec2f from_01(const Rectf& r, const vec2f& from);
225  [[nodiscard]] vec2i from_01(const Recti& r, const vec2f& from);
226 
227  // todo(Gustav): add union and intersection functions
228 
229  // todo(Gustav): provide a keep_within, WrapWithin functions
230  // like is_within below
231  bool is_within(const Rectf& r, const vec2f& p);
232  bool is_within(const Recti& r, const vec2i& p);
233 
234  vec2f get_random_point(Random* random, const Rectf& r);
235  vec2i get_random_point(Random* random, const Recti& r);
236 
237  std::string to_string(const Rectf& r);
238  std::string to_string(const Recti& r);
239 
240  bool operator==(const Recti& lhs, const Recti& rhs);
241 }
242 
Definition: assert.h:90
bool operator==(const Lrud< T > &lhs, const Lrud< T > &rhs)
Definition: lrud.h:75
float to01(const Range< T > &range, T value)
Definition: range.h:84
std::string to_string(const Aabb &a)
Definition: aabb.cc:110
float from_01(const Range< float > &range, float value)
Definition: range.cc:13
bool is_within(const Range< T > &range, T value)
Definition: range.h:108
vec3f get_random_point(Random *rand, const Aabb &a)
Definition: aabb.cc:116
ADD_DEFAULT_FORMATTER(eu::Rectf, std::string, eu::to_string)
WEL512 Random Number Generator.
Definition: random.h:21
Definition: rect.h:27
void inset(float dx, float dy)
Definition: rect.cc:263
vec2f get_relative_center_pos_from_bottom_left() const
Definition: rect.cc:154
bool contains_inclusive(const vec2f &p) const
Definition: rect.cc:214
float get_relative_center_x_from_bottom_left() const
Definition: rect.cc:142
static Rectf from_position_anchor_width_and_height(const vec2f &pos, const Scale2f &anchor, float width, float height)
Definition: rect.cc:56
static Rectf from_point(const vec2f &point)
Definition: rect.cc:112
float get_width() const
Definition: rect.cc:436
size2f get_size() const
Definition: rect.cc:455
Rectf set_bottom_left_to_copy(float new_left, float new_bottom) const
Definition: rect.cc:404
Rectf translate_copy(float dx, float dy) const
Definition: rect.cc:372
float top
Definition: rect.h:30
bool contains_exclusive(const Rectf &r) const
Definition: rect.cc:182
static Rectf from_bottom_left_width_height(const vec2f &bl, float width, float height)
Definition: rect.cc:74
Range< float > get_range_y() const
Definition: rect.cc:443
bool operator==(const Rectf &rhs)=delete
bool is_valid() const
Definition: rect.cc:337
Range< float > get_range_x() const
Definition: rect.cc:449
Rectf get_scaled_around_center_copy(float scale) const
Definition: rect.cc:232
float right
Definition: rect.h:29
float get_height() const
Definition: rect.cc:430
static Rectf from_left_right_top_bottom(float left_side, float right_side, float top_side, float bottom_side)
Definition: rect.cc:47
vec2f get_top_left() const
Definition: rect.cc:461
static Rectf from_left_right_bottom_top(float left_side, float right_side, float bottom_side, float top_side)
Definition: rect.cc:39
Rectf()
Definition: rect.cc:9
Rectf center_inside_other(const Rectf &other) const
Definition: rect.cc:124
float get_relative_center_y_from_bottom_left() const
Definition: rect.cc:148
Rectf expand_copy(float expand) const
Definition: rect.cc:364
Recti to_i() const
Definition: rect.cc:27
static Rectf from_top_left_width_height(const vec2f &topleft, float width, float height)
Definition: rect.cc:86
float get_absolute_center_y() const
Definition: rect.cc:170
Rectf inset_copy(float dx, float dy) const
Definition: rect.cc:282
vec2f get_top_right() const
Definition: rect.cc:467
void translate(float dx, float dy)
Definition: rect.cc:344
vec2f get_bottom_left() const
Definition: rect.cc:118
vec2f get_absolute_center_pos() const
Definition: rect.cc:176
vec2f get_bottom_right() const
Definition: rect.cc:473
static Rectf from_width_height(float width, float height)
Definition: rect.cc:98
void scale(float dx, float dy)
Definition: rect.cc:244
void extend(float dx, float dy)
Definition: rect.cc:298
void include(const Rectf &o)
Definition: rect.cc:320
float get_absolute_center_x() const
Definition: rect.cc:164
Rectf extend_copy(float dx, float dy) const
Definition: rect.cc:305
bool is_empty() const
Definition: rect.cc:330
float left
Definition: rect.h:28
void expand(float expand)
Definition: rect.cc:354
void set_empty()
Definition: rect.cc:423
vec2f get_position_from_bottom_left(const vec2f &v) const
Definition: rect.cc:136
float bottom
Definition: rect.h:31
Rectf set_top_left_to_copy(float new_left, float new_top) const
Definition: rect.cc:387
Rectf scale_copy(float dx, float dy) const
Definition: rect.cc:254
Recti inset_copy(int dx, int dy) const
Definition: rect.cc:753
int left
Definition: rect.h:126
vec2i get_absolute_center_pos() const
Definition: rect.cc:652
bool contains_inclusive(const vec2i &p) const
Definition: rect.cc:689
void extend(int dx, int dy)
Definition: rect.cc:769
vec2i get_bottom_right() const
Definition: rect.cc:934
vec2i get_relative_center_pos_from_bottom_left() const
Definition: rect.cc:630
Recti extend_copy(int dx, int dy) const
Definition: rect.cc:775
static Recti from_position_anchor_width_and_height(const vec2i &pos, const Scale2f &anchor, int width, int height)
Definition: rect.cc:530
void translate(int dx, int dy)
Definition: rect.cc:810
Range< int > get_range_x() const
Definition: rect.cc:910
static Recti from_width_height(int width, int height)
Definition: rect.cc:573
Recti()
Definition: rect.cc:484
void inset(int dx, int dy)
Definition: rect.cc:735
static Recti from_left_right_bottom_top(int left_side, int right_side, int bottom_side, int top_side)
Definition: rect.cc:513
void set_empty()
Definition: rect.cc:884
vec2i get_position_from_bottom_left(const vec2i &v) const
Definition: rect.cc:612
int top
Definition: rect.h:128
Recti expand_copy(int expand) const
Definition: rect.cc:828
static Recti from_point(const vec2i &point)
Definition: rect.cc:587
void scale(int dx, int dy)
Definition: rect.cc:718
Recti center_inside_other(const Recti &other) const
Definition: rect.cc:600
int get_relative_center_y_from_bottom_left() const
Definition: rect.cc:624
void include(const Recti &o)
Definition: rect.cc:789
Rectf to_f() const
Definition: rect.cc:501
Recti get_scaled_around_center_copy(int scale) const
Definition: rect.cc:706
int get_absolute_center_x() const
Definition: rect.cc:640
vec2i get_top_right() const
Definition: rect.cc:928
int get_absolute_center_y() const
Definition: rect.cc:646
int bottom
Definition: rect.h:129
int get_relative_center_x_from_bottom_left() const
Definition: rect.cc:618
bool is_valid() const
Definition: rect.cc:804
bool contains_exclusive(const Recti &r) const
Definition: rect.cc:658
void expand(int expand)
Definition: rect.cc:819
Recti translate_copy(int dx, int dy) const
Definition: rect.cc:836
Range< int > get_range_y() const
Definition: rect.cc:904
vec2i get_bottom_left() const
Definition: rect.cc:593
Recti set_top_left_to_copy(int new_left, int new_top) const
Definition: rect.cc:850
size2i get_size() const
Definition: rect.cc:916
Recti set_bottom_left_to_copy(int new_left, int new_bottom) const
Definition: rect.cc:867
vec2i get_top_left() const
Definition: rect.cc:922
Recti scale_copy(int dx, int dy) const
Definition: rect.cc:727
int get_height() const
Definition: rect.cc:891
bool is_empty() const
Definition: rect.cc:798
int right
Definition: rect.h:127
static Recti from_bottom_left_width_height(const vec2i &bl, int width, int height)
Definition: rect.cc:548
static Recti from_left_right_top_bottom(int left_side, int right_side, int top_side, int bottom_side)
Definition: rect.cc:521
int get_width() const
Definition: rect.cc:897
static Recti from_top_left_width_height(const vec2i &topleft, int width, int height)
Definition: rect.cc:561
Definition: vec2.h:33
Definition: vec2.h:72