Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "Rect.h"
21 :
22 : #include "maths/Size2D.h"
23 : #include "maths/Vector2D.h"
24 :
25 : #include <algorithm>
26 :
27 140 : CRect::CRect() :
28 140 : left(0.f), top(0.f), right(0.f), bottom(0.f)
29 : {
30 140 : }
31 :
32 93 : CRect::CRect(const CRect& rect) :
33 93 : left(rect.left), top(rect.top), right(rect.right), bottom(rect.bottom)
34 : {
35 93 : }
36 :
37 0 : CRect::CRect(const CVector2D& pos) :
38 0 : left(pos.X), top(pos.Y), right(pos.X), bottom(pos.Y)
39 : {
40 0 : }
41 :
42 2 : CRect::CRect(const CSize2D& size) :
43 2 : left(0.f), top(0.f), right(size.Width), bottom(size.Height)
44 : {
45 2 : }
46 :
47 0 : CRect::CRect(const CVector2D& upperleft, const CVector2D& bottomright) :
48 0 : left(upperleft.X), top(upperleft.Y), right(bottomright.X), bottom(bottomright.Y)
49 : {
50 0 : }
51 :
52 0 : CRect::CRect(const CVector2D& pos, const CSize2D& size) :
53 0 : left(pos.X), top(pos.Y), right(pos.X + size.Width), bottom(pos.Y + size.Height)
54 : {
55 0 : }
56 :
57 25 : CRect::CRect(const float l, const float t, const float r, const float b) :
58 25 : left(l), top(t), right(r), bottom(b)
59 : {
60 25 : }
61 :
62 13 : CRect& CRect::operator=(const CRect& a)
63 : {
64 13 : left = a.left;
65 13 : top = a.top;
66 13 : right = a.right;
67 13 : bottom = a.bottom;
68 13 : return *this;
69 : }
70 :
71 24 : bool CRect::operator==(const CRect &a) const
72 : {
73 48 : return (left == a.left &&
74 24 : top == a.top &&
75 48 : right == a.right &&
76 22 : bottom == a.bottom);
77 : }
78 :
79 0 : bool CRect::operator!=(const CRect& a) const
80 : {
81 0 : return !(*this == a);
82 : }
83 :
84 0 : CRect CRect::operator-() const
85 : {
86 0 : return CRect(-left, -top, -right, -bottom);
87 : }
88 :
89 0 : CRect CRect::operator+() const
90 : {
91 0 : return *this;
92 : }
93 :
94 0 : CRect CRect::operator+(const CRect& a) const
95 : {
96 0 : return CRect(left + a.left, top + a.top, right + a.right, bottom + a.bottom);
97 : }
98 :
99 0 : CRect CRect::operator+(const CVector2D& a) const
100 : {
101 0 : return CRect(left + a.X, top + a.Y, right + a.X, bottom + a.Y);
102 : }
103 :
104 0 : CRect CRect::operator+(const CSize2D& a) const
105 : {
106 0 : return CRect(left + a.Width, top + a.Height, right + a.Width, bottom + a.Height);
107 : }
108 :
109 0 : CRect CRect::operator-(const CRect& a) const
110 : {
111 0 : return CRect(left - a.left, top - a.top, right - a.right, bottom - a.bottom);
112 : }
113 :
114 0 : CRect CRect::operator-(const CVector2D& a) const
115 : {
116 0 : return CRect(left - a.X, top - a.Y, right - a.X, bottom - a.Y);
117 : }
118 :
119 0 : CRect CRect::operator-(const CSize2D& a) const
120 : {
121 0 : return CRect(left - a.Width, top - a.Height, right - a.Width, bottom - a.Height);
122 : }
123 :
124 0 : void CRect::operator+=(const CRect& a)
125 : {
126 0 : left += a.left;
127 0 : top += a.top;
128 0 : right += a.right;
129 0 : bottom += a.bottom;
130 0 : }
131 :
132 0 : void CRect::operator+=(const CVector2D& a)
133 : {
134 0 : left += a.X;
135 0 : top += a.Y;
136 0 : right += a.X;
137 0 : bottom += a.Y;
138 0 : }
139 :
140 0 : void CRect::operator+=(const CSize2D& a)
141 : {
142 0 : left += a.Width;
143 0 : top += a.Height;
144 0 : right += a.Width;
145 0 : bottom += a.Height;
146 0 : }
147 :
148 0 : void CRect::operator-=(const CRect& a)
149 : {
150 0 : left -= a.left;
151 0 : top -= a.top;
152 0 : right -= a.right;
153 0 : bottom -= a.bottom;
154 0 : }
155 :
156 0 : void CRect::operator-=(const CVector2D& a)
157 : {
158 0 : left -= a.X;
159 0 : top -= a.Y;
160 0 : right -= a.X;
161 0 : bottom -= a.Y;
162 0 : }
163 :
164 0 : void CRect::operator-=(const CSize2D& a)
165 : {
166 0 : left -= a.Width;
167 0 : top -= a.Height;
168 0 : right -= a.Width;
169 0 : bottom -= a.Height;
170 0 : }
171 :
172 0 : float CRect::GetWidth() const
173 : {
174 0 : return right-left;
175 : }
176 :
177 0 : float CRect::GetHeight() const
178 : {
179 0 : return bottom-top;
180 : }
181 :
182 0 : CSize2D CRect::GetSize() const
183 : {
184 0 : return CSize2D(right - left, bottom - top);
185 : }
186 :
187 0 : CVector2D CRect::TopLeft() const
188 : {
189 0 : return CVector2D(left, top);
190 : }
191 :
192 0 : CVector2D CRect::TopRight() const
193 : {
194 0 : return CVector2D(right, top);
195 : }
196 :
197 0 : CVector2D CRect::BottomLeft() const
198 : {
199 0 : return CVector2D(left, bottom);
200 : }
201 :
202 0 : CVector2D CRect::BottomRight() const
203 : {
204 0 : return CVector2D(right, bottom);
205 : }
206 :
207 0 : CVector2D CRect::CenterPoint() const
208 : {
209 0 : return CVector2D((left + right) / 2.f, (top + bottom) / 2.f);
210 : }
211 :
212 0 : bool CRect::PointInside(const CVector2D& point) const
213 : {
214 0 : return (point.X >= left &&
215 0 : point.X <= right &&
216 0 : point.Y >= top &&
217 0 : point.Y <= bottom);
218 : }
219 :
220 0 : CRect CRect::Scale(float x, float y) const
221 : {
222 0 : return CRect(left * x, top * y, right * x, bottom * y);
223 : }
224 :
225 0 : bool CRect::IntersectWith(const CRect& a) const
226 : {
227 0 : return left < a.right && right > a.left && top < a.bottom && bottom > a.top;
228 : }
229 :
230 0 : CRect CRect::Intersection(const CRect& a) const
231 : {
232 0 : if (!IntersectWith(a))
233 0 : return CRect();
234 :
235 0 : return CRect(std::max(left, a.left), std::max(top, a.top), std::min(right, a.right), std::min(bottom, a.bottom));
236 : }
|