Gamedev Framework (gf)  0.8.0
A C++14 framework for 2D games
Rect.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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 
86  template<typename T>
87  struct Rect {
88  T left;
89  T top;
90  T width;
92 
99  constexpr Rect() noexcept
100  : left{0}, top{0}, width{0}, height{0}
101  {
102 
103  }
104 
116  constexpr Rect(T rectLeft, T rectTop, T rectWidth, T rectHeight) noexcept
117  : left{rectLeft}, top{rectTop}, width{rectWidth}, height{rectHeight}
118  {
119 
120  }
121 
131  Rect(const Vector<T, 2>& position, const Vector<T, 2>& size) noexcept
132  : left(position.x), top(position.y), width(size.width), height(size.height)
133  {
134 
135  }
136 
140  Rect(const Rect&) = default;
141 
145  Rect& operator=(const Rect&) = default;
146 
153  constexpr Vector<T, 2> getPosition() const noexcept {
154  return { left, top };
155  }
156 
163  constexpr void setPosition(Vector<T, 2> position) noexcept {
164  left = position.x;
165  top = position.y;
166  }
167 
174  constexpr Vector<T, 2> getSize() const noexcept {
175  return { width, height };
176  }
177 
184  constexpr void setSize(Vector<T, 2> size) noexcept {
185  width = size.width;
186  height = size.height;
187  }
188 
197  constexpr bool isEmpty() const noexcept {
198  return width == 0 || height == 0;
199  }
200 
206  constexpr Vector<T, 2> getCenter() const noexcept {
207  return { left + width / 2, top + height / 2 };
208  }
209 
215  constexpr Vector<T, 2> getTopLeft() const noexcept {
216  return { left, top };
217  }
218 
224  constexpr Vector<T, 2> getTopRight() const noexcept {
225  return { left + width, top };
226  }
227 
233  constexpr Vector<T, 2> getBottomLeft() const noexcept {
234  return { left, top + height };
235  }
236 
242  constexpr Vector<T, 2> getBottomRight() const noexcept {
243  return { left + width, top + height };
244  }
245 
246 
254  constexpr bool contains(const Vector<T, 2>& point) const noexcept {
255  return left <= point.x && point.x < left + width && top <= point.y && point.y < top + height;
256  }
257 
265  constexpr bool contains(const Rect<T>& other) const noexcept {
266  return left <= other.left && other.left + other.width <= left + width
267  && top <= other.top && other.top + other.height <= top + height;
268  }
269 
277  constexpr bool intersects(const Rect<T>& other) const noexcept {
278  return left + width > other.left && left < other.left + other.width
279  && top + height > other.top && top < other.top + other.height;
280  }
281 
293  bool intersects(const Rect<T>& other, Rect<T>& result) const noexcept {
294  if (!intersects(other)) {
295  result = Rect<T>();
296  return false;
297  }
298 
299  T resultLeft = std::max(left, other.left);
300  T resultTop = std::max(top, other.top);
301  T resultRight = std::min(left + width, other.left + other.width);
302  T resultBottom = std::min(top + height, other.top + other.height);
303 
304  result = Rect<T>(resultLeft, resultTop, resultRight - resultLeft, resultBottom - resultTop);
305  return true;
306  }
307 
315  constexpr Rect<T> grow(T value) const noexcept {
316  return Rect<T>(left - value, top - value, width + 2 * value, height + 2 * value);
317  }
318 
326  constexpr Rect<T> shrink(T value) const noexcept {
327  return Rect<T>(left + value, top + value, width - 2 * value, height - 2 * value);
328  }
329  };
330 
338 
345  using RectI = Rect<int>;
346 
354 
362 
363 // MSVC does not like extern template
364 #ifndef _MSC_VER
365  extern template struct Rect<float>;
366  extern template struct Rect<int>;
367  extern template struct Rect<unsigned>;
368 #endif
369 
378  template<typename T>
379  inline
380  bool operator==(const Rect<T>& lhs, const Rect<T>& rhs) {
381  return lhs.left == rhs.left && lhs.top == rhs.top && lhs.width == rhs.width && lhs.height == rhs.height;
382  }
383 
392  template<typename T>
393  inline
394  bool operator!=(const Rect<T>& lhs, const Rect<T>& rhs) {
395  return !(lhs == rhs);
396  }
397 
398 #ifndef DOXYGEN_SHOULD_SKIP_THIS
399 }
400 #endif
401 }
402 
403 #endif // GF_RECT_H
T top
Top coordinate of the rectangle.
Definition: Rect.h:89
constexpr Vector< T, 2 > getBottomLeft() const noexcept
Get the bottom left corner.
Definition: Rect.h:233
bool operator!=(const Rect< T > &lhs, const Rect< T > &rhs)
Inequality operator.
Definition: Rect.h:394
constexpr bool isEmpty() const noexcept
Check if the rectangle is empty.
Definition: Rect.h:197
bool intersects(const Rect< T > &other, Rect< T > &result) const noexcept
Check the intersection between two rectangles.
Definition: Rect.h:293
constexpr Vector< T, 2 > getPosition() const noexcept
Get the position of the rectangle.
Definition: Rect.h:153
constexpr Rect< T > grow(T value) const noexcept
Grow the rectangle.
Definition: Rect.h:315
constexpr void setPosition(Vector< T, 2 > position) noexcept
Set the position of the rectangle.
Definition: Rect.h:163
constexpr bool intersects(const Rect< T > &other) const noexcept
Check the intersection between two rectangles.
Definition: Rect.h:277
constexpr void setSize(Vector< T, 2 > size) noexcept
Set the size of the rectangle.
Definition: Rect.h:184
constexpr bool contains(const Rect< T > &other) const noexcept
Check if a rectangle is inside the rectangle&#39;s area.
Definition: Rect.h:265
constexpr Vector< T, 2 > getTopRight() const noexcept
Get the top right corner.
Definition: Rect.h:224
T width
Width of the rectangle.
Definition: Rect.h:90
Utility class for manipulating 2D axis aligned rectangles.
Definition: Rect.h:87
The namespace for gf classes.
Definition: Action.h:34
constexpr Rect< T > shrink(T value) const noexcept
Shrink the rectangle.
Definition: Rect.h:326
constexpr Vector< T, 2 > getCenter() const noexcept
Get the center of the rectangle.
Definition: Rect.h:206
A 2D vector.
Definition: Vector.h:316
constexpr Rect() noexcept
Default constructor.
Definition: Rect.h:99
constexpr Vector< T, 2 > getTopLeft() const noexcept
Get the top left corner.
Definition: Rect.h:215
T left
Left coordinate of the rectangle.
Definition: Rect.h:88
T height
Height of the rectangle.
Definition: Rect.h:91
constexpr Vector< T, 2 > getBottomRight() const noexcept
Get the bottom right corner.
Definition: Rect.h:242
constexpr bool contains(const Vector< T, 2 > &point) const noexcept
Check if a point is inside the rectangle&#39;s area.
Definition: Rect.h:254
constexpr Vector< T, 2 > getSize() const noexcept
Get the size of the rectangle.
Definition: Rect.h:174
constexpr Rect(T rectLeft, T rectTop, T rectWidth, T rectHeight) noexcept
Construct the rectangle from its coordinates.
Definition: Rect.h:116
bool operator==(const Rect< T > &lhs, const Rect< T > &rhs)
Equality operator.
Definition: Rect.h:380
Rect(const Vector< T, 2 > &position, const Vector< T, 2 > &size) noexcept
Construct the rectangle from position and size.
Definition: Rect.h:131