Gamedev Framework (gf)  0.5.0
A C++11 framework for 2D games
Rect.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_RECT_H
25 #define GF_RECT_H
26 
27 #include <cstddef>
28 #include <algorithm>
29 
30 #include "Portability.h"
31 #include "Vector.h"
32 
33 namespace gf {
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 inline namespace v1 {
36 #endif
37 
87  template<typename T>
88  struct Rect {
92  union {
93  struct {
94  T left;
95  T top;
96  T width;
98  };
99  struct {
102  };
103  };
104 
111  constexpr Rect() noexcept
112  : left{0}, top{0}, width{0}, height{0}
113  {
114 
115  }
116 
128  constexpr Rect(T rectLeft, T rectTop, T rectWidth, T rectHeight) noexcept
129  : left{rectLeft}, top{rectTop}, width{rectWidth}, height{rectHeight}
130  {
131 
132  }
133 
143  Rect(const Vector<T, 2>& rectPosition, const Vector<T, 2>& rectSize) noexcept
144  : position(rectPosition), size(rectSize)
145  {
146 
147  }
148 
157  constexpr Vector<T, 2> getPosition() const noexcept {
158  return position;
159  }
160 
169  constexpr Vector<T, 2> getSize() const noexcept {
170  return size;
171  }
172 
181  constexpr bool isEmpty() const noexcept {
182  return width == 0 || height == 0;
183  }
184 
190  constexpr Vector<T, 2> getCenter() const noexcept {
191  return { left + width / 2, top + height / 2 };
192  }
193 
199  constexpr Vector<T, 2> getTopLeft() const noexcept {
200  return { left, top };
201  }
202 
208  constexpr Vector<T, 2> getTopRight() const noexcept {
209  return { left + width, top };
210  }
211 
217  constexpr Vector<T, 2> getBottomLeft() const noexcept {
218  return { left, top + height };
219  }
220 
226  constexpr Vector<T, 2> getBottomRight() const noexcept {
227  return { left + width, top + height };
228  }
229 
230 
238  constexpr bool contains(const Vector<T, 2>& point) const noexcept {
239  return left <= point.x && point.x < left + width && top <= point.y && point.y < top + height;
240  }
241 
249  constexpr bool contains(const Rect<T>& other) const noexcept {
250  return left <= other.left && other.left + other.width <= left + width
251  && top <= other.top && other.top + other.height <= top + height;
252  }
253 
261  constexpr bool intersects(const Rect<T>& other) const noexcept {
262  return left + width > other.left && left < other.left + other.width
263  && top + height > other.top && top < other.top + other.height;
264  }
265 
277  bool intersects(const Rect<T>& other, Rect<T>& result) const noexcept {
278  if (!intersects(other)) {
279  result = Rect<T>();
280  return false;
281  }
282 
283  T resultLeft = std::max(left, other.left);
284  T resultTop = std::max(top, other.top);
285  T resultRight = std::min(left + width, other.left + other.width);
286  T resultBottom = std::min(top + height, other.top + other.height);
287 
288  result = Rect<T>(resultLeft, resultTop, resultRight - resultLeft, resultBottom - resultTop);
289  return true;
290  }
291 
299  constexpr Rect<T> extend(T value) const noexcept {
300  return Rect<T>(left - value, top - value, width + 2 * value, height + 2 * value);
301  }
302 
310  constexpr Rect<T> shrink(T value) const noexcept {
311  return Rect<T>(left + value, top + value, width - 2 * value, height - 2 * value);
312  }
313  };
314 
322 
329  using RectI = Rect<int>;
330 
338 
346 
347 // MSVC does not like extern template
348 #ifndef _MSC_VER
349  extern template struct Rect<float>;
350  extern template struct Rect<int>;
351  extern template struct Rect<unsigned>;
352 #endif
353 
362  template<typename T>
363  inline
364  bool operator==(const Rect<T>& lhs, const Rect<T>& rhs) {
365  return lhs.position == rhs.position && lhs.size == rhs.size;
366  }
367 
376  template<typename T>
377  inline
378  bool operator!=(const Rect<T>& lhs, const Rect<T>& rhs) {
379  return lhs.position != rhs.position || lhs.size != rhs.size;
380  }
381 
382 #ifndef DOXYGEN_SHOULD_SKIP_THIS
383 }
384 #endif
385 }
386 
387 #endif // GF_RECT_H
T top
Top coordinate of the rectangle.
Definition: Rect.h:95
Rect(const Vector< T, 2 > &rectPosition, const Vector< T, 2 > &rectSize) noexcept
Construct the rectangle from position and size.
Definition: Rect.h:143
constexpr Vector< T, 2 > getBottomLeft() const noexcept
Get the bottom left corner.
Definition: Rect.h:217
bool operator!=(const Rect< T > &lhs, const Rect< T > &rhs)
Inequality operator.
Definition: Rect.h:378
constexpr bool isEmpty() const noexcept
Check if the rectangle is empty.
Definition: Rect.h:181
Vector< T, 2 > position
Position of the rectangle.
Definition: Rect.h:100
bool intersects(const Rect< T > &other, Rect< T > &result) const noexcept
Check the intersection between two rectangles.
Definition: Rect.h:277
constexpr Vector< T, 2 > getPosition() const noexcept
Get the position of the rectangle.
Definition: Rect.h:157
constexpr Rect< T > extend(T value) const noexcept
Extend the rectangle.
Definition: Rect.h:299
constexpr bool intersects(const Rect< T > &other) const noexcept
Check the intersection between two rectangles.
Definition: Rect.h:261
constexpr bool contains(const Rect< T > &other) const noexcept
Check if a rectangle is inside the rectangle&#39;s area.
Definition: Rect.h:249
constexpr Vector< T, 2 > getTopRight() const noexcept
Get the top right corner.
Definition: Rect.h:208
T width
Width of the rectangle.
Definition: Rect.h:96
Utility class for manipulating 2D axis aligned rectangles.
Definition: Rect.h:88
The namespace for gf classes.
Definition: Action.h:34
constexpr Rect< T > shrink(T value) const noexcept
Shrink the rectangle.
Definition: Rect.h:310
constexpr Vector< T, 2 > getCenter() const noexcept
Get the center of the rectangle.
Definition: Rect.h:190
Vector< T, 2 > size
Size of the rectangle.
Definition: Rect.h:101
A 2D vector.
Definition: Vector.h:298
constexpr Rect() noexcept
Default constructor.
Definition: Rect.h:111
constexpr Vector< T, 2 > getTopLeft() const noexcept
Get the top left corner.
Definition: Rect.h:199
T left
Left coordinate of the rectangle.
Definition: Rect.h:94
T height
Height of the rectangle.
Definition: Rect.h:97
constexpr Vector< T, 2 > getBottomRight() const noexcept
Get the bottom right corner.
Definition: Rect.h:226
constexpr bool contains(const Vector< T, 2 > &point) const noexcept
Check if a point is inside the rectangle&#39;s area.
Definition: Rect.h:238
constexpr Vector< T, 2 > getSize() const noexcept
Get the size of the rectangle.
Definition: Rect.h:169
constexpr Rect(T rectLeft, T rectTop, T rectWidth, T rectHeight) noexcept
Construct the rectangle from its coordinates.
Definition: Rect.h:128
bool operator==(const Rect< T > &lhs, const Rect< T > &rhs)
Equality operator.
Definition: Rect.h:364