Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Rect.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 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#include <limits>
30#include <tuple>
31
32#include "Anchor.h"
33#include "CoreApi.h"
34#include "Quarter.h"
35#include "Vector.h"
36#include "VectorOps.h"
37
38namespace gf {
39#ifndef DOXYGEN_SHOULD_SKIP_THIS
40inline namespace v1 {
41#endif
42
71 template<typename T>
72 struct Rect {
78 constexpr Rect() noexcept
79 : min(gf::Zero)
80 , max(gf::Zero)
81 {
82 }
83
91 static constexpr Rect<T> fromMinMax(Vector<T, 2> min, Vector<T, 2> max) noexcept {
92 return Rect<T>(min, max);
93 }
94
102 static constexpr Rect<T> fromPositionSize(Vector<T, 2> position, Vector<T, 2> size) noexcept {
103 return Rect<T>(position, position + size);
104 }
105
114 static constexpr Rect<T> fromSize(Vector<T, 2> size) noexcept {
115 return Rect<T>({ T(0), T(0) }, size);
116 }
117
125 static constexpr Rect<T> fromCenterSize(Vector<T, 2> center, Vector<T, 2> size) noexcept {
126 return Rect<T>(center - size / T(2), center + size / T(2));
127 }
128
134 static constexpr Rect<T> empty() noexcept {
135 return Rect<T>(Vector<T, 2>(std::numeric_limits<T>::max()), Vector<T, 2>(std::numeric_limits<T>::lowest()));
136 }
137
145 constexpr Vector<T, 2> getPosition() const noexcept {
146 return min;
147 }
148
154 constexpr Vector<T, 2> getSize() const noexcept {
155 return max - min;
156 }
157
163 constexpr bool isEmpty() const noexcept {
164 return min.x >= max.x || min.y >= max.y;
165 }
166
172 constexpr Vector<T, 2> getCenter() const noexcept {
173 return min + (max - min) / 2;
174 }
175
182 constexpr bool contains(Vector<T, 2> point) const noexcept {
183 return min.x <= point.x && point.x < max.x && min.y <= point.y && point.y < max.y;
184 }
185
192 constexpr bool contains(const Rect& other) const noexcept {
193 return min.x <= other.min.x && other.max.x <= max.x && min.y <= other.min.y && other.max.y <= max.y;
194 }
195
202 constexpr bool intersects(const Rect& other) const noexcept {
203 return min.x < other.max.x && other.min.x < max.x && min.y < other.max.y && other.min.y < max.y;
204 }
205
213 constexpr bool intersects(const Rect& other, Rect& result) const noexcept {
214 if (!intersects(other)) {
215 return false;
216 }
217
218 result = getIntersection(other);
219 return true;
220 }
221
228 constexpr Rect getIntersection(const Rect& other) const noexcept {
229 Rect res;
230
231 res.min.x = std::max(min.x, other.min.x);
232 res.min.y = std::max(min.y, other.min.y);
233 res.max.x = std::min(max.x, other.max.x);
234 res.max.y = std::min(max.y, other.max.y);
235
236 return res;
237 }
238
245 constexpr T getIntersectionVolume(const Rect& other) const noexcept {
246 auto xMin = std::max(min.x, other.min.x);
247 auto xMax = std::min(max.x, other.max.x);
248
249 if (xMin >= xMax) {
250 return T(0);
251 }
252
253 auto yMin = std::max(min.y, other.min.y);
254 auto yMax = std::min(max.y, other.max.y);
255
256 if (yMin >= yMax) {
257 return T(0);
258 }
259
260 return (xMax - xMin) * (yMax - yMin);
261 }
262
269 constexpr T getIntersectionExtentLength(const Rect& other) const noexcept {
270 auto xMin = std::max(min.x, other.min.x);
271 auto xMax = std::min(max.x, other.max.x);
272
273 if (xMin >= xMax) {
274 return T(0);
275 }
276
277 auto yMin = std::max(min.y, other.min.y);
278 auto yMax = std::min(max.y, other.max.y);
279
280 if (yMin >= yMax) {
281 return T(0);
282 }
283
284 return (xMax - xMin) + (yMax - yMin);
285 }
286
292 constexpr void extend(const T (&point)[2]) noexcept {
293 min.x = std::min(min.x, point[0]);
294 min.y = std::min(min.y, point[1]);
295 max.x = std::max(max.x, point[0]);
296 max.y = std::max(max.y, point[1]);
297 }
298
304 constexpr void extend(Vector<T, 2> point) noexcept {
305 min.x = std::min(min.x, point.x);
306 min.y = std::min(min.y, point.y);
307 max.x = std::max(max.x, point.x);
308 max.y = std::max(max.y, point.y);
309 }
310
316 constexpr void extend(const Rect& other) noexcept {
317 min.x = std::min(min.x, other.min.x);
318 min.y = std::min(min.y, other.min.y);
319 max.x = std::max(max.x, other.max.x);
320 max.y = std::max(max.y, other.max.y);
321 }
322
328 constexpr Rect getExtended(const Rect& other) const noexcept {
329 Rect res(*this);
330 res.extend(other);
331 return res;
332 }
333
339 constexpr T getVolume() const noexcept {
340 return (max.x - min.x) * (max.y - min.y);
341 }
342
348 constexpr T getExtentLength() const noexcept {
349 return (max.x - min.x) + (max.y - min.y);
350 }
351
357 constexpr T getMinimumEdge() const noexcept {
358 return std::min(max.x - min.x, max.y - min.y);
359 }
360
368 constexpr Rect grow(T value) const noexcept {
369 return Rect(min - value, max + value);
370 }
371
379 constexpr Rect shrink(T value) const noexcept {
380 return Rect(min + value, max - value);
381 }
382
386 constexpr void normalize() noexcept {
387 if (min.x > max.x) { std::swap(min.x, max.x); }
388 if (min.y > max.y) { std::swap(min.y, max.y); }
389 }
390
397 constexpr Vector<T, 2> getPositionFromAnchor(Anchor anchor) const noexcept {
398 switch (anchor) {
399 case Anchor::TopLeft:
400 return this->min;
402 return { (this->min.x + this->max.x) / T(2), this->min.y };
403 case Anchor::TopRight:
404 return { this->max.x, this->min.y };
406 return { this->min.x, (this->min.y + this->max.y) / T(2) };
407 case Anchor::Center:
408 return this->getCenter();
410 return { this->max.x, (this->min.y + this->max.y) / T(2) };
412 return { this->min.x, this->max.y };
414 return { (this->min.x + this->max.x) / T(2), this->max.y };
416 return this->max;
417 }
418
419 return this->min;
420 }
421
427 constexpr Vector<T, 2> getTopLeft() const noexcept {
428 return getPositionFromAnchor(Anchor::TopLeft);
429 }
430
436 constexpr Vector<T, 2> getTopRight() const noexcept {
437 return getPositionFromAnchor(Anchor::TopRight);
438 }
439
445 constexpr Vector<T, 2> getBottomLeft() const noexcept {
446 return getPositionFromAnchor(Anchor::BottomLeft);
447 }
448
454 constexpr Vector<T, 2> getBottomRight() const noexcept {
455 return getPositionFromAnchor(Anchor::BottomRight);
456 }
457
463 constexpr T getWidth() const noexcept {
464 return this->max.x - this->min.x;
465 }
466
472 constexpr T getHeight() const noexcept {
473 return this->max.y - this->min.y;
474 }
475
478
479 private:
480 constexpr Rect(Vector<T, 2> min, Vector<T, 2> max) noexcept
481 : min(min)
482 , max(max)
483 {
484 }
485 };
486
494
502
510
518
526
527// MSVC does not like extern template
528#ifndef _MSC_VER
529 extern template struct GF_CORE_API Rect<float>;
530 extern template struct GF_CORE_API Rect<double>;
531 extern template struct GF_CORE_API Rect<int>;
532 extern template struct GF_CORE_API Rect<unsigned>;
533#endif
534
543 template<typename T>
544 inline
545 bool operator==(const Rect<T>& lhs, const Rect<T>& rhs) {
546 return lhs.min == rhs.min && lhs.max == rhs.max;
547 }
548
557 template<typename T>
558 inline
559 bool operator!=(const Rect<T>& lhs, const Rect<T>& rhs) {
560 return !(lhs == rhs);
561 }
562
570 template<typename T>
571 constexpr
572 Rect<T> computeBoxQuarter(const Rect<T>& other, Quarter quarter) {
573 Vector<T, 2> size = (other.max - other.min) / 2;
574
575 switch (quarter) {
577 return Rect<T>::fromMinMax(other.min, other.max - size);
579 return Rect<T>::fromMinMax({ other.min.x + size.width, other.min.y }, { other.max.x, other.max.y - size.height });
581 return Rect<T>::fromMinMax(other.min + size, other.max);
583 return Rect<T>::fromMinMax({ other.min.x, other.min.y + size.height }, { other.max.x - size.width, other.max.y });
584 }
585
586 return other;
587 }
588
597 template<typename Archive, typename T>
598 Archive& operator|(Archive& ar, Rect<T>& rect) {
599 return ar | rect.min.x | rect.min.y | rect.max.x | rect.max.y;
600 }
601
602
603#ifndef DOXYGEN_SHOULD_SKIP_THIS
604}
605#endif
606}
607
608#endif // GF_RECT_H
constexpr Rect< T > computeBoxQuarter(const Rect< T > &other, Quarter quarter)
Divide a rectangle in quarters.
Definition: Rect.h:572
constexpr ZeroType Zero
Constant to represent "zero".
Definition: Types.h:93
Quarter
A quarter in a square.
Definition: Quarter.h:33
@ UpperRight
Upper-right quarter.
Definition: Quarter.h:35
@ LowerLeft
Lower-left quarter.
Definition: Quarter.h:37
@ UpperLeft
Upper-left quarter.
Definition: Quarter.h:34
@ LowerRight
Lower-right quarter.
Definition: Quarter.h:36
Anchor
An anchor of a box.
Definition: Anchor.h:38
@ TopRight
Top-right.
@ Center
Center.
@ BottomRight
Bottom-right.
@ TopCenter
Top-center.
@ BottomLeft
Bottom-left.
@ CenterLeft
Center-left.
@ CenterRight
Center-right.
@ TopLeft
Top-left.
@ BottomCenter
Bottom-center.
The namespace for gf classes.
Utility class for manipulating 2D axis aligned rectangles.
Definition: Rect.h:72
constexpr Vector< T, 2 > getTopLeft() const noexcept
Get the top left corner.
Definition: Rect.h:427
constexpr Rect shrink(T value) const noexcept
Shrink the rectangle.
Definition: Rect.h:379
constexpr void extend(Vector< T, 2 > point) noexcept
Extend the rectangle with a point.
Definition: Rect.h:304
constexpr Vector< T, 2 > getCenter() const noexcept
Get the center of the rectangle.
Definition: Rect.h:172
constexpr Vector< T, 2 > getTopRight() const noexcept
Get the top right corner.
Definition: Rect.h:436
constexpr T getIntersectionVolume(const Rect &other) const noexcept
Get the volume of the intersection.
Definition: Rect.h:245
bool operator==(const Rect< T > &lhs, const Rect< T > &rhs)
Equality operator.
Definition: Rect.h:545
static constexpr Rect< T > fromSize(Vector< T, 2 > size) noexcept
Create a rectangle from a size.
Definition: Rect.h:114
constexpr bool intersects(const Rect &other, Rect &result) const noexcept
Check if two rectangles interset and get the intersetion rectangle.
Definition: Rect.h:213
static constexpr Rect< T > fromPositionSize(Vector< T, 2 > position, Vector< T, 2 > size) noexcept
Create a rectangle from a position (top-left) and a size.
Definition: Rect.h:102
Archive & operator|(Archive &ar, Rect< T > &rect)
Serialize a rectangle.
Definition: Rect.h:598
bool operator!=(const Rect< T > &lhs, const Rect< T > &rhs)
Inequality operator.
Definition: Rect.h:559
constexpr bool contains(Vector< T, 2 > point) const noexcept
Check if a point is inside the rectangle.
Definition: Rect.h:182
constexpr Vector< T, 2 > getBottomRight() const noexcept
Get the bottom right corner.
Definition: Rect.h:454
constexpr Vector< T, 2 > getPositionFromAnchor(Anchor anchor) const noexcept
Get a position from the rectangle and an anchor.
Definition: Rect.h:397
Vector< T, 2 > max
The maximum point of the rectangle.
Definition: Rect.h:477
constexpr Rect() noexcept
Default constructor.
Definition: Rect.h:78
constexpr T getVolume() const noexcept
Get the volume of the rectangle.
Definition: Rect.h:339
constexpr Rect getIntersection(const Rect &other) const noexcept
Compute the intersection of two rectangles.
Definition: Rect.h:228
constexpr T getMinimumEdge() const noexcept
Get the minimum edge of the rectangle.
Definition: Rect.h:357
constexpr bool intersects(const Rect &other) const noexcept
Check if two rectangles interset.
Definition: Rect.h:202
constexpr T getIntersectionExtentLength(const Rect &other) const noexcept
Get the extent length of the intersection.
Definition: Rect.h:269
constexpr void extend(const T(&point)[2]) noexcept
Extend the rectangle with a point (as array)
Definition: Rect.h:292
constexpr void normalize() noexcept
Ensures that min coordinates are less than max coordinates.
Definition: Rect.h:386
static constexpr Rect< T > fromMinMax(Vector< T, 2 > min, Vector< T, 2 > max) noexcept
Create a rectangle from a min point and a max point.
Definition: Rect.h:91
constexpr T getExtentLength() const noexcept
Get the extent length of the rectangle.
Definition: Rect.h:348
constexpr T getHeight() const noexcept
Get the height of the rectangle.
Definition: Rect.h:472
static constexpr Rect< T > fromCenterSize(Vector< T, 2 > center, Vector< T, 2 > size) noexcept
Create a rectangle from a center and a size.
Definition: Rect.h:125
constexpr bool contains(const Rect &other) const noexcept
Check if a rectangle is totally inside the rectangle.
Definition: Rect.h:192
constexpr Rect grow(T value) const noexcept
Grow the rectangle.
Definition: Rect.h:368
constexpr Vector< T, 2 > getBottomLeft() const noexcept
Get the bottom left corner.
Definition: Rect.h:445
constexpr bool isEmpty() const noexcept
Check if the rectangle is empty.
Definition: Rect.h:163
constexpr Rect getExtended(const Rect &other) const noexcept
Get the rectangle extended by another rectangle.
Definition: Rect.h:328
Vector< T, 2 > min
The minimum point of the rectangle.
Definition: Rect.h:476
constexpr T getWidth() const noexcept
Get the width of the rectangle.
Definition: Rect.h:463
static constexpr Rect< T > empty() noexcept
Create an empty rectangle.
Definition: Rect.h:134
constexpr Vector< T, 2 > getPosition() const noexcept
Get the position of the rectangle.
Definition: Rect.h:145
constexpr Vector< T, 2 > getSize() const noexcept
Get the size of the rectangle.
Definition: Rect.h:154
constexpr void extend(const Rect &other) noexcept
Extend the rectangle with a rectangle.
Definition: Rect.h:316
A 2D vector.
Definition: Vector.h:316
T height
Second coordinate in the size representation.
Definition: Vector.h:528
T width
First coordinate in the size representation.
Definition: Vector.h:517
T y
Second coordinate in the (x,y) representation.
Definition: Vector.h:525
T x
First coordinate in the (x,y) representation.
Definition: Vector.h:514