| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #pragma once |
| 4 | |
| 5 | #include "Prerequisites/BsPrerequisitesUtil.h" |
| 6 | |
| 7 | namespace bs |
| 8 | { |
| 9 | /** @addtogroup Math |
| 10 | * @{ |
| 11 | */ |
| 12 | |
| 13 | /** Represents a 2D rectangle using integer values. Rectangle is represented with an origin in top left and width/height. */ |
| 14 | class BS_UTILITY_EXPORT Rect2I |
| 15 | { |
| 16 | public: |
| 17 | constexpr Rect2I() = default; |
| 18 | |
| 19 | constexpr Rect2I(INT32 x, INT32 y, UINT32 width, UINT32 height) |
| 20 | :x(x), y(y), width(width), height(height) |
| 21 | { } |
| 22 | |
| 23 | INT32 x = 0; |
| 24 | INT32 y = 0; |
| 25 | UINT32 width = 0; |
| 26 | UINT32 height = 0; |
| 27 | |
| 28 | /** Returns true if the rectangle contains the provided point. */ |
| 29 | bool contains(const Vector2I& point) const; |
| 30 | |
| 31 | /** |
| 32 | * Returns true if the rectangle overlaps the provided rectangle. Also returns true if the rectangles are contained |
| 33 | * within each other completely (no intersecting edges). |
| 34 | */ |
| 35 | bool overlaps(const Rect2I& other) const; |
| 36 | |
| 37 | /** Extends this rectangle so that the provided rectangle is completely contained within it. */ |
| 38 | void encapsulate(const Rect2I& other); |
| 39 | |
| 40 | /** Clips current rectangle so that it does not overlap the provided rectangle. */ |
| 41 | void clip(const Rect2I& clipRect); |
| 42 | |
| 43 | /** |
| 44 | * Cuts the current rectangle with the provided rectangle and outputs the pieces. The pieces will contain all area |
| 45 | * of the current rectangle without including the cut rectangle area. |
| 46 | */ |
| 47 | void cut(const Rect2I& cutRect, Vector<Rect2I>& pieces); |
| 48 | |
| 49 | /** |
| 50 | * Cuts the current rectangle with the provided rectangles and outputs the pieces. The pieces will contain all area |
| 51 | * of the current rectangle without including the cut rectangles area. |
| 52 | */ |
| 53 | void cut(const Vector<Rect2I>& cutRects, Vector<Rect2I>& pieces); |
| 54 | |
| 55 | /** |
| 56 | * Transforms the bounds by the given matrix. Resulting value is an axis aligned rectangle encompassing the |
| 57 | * transformed points. |
| 58 | * |
| 59 | * @note |
| 60 | * Since the resulting value is an AA rectangle of the original transformed rectangle, the bounds will be larger |
| 61 | * than needed. Oriented rectangle would provide a much tighter fit. |
| 62 | */ |
| 63 | void transform(const Matrix4& matrix); |
| 64 | |
| 65 | bool operator== (const Rect2I& rhs) const |
| 66 | { |
| 67 | return x == rhs.x && y == rhs.y && width == rhs.width && height == rhs.height; |
| 68 | } |
| 69 | |
| 70 | bool operator!= (const Rect2I& rhs) const |
| 71 | { |
| 72 | return !(*this == rhs); |
| 73 | } |
| 74 | |
| 75 | static const Rect2I EMPTY; |
| 76 | }; |
| 77 | |
| 78 | /** @} */ |
| 79 | } |
| 80 | |