Kiwano Engine v1.3.x
Rect.hpp
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/math/Vec2.hpp>
23
24namespace kiwano
25{
26namespace math
27{
28template <typename _Ty>
29struct RectT
30{
31public:
32 using ValueType = _Ty;
33
34 Vec2T<ValueType> left_top;
35 Vec2T<ValueType> right_bottom;
36
37public:
38 RectT() {}
39
40 RectT(ValueType left, ValueType top, ValueType right, ValueType bottom)
41 : left_top(left, top)
42 , right_bottom(right, bottom)
43 {
44 }
45
46 RectT(const Vec2T<ValueType>& left_top, const Vec2T<ValueType>& right_bottom)
47 : left_top(left_top)
48 , right_bottom(right_bottom)
49 {
50 }
51
52 RectT(const RectT& other)
53 : left_top(other.left_top)
54 , right_bottom(other.right_bottom)
55 {
56 }
57
58 RectT& operator=(const RectT& other)
59 {
60 left_top = other.left_top;
61 right_bottom = other.right_bottom;
62 return *this;
63 }
64
65 inline bool operator==(const RectT& rect) const
66 {
67 return (left_top == rect.left_top) && (right_bottom == rect.right_bottom);
68 }
69
70 inline void Set(ValueType left, ValueType top, ValueType right, ValueType bottom)
71 {
72 left_top = Vec2T<ValueType>{ left, top };
73 right_bottom = Vec2T<ValueType>{ right, bottom };
74 }
75
76 inline Vec2T<ValueType> GetCenter() const
77 {
78 return Vec2T<ValueType>{ (left_top.x + right_bottom.x) / 2, (left_top.y + right_bottom.y) / 2 };
79 }
80
81 inline Vec2T<ValueType> GetLeftTop() const
82 {
83 return left_top;
84 }
85
86 inline Vec2T<ValueType> GetRightBottom() const
87 {
88 return right_bottom;
89 }
90
91 inline Vec2T<ValueType> GetRightTop() const
92 {
93 return Vec2T<ValueType>{ right_bottom.x, left_top.y };
94 }
95
96 inline Vec2T<ValueType> GetLeftBottom() const
97 {
98 return Vec2T<ValueType>{ left_top.x, right_bottom.y };
99 }
100
101 inline ValueType GetLeft() const
102 {
103 return left_top.x;
104 }
105
106 inline ValueType GetTop() const
107 {
108 return left_top.y;
109 }
110
111 inline ValueType GetRight() const
112 {
113 return right_bottom.x;
114 }
115
116 inline ValueType GetBottom() const
117 {
118 return right_bottom.y;
119 }
120
121 inline ValueType GetWidth() const
122 {
123 return right_bottom.x - left_top.x;
124 }
125
126 inline ValueType GetHeight() const
127 {
128 return right_bottom.y - left_top.y;
129 }
130
131 inline Vec2T<ValueType> GetSize() const
132 {
133 return Vec2T<ValueType>{ GetWidth(), GetHeight() };
134 }
135
136 inline bool IsEmpty() const
137 {
138 return left_top.IsOrigin() && right_bottom.IsOrigin();
139 }
140
141 inline bool ContainsPoint(const Vec2T<ValueType>& point) const
142 {
143 return point.x >= left_top.x && point.x <= right_bottom.x && point.y >= left_top.y && point.y <= right_bottom.y;
144 }
145
146 inline bool Intersects(const RectT& rect) const
147 {
148 return !(right_bottom.x < rect.left_top.x || rect.right_bottom.x < left_top.x
149 || right_bottom.y < rect.left_top.y || rect.right_bottom.y < left_top.y);
150 }
151
152 static inline RectT Infinite()
153 {
154 return RectT{ -math::FLOAT_MAX, -math::FLOAT_MAX, math::FLOAT_MAX, math::FLOAT_MAX };
155 }
156};
157} // namespace math
158} // namespace kiwano
Definition: Rect.hpp:30