galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Rect.hpp
Go to the documentation of this file.
1
7
8#ifndef GALAXY_MATH_RECT_HPP_
9#define GALAXY_MATH_RECT_HPP_
10
11#include <compare>
12
13#include <glm/vec2.hpp>
14
16
17namespace galaxy
18{
24 template<meta::is_arithmetic Type>
25 class Rect final
26 {
27 public:
31 Rect() noexcept;
32
41 Rect(const Type x, const Type y, const Type width, const Type height) noexcept;
42
46 ~Rect() noexcept;
47
56 [[nodiscard]]
57 bool contains(const Type _x, const Type _y) const noexcept;
58
66 [[nodiscard]]
67 bool contains(const Rect<Type>& b) const noexcept;
68
76 [[nodiscard]]
77 bool overlaps(const Rect<Type>& b) noexcept;
78
86 [[nodiscard]]
87 bool intersects(const Rect<Type>& b) const noexcept;
88
94 void set_top_left(const glm::vec<2, Type, glm::defaultp>& pos) noexcept;
95
101 [[nodiscard]]
102 Type get_right() const noexcept;
103
109 [[nodiscard]]
110 Type get_bottom() const noexcept;
111
117 [[nodiscard]]
118 glm::vec<2, Type, glm::defaultp> get_top_left() const noexcept;
119
125 [[nodiscard]]
126 glm::vec<2, Type, glm::defaultp> get_center() const noexcept;
127
133 [[nodiscard]]
134 glm::vec<2, Type, glm::defaultp> get_size() const noexcept;
135
139 [[nodiscard]]
140 auto operator<=>(const Rect&) const noexcept = default;
141
142 public:
146 Type x;
147
151 Type y;
152
156 Type width;
157
161 Type height;
162
163 private:
174 [[nodiscard]]
175 bool value_in_range(const Type value, const Type min, const Type max) noexcept;
176 };
177
181 typedef Rect<int> iRect;
182
186 typedef Rect<float> fRect;
187
188 template<meta::is_arithmetic Type>
189 inline Rect<Type>::Rect() noexcept
190 : x {0}
191 , y {0}
192 , width {0}
193 , height {0}
194 {
195 }
196
197 template<meta::is_arithmetic Type>
198 inline Rect<Type>::Rect(const Type x, const Type y, const Type width, const Type height) noexcept
199 : x {x}
200 , y {y}
201 , width {width}
202 , height {height}
203 {
204 }
205
206 template<meta::is_arithmetic Type>
207 inline Rect<Type>::~Rect() noexcept
208 {
209 }
210
211 template<meta::is_arithmetic Type>
212 inline bool Rect<Type>::contains(const Type _x, const Type _y) const noexcept
213 {
214 // Checks if the rectangle contains the point (x, y) using some basic math.
215 return ((_x > x) && (_x < (x + width)) && (_y > y) && (_y < (y + height)));
216 }
217
218 template<meta::is_arithmetic Type>
219 inline bool Rect<Type>::contains(const Rect<Type>& b) const noexcept
220 {
221 return x <= b.x && b.get_right() <= get_right() && y <= b.y && b.get_bottom() <= get_bottom();
222 }
223
224 template<meta::is_arithmetic Type>
225 inline bool Rect<Type>::overlaps(const Rect<Type>& b) noexcept
226 {
227 // Check for overlaps using math.
228 const auto _x = value_in_range(x, b.x, b.x + b.width) || value_in_range(b.x, x, x + width);
229 const auto _y = value_in_range(y, b.y, b.y + b.height) || value_in_range(b.y, y, y + height);
230
231 return _x && _y;
232 }
233
234 template<meta::is_arithmetic Type>
235 inline bool Rect<Type>::intersects(const Rect<Type>& b) const noexcept
236 {
237 return !(x >= b.get_right() || get_right() <= b.x || y >= b.get_bottom() || get_bottom() <= b.y);
238 }
239
240 template<meta::is_arithmetic Type>
241 inline void Rect<Type>::set_top_left(const glm::vec<2, Type, glm::defaultp>& pos) noexcept
242 {
243 x = pos.x;
244 y = pos.y;
245 }
246
247 template<meta::is_arithmetic Type>
248 inline Type Rect<Type>::get_right() const noexcept
249 {
250 return x + width;
251 }
252
253 template<meta::is_arithmetic Type>
254 inline Type Rect<Type>::get_bottom() const noexcept
255 {
256 return y + height;
257 }
258
259 template<meta::is_arithmetic Type>
260 inline glm::vec<2, Type, glm::defaultp> Rect<Type>::get_top_left() const noexcept
261 {
262 return {x, y};
263 }
264
265 template<meta::is_arithmetic Type>
266 inline glm::vec<2, Type, glm::defaultp> Rect<Type>::get_center() const noexcept
267 {
268 return {(x + width) / 2.0, (y + height) / 2.0};
269 }
270
271 template<meta::is_arithmetic Type>
272 inline glm::vec<2, Type, glm::defaultp> Rect<Type>::get_size() const noexcept
273 {
274 return {width, height};
275 }
276
277 template<meta::is_arithmetic Type>
278 inline bool Rect<Type>::value_in_range(const Type value, const Type min, const Type max) noexcept
279 {
280 // Check if a value is between min and max - i.e. in range.
281 return (value >= min) && (value <= max);
282 }
283} // namespace galaxy
284
285#endif
Represents a rectangle object.
Definition Rect.hpp:26
bool intersects(const Rect< Type > &b) const noexcept
Do two rectangles intersect.
Definition Rect.hpp:235
bool contains(const Type _x, const Type _y) const noexcept
Does the rectangle contain the point (x, y).
Definition Rect.hpp:212
bool overlaps(const Rect< Type > &b) noexcept
Do the rectangles a and b overlap.
Definition Rect.hpp:225
Type x
X position.
Definition Rect.hpp:146
Type height
Height of rectangle.
Definition Rect.hpp:161
void set_top_left(const glm::vec< 2, Type, glm::defaultp > &pos) noexcept
Set the top-left position.
Definition Rect.hpp:241
Type get_right() const noexcept
Get top right corner.
Definition Rect.hpp:248
bool value_in_range(const Type value, const Type min, const Type max) noexcept
Private function to determine if value is in range. Inclusive. Credits: https://stackoverflow....
Definition Rect.hpp:278
Type width
Width of rectangle.
Definition Rect.hpp:156
Type y
Y position.
Definition Rect.hpp:151
glm::vec< 2, Type, glm::defaultp > get_size() const noexcept
Gets width and height of rectangle.
Definition Rect.hpp:272
~Rect() noexcept
Destructor.
Definition Rect.hpp:207
glm::vec< 2, Type, glm::defaultp > get_top_left() const noexcept
Get the upper-left coordinate.
Definition Rect.hpp:260
Type get_bottom() const noexcept
Get bottom left corner.
Definition Rect.hpp:254
glm::vec< 2, Type, glm::defaultp > get_center() const noexcept
Gets the center of the rectangle.
Definition Rect.hpp:266
Rect() noexcept
Constructor.
Definition Rect.hpp:189
Timer.hpp galaxy.
Definition Timer.cpp:18