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 <Raylib.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 ray::Vector2& pos) noexcept;
95
101 [[nodiscard]]
102 Type get_right() const noexcept;
103
109 [[nodiscard]]
110 Type get_bottom() const noexcept;
111
117 [[nodiscard]]
118 ray::Vector2 get_top_left() const noexcept;
119
125 [[nodiscard]]
126 ray::Vector2 get_center() const noexcept;
127
133 [[nodiscard]]
134 ray::Vector2 get_size() const noexcept;
135
139 [[nodiscard]]
140 auto operator<=>(const Rect&) const noexcept = default;
141
142 public:
146 Type m_xpos;
147
151 Type m_ypos;
152
157
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 : m_xpos {0}
191 , m_ypos {0}
192 , m_width {0}
193 , m_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 : m_xpos {x}
200 , m_ypos {y}
201 , m_width {width}
202 , m_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 > m_xpos) && (x < (m_xpos + m_width)) && (y > m_ypos) && (y < (m_ypos + m_height)));
216 }
217
218 template<meta::is_arithmetic Type>
219 inline bool Rect<Type>::contains(const Rect<Type>& b) const noexcept
220 {
221 return m_xpos <= b.m_xpos && b.get_right() <= get_right() && m_ypos <= b.m_ypos && 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(m_xpos, b.m_xpos, b.m_xpos + b.m_width) || value_in_range(b.m_xpos, m_xpos, m_xpos + m_width);
229 const auto y = value_in_range(m_ypos, b.m_ypos, b.m_ypos + b.m_height) || value_in_range(b.m_ypos, m_ypos, m_ypos + m_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 !(m_xpos >= b.get_right() || get_right() <= b.m_xpos || m_ypos >= b.get_bottom() || get_bottom() <= b.m_ypos);
238 }
239
240 template<meta::is_arithmetic Type>
241 inline void Rect<Type>::set_top_left(const ray::Vector2& pos) noexcept
242 {
243 m_xpos = static_cast<Type>(pos.x);
244 m_ypos = static_cast<Type>(pos.y);
245 }
246
247 template<meta::is_arithmetic Type>
248 inline Type Rect<Type>::get_right() const noexcept
249 {
250 return m_xpos + m_width;
251 }
252
253 template<meta::is_arithmetic Type>
254 inline Type Rect<Type>::get_bottom() const noexcept
255 {
256 return m_ypos + m_height;
257 }
258
259 template<meta::is_arithmetic Type>
260 inline ray::Vector2 Rect<Type>::get_top_left() const noexcept
261 {
262 return {static_cast<float>(m_xpos), static_cast<float>(m_ypos)};
263 }
264
265 template<meta::is_arithmetic Type>
266 inline ray::Vector2 Rect<Type>::get_center() const noexcept
267 {
268 return {static_cast<float>((m_xpos + m_width) / 2.0), static_cast<float>((m_ypos + m_height) / 2.0)};
269 }
270
271 template<meta::is_arithmetic Type>
272 inline ray::Vector2 Rect<Type>::get_size() const noexcept
273 {
274 return {static_cast<float>(m_width), static_cast<float>(m_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
bool contains(const Type _x, const Type _y) const noexcept
ray::Vector2 get_top_left() const noexcept
ray::Vector2 get_size() const noexcept
bool overlaps(const Rect< Type > &b) noexcept
Type get_right() const noexcept
ray::Vector2 get_center() const noexcept
bool value_in_range(const Type value, const Type min, const Type max) noexcept
void set_top_left(const ray::Vector2 &pos) noexcept
~Rect() noexcept
Destructor.
Definition Rect.hpp:207
Type get_bottom() const noexcept
Rect() noexcept
Constructor.
Definition Rect.hpp:189
Application.hpp galaxy.
Rect< int > iRect
Type definition for an int rectangle.
Definition Rect.hpp:181
Rect< float > fRect
Type definition for a floating point rectangle.
Definition Rect.hpp:186