GameKit  0.0.1a
C++ gamedev tools
Rect.hpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: Rect.hpp
5  *
6  * Description:
7  *
8  * Created: 15/09/2014 22:13:37
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #ifndef GK_RECT_HPP_
15 #define GK_RECT_HPP_
16 
17 #include <algorithm>
18 
19 #include "gk/core/Vector2.hpp"
20 
21 namespace gk {
22 
27 template<typename T>
28 class Rect {
29  public:
37  Rect() = default;
38 
51  Rect(T _x, T _y, T _width, T _height)
52  : x(_x), y(_y), width(_width), height(_height) {}
53 
64  Rect(const Vector2<T> &position, const Vector2<T> &size)
65  : x(position.x), y(position.x), width(size.x), height(size.y) {}
66 
78  template<typename U>
79  explicit Rect(const Rect<U> &rect)
80  : Rect(rect.x, rect.y, rect.width, rect.height) {}
81 
96  bool contains(T _x, T _y) const {
97  T minX = std::min(x, static_cast<T>(x + width));
98  T maxX = std::max(x, static_cast<T>(x + width));
99  T minY = std::min(y, static_cast<T>(y + height));
100  T maxY = std::max(y, static_cast<T>(y + height));
101 
102  return (_x >= minX) && (_x < maxX) && (_y >= minY) && (_y < maxY);
103  }
104 
118  bool contains(const Vector2<T> &point) const {
119  return contains(point.x, point.y);
120  }
121 
132  bool intersects(const Rect<T> &rect) const {
133  T r1MinX = std::min(x, static_cast<T>(x + width));
134  T r1MaxX = std::max(x, static_cast<T>(x + width));
135  T r1MinY = std::min(y, static_cast<T>(y + height));
136  T r1MaxY = std::max(y, static_cast<T>(y + height));
137 
138  T r2MinX = std::min(rect.x, static_cast<T>(rect.x + rect.width));
139  T r2MaxX = std::max(rect.x, static_cast<T>(rect.x + rect.width));
140  T r2MinY = std::min(rect.y, static_cast<T>(rect.y + rect.height));
141  T r2MaxY = std::max(rect.y, static_cast<T>(rect.y + rect.height));
142 
143  T interLeft = std::max(r1MinX, r2MinX);
144  T interTop = std::max(r1MinY, r2MinY);
145  T interRight = std::min(r1MaxX, r2MaxX);
146  T interBottom = std::min(r1MaxY, r2MaxY);
147 
148  return interLeft < interRight && interTop < interBottom;
149  }
150 
161  T intersectionDirection(const Rect<T> &rect) const {
162  T r1MinX = std::min(x, static_cast<T>(x + width));
163  T r1MaxX = std::max(x, static_cast<T>(x + width));
164  T r1MinY = std::min(y, static_cast<T>(y + height));
165  T r1MaxY = std::max(y, static_cast<T>(y + height));
166 
167  T r2MinX = std::min(rect.x, static_cast<T>(rect.x + rect.width));
168  T r2MaxX = std::max(rect.x, static_cast<T>(rect.x + rect.width));
169  T r2MinY = std::min(rect.y, static_cast<T>(rect.y + rect.height));
170  T r2MaxY = std::max(rect.y, static_cast<T>(rect.y + rect.height));
171 
172  T interLeft = std::max(r1MinX, r2MinX);
173  T interTop = std::max(r1MinY, r2MinY);
174  T interRight = std::min(r1MaxX, r2MaxX);
175  T interBottom = std::min(r1MaxY, r2MaxY);
176 
177  if(interLeft < interRight && interTop < interBottom) {
178  if(interRight - interLeft < interBottom - interTop) {
179  return 1;
180  } else {
181  return 2;
182  }
183  } else {
184  return 0;
185  }
186  }
187 
198  bool operator==(const Rect<T> &rect) const { return x == rect.x && y == rect.y && width == rect.width && height == rect.height; }
199 
210  bool operator!=(const Rect<T> &rect) const { return !operator==(rect); }
211 
213  // Member data
215  T x = 0;
216  T y = 0;
217  T width = 0;
218  T height = 0;
219 };
220 
221 // Create typedefs for the most common types
224 
225 } // namespace gk
226 
227 #endif // GK_RECT_HPP_
228 
Rect()=default
Default constructor.
Rect(const Rect< U > &rect)
Construct the rectangle from another type of rectangle.
Definition: Rect.hpp:79
Rect(T _x, T _y, T _width, T _height)
Construct the rectangle from its coordinates.
Definition: Rect.hpp:51
bool contains(const Vector2< T > &point) const
Check if a point is inside the rectangle&#39;s area.
Definition: Rect.hpp:118
bool operator==(const Rect< T > &rect) const
Overload of binary operator ==.
Definition: Rect.hpp:198
T y
Top coordinate of the rectangle.
Definition: Rect.hpp:216
bool operator!=(const Rect< T > &rect) const
Overload of binary operator !=.
Definition: Rect.hpp:210
Utility class for manipulating 2D axis aligned rectangles.
Definition: Rect.hpp:28
bool contains(T _x, T _y) const
Check if a point is inside the rectangle&#39;s area.
Definition: Rect.hpp:96
T x
Left coordinate of the rectangle.
Definition: Rect.hpp:215
T intersectionDirection(const Rect< T > &rect) const
Check the intersection direction between two rectangles.
Definition: Rect.hpp:161
bool intersects(const Rect< T > &rect) const
Check the intersection between two rectangles.
Definition: Rect.hpp:132
T width
Width of the rectangle.
Definition: Rect.hpp:217
T height
Height of the rectangle.
Definition: Rect.hpp:218
Rect(const Vector2< T > &position, const Vector2< T > &size)
Construct the rectangle from position and size.
Definition: Rect.hpp:64