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 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 glm::vec<2, Type, glm::defaultp>& pos) noexcept
242 {
243 m_xpos = pos.x;
244 m_ypos = 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 glm::vec<2, Type, glm::defaultp> Rect<Type>::get_top_left() const noexcept
261 {
262 return {m_xpos, m_ypos};
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 {(m_xpos + m_width) / 2.0, (m_ypos + m_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 {m_width, 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
Do two rectangles intersect.
Definition Rect.hpp:235
Type m_height
Height of rectangle.
Definition Rect.hpp:161
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
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 m_ypos
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
Type m_width
Width of rectangle.
Definition Rect.hpp:156
Type m_xpos
X position.
Definition Rect.hpp:146
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
Animated.cpp galaxy.
Definition Animated.cpp:16