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 | #include "Math/BsVector2.h" |
7 | |
8 | namespace bs |
9 | { |
10 | /** @addtogroup Math |
11 | * @{ |
12 | */ |
13 | |
14 | /** Represents a 2D rectangle using real values. Rectangle is represented with an origin in top left and width/height. */ |
15 | class BS_UTILITY_EXPORT Rect2 |
16 | { |
17 | public: |
18 | Rect2() = default; |
19 | |
20 | Rect2(float x, float y, float width, float height) |
21 | :x(x), y(y), width(width), height(height) |
22 | { } |
23 | |
24 | Rect2(const Vector2& topLeft, const Vector2& size) |
25 | :x(topLeft.x), y(topLeft.y), width(size.x), height(size.y) |
26 | { } |
27 | |
28 | float x = 0.0f; |
29 | float y = 0.0f; |
30 | float width = 0.0f; |
31 | float height = 0.0f; |
32 | |
33 | /** Returns true if the rectangle contains the provided point. */ |
34 | bool contains(const Vector2& point) const; |
35 | |
36 | /** |
37 | * Returns true if the rectangle overlaps the provided rectangle. Also returns true if the rectangles are contained |
38 | * within each other completely (no intersecting edges). |
39 | */ |
40 | bool overlaps(const Rect2& other) const; |
41 | |
42 | /** Extends this rectangle so that the provided rectangle is completely contained within it. */ |
43 | void encapsulate(const Rect2& other); |
44 | |
45 | /** Clips current rectangle so that it does not overlap the provided rectangle. */ |
46 | void clip(const Rect2& clipRect); |
47 | |
48 | /** |
49 | * Transforms the bounds by the given matrix. Resulting value is an axis aligned rectangle encompassing the |
50 | * transformed points. |
51 | * |
52 | * @note Since the resulting value is an AA rectangle of the original transformed rectangle, the bounds |
53 | * will be larger than needed. Oriented rectangle would provide a much tighter fit. |
54 | */ |
55 | void transform(const Matrix4& matrix); |
56 | |
57 | /** Center of the rectangle. */ |
58 | Vector2 getCenter() const; |
59 | |
60 | /** Extents of the rectangle (distance from center to one of the corners) */ |
61 | Vector2 getHalfSize() const; |
62 | |
63 | bool operator== (const Rect2& rhs) const |
64 | { |
65 | return x == rhs.x && y == rhs.y && width == rhs.width && height == rhs.height; |
66 | } |
67 | |
68 | bool operator!= (const Rect2& rhs) const |
69 | { |
70 | return !(*this == rhs); |
71 | } |
72 | |
73 | static const Rect2 EMPTY; |
74 | }; |
75 | |
76 | /** @} */ |
77 | |
78 | /** @cond SPECIALIZATIONS */ |
79 | BS_ALLOW_MEMCPY_SERIALIZATION(Rect2); |
80 | /** @endcond */ |
81 | } |
82 | |