xref: /haiku/src/kits/interface/Rect.cpp (revision c302a243e15e640fae0f689e32cdf0c18749afee)
1 /*
2  * Copyright 2001-2014 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Frans van Nispen
7  *		John Scipione, jscipione@gmail.com
8  */
9 
10 
11 #include <Rect.h>
12 
13 #include <algorithm>
14 
15 #include <stdio.h>
16 
17 
18 void
SetLeftTop(const BPoint point)19 BRect::SetLeftTop(const BPoint point)
20 {
21 	left = point.x;
22 	top = point.y;
23 }
24 
25 
26 void
SetRightBottom(const BPoint point)27 BRect::SetRightBottom(const BPoint point)
28 {
29 	right = point.x;
30 	bottom = point.y;
31 }
32 
33 
34 void
SetLeftBottom(const BPoint point)35 BRect::SetLeftBottom(const BPoint point)
36 {
37 	left = point.x;
38 	bottom = point.y;
39 }
40 
41 
42 void
SetRightTop(const BPoint point)43 BRect::SetRightTop(const BPoint point)
44 {
45 	right = point.x;
46 	top = point.y;
47 }
48 
49 
50 void
InsetBy(BPoint point)51 BRect::InsetBy(BPoint point)
52 {
53 	left += point.x;
54 	right -= point.x;
55 	top += point.y;
56 	bottom -= point.y;
57 }
58 
59 
60 void
InsetBy(float dx,float dy)61 BRect::InsetBy(float dx, float dy)
62 {
63 	left += dx;
64 	right -= dx;
65 	top += dy;
66 	bottom -= dy;
67 }
68 
69 
70 BRect&
InsetBySelf(BPoint point)71 BRect::InsetBySelf(BPoint point)
72 {
73 	InsetBy(point);
74 	return *this;
75 }
76 
77 
78 BRect&
InsetBySelf(float dx,float dy)79 BRect::InsetBySelf(float dx, float dy)
80 {
81 	InsetBy(dx, dy);
82 	return *this;
83 }
84 
85 
86 BRect
InsetByCopy(BPoint point) const87 BRect::InsetByCopy(BPoint point) const
88 {
89 	BRect copy(*this);
90 	copy.InsetBy(point);
91 	return copy;
92 }
93 
94 
95 BRect
InsetByCopy(float dx,float dy) const96 BRect::InsetByCopy(float dx, float dy) const
97 {
98 	BRect copy(*this);
99 	copy.InsetBy(dx, dy);
100 	return copy;
101 }
102 
103 
104 void
OffsetBy(BPoint point)105 BRect::OffsetBy(BPoint point)
106 {
107 	left += point.x;
108 	right += point.x;
109 	top += point.y;
110 	bottom += point.y;
111 }
112 
113 
114 void
OffsetBy(float dx,float dy)115 BRect::OffsetBy(float dx, float dy)
116 {
117 	left += dx;
118 	right += dx;
119 	top += dy;
120 	bottom += dy;
121 }
122 
123 
124 BRect&
OffsetBySelf(BPoint point)125 BRect::OffsetBySelf(BPoint point)
126 {
127 	OffsetBy(point);
128 	return *this;
129 }
130 
131 
132 BRect&
OffsetBySelf(float dx,float dy)133 BRect::OffsetBySelf(float dx, float dy)
134 {
135 	OffsetBy(dx, dy);
136 	return *this;
137 }
138 
139 
140 BRect
OffsetByCopy(BPoint point) const141 BRect::OffsetByCopy(BPoint point) const
142 {
143 	BRect copy(*this);
144 	copy.OffsetBy(point);
145 	return copy;
146 }
147 
148 
149 BRect
OffsetByCopy(float dx,float dy) const150 BRect::OffsetByCopy(float dx, float dy) const
151 {
152 	BRect copy(*this);
153 	copy.OffsetBy(dx, dy);
154 	return copy;
155 }
156 
157 
158 void
OffsetTo(BPoint point)159 BRect::OffsetTo(BPoint point)
160 {
161 	right = (right - left) + point.x;
162 	left = point.x;
163 	bottom = (bottom - top) + point.y;
164 	top = point.y;
165 }
166 
167 
168 void
OffsetTo(float x,float y)169 BRect::OffsetTo(float x, float y)
170 {
171 	right = (right - left) + x;
172 	left = x;
173 	bottom = (bottom - top) + y;
174 	top=y;
175 }
176 
177 
178 BRect&
OffsetToSelf(BPoint point)179 BRect::OffsetToSelf(BPoint point)
180 {
181 	OffsetTo(point);
182 	return *this;
183 }
184 
185 
186 BRect&
OffsetToSelf(float x,float y)187 BRect::OffsetToSelf(float x, float y)
188 {
189 	OffsetTo(x, y);
190 	return *this;
191 }
192 
193 
194 BRect
OffsetToCopy(BPoint point) const195 BRect::OffsetToCopy(BPoint point) const
196 {
197 	BRect copy(*this);
198 	copy.OffsetTo(point);
199 	return copy;
200 }
201 
202 
203 BRect
OffsetToCopy(float x,float y) const204 BRect::OffsetToCopy(float x, float y) const
205 {
206 	BRect copy(*this);
207 	copy.OffsetTo(x, y);
208 	return copy;
209 }
210 
211 
212 void
PrintToStream() const213 BRect::PrintToStream() const
214 {
215 	printf("BRect(l:%.1f, t:%.1f, r:%.1f, b:%.1f)\n", left, top, right, bottom);
216 }
217 
218 
219 bool
operator ==(BRect other) const220 BRect::operator==(BRect other) const
221 {
222 	return left == other.left && right == other.right &&
223 		top == other.top && bottom == other.bottom;
224 }
225 
226 
227 bool
operator !=(BRect other) const228 BRect::operator!=(BRect other) const
229 {
230 	return !(*this == other);
231 }
232 
233 
234 BRect
operator &(BRect other) const235 BRect::operator&(BRect other) const
236 {
237 	return BRect(std::max(left, other.left), std::max(top, other.top),
238 		std::min(right, other.right), std::min(bottom, other.bottom));
239 }
240 
241 
242 BRect
operator |(BRect other) const243 BRect::operator|(BRect other) const
244 {
245 	return BRect(std::min(left, other.left), std::min(top, other.top),
246 		std::max(right, other.right), std::max(bottom, other.bottom));
247 }
248 
249 
250 bool
Intersects(BRect rect) const251 BRect::Intersects(BRect rect) const
252 {
253 	if (!IsValid() || !rect.IsValid())
254 		return false;
255 
256 	return !(rect.left > right || rect.right < left
257 		|| rect.top > bottom || rect.bottom < top);
258 }
259 
260 
261 bool
Contains(BPoint point) const262 BRect::Contains(BPoint point) const
263 {
264 	return point.x >= left && point.x <= right
265 		&& point.y >= top && point.y <= bottom;
266 }
267 
268 
269 bool
Contains(BRect rect) const270 BRect::Contains(BRect rect) const
271 {
272 	return rect.left >= left && rect.right <= right
273 		&& rect.top >= top && rect.bottom <= bottom;
274 }
275 
276 
277 // #pragma mark - BeOS compatibility only
278 #if __GNUC__ == 2
279 
280 
281 extern "C" BRect
InsetByCopy__5BRectG6BPoint(BRect * self,BPoint point)282 InsetByCopy__5BRectG6BPoint(BRect* self, BPoint point)
283 {
284 	BRect copy(*self);
285 	copy.InsetBy(point);
286 	return copy;
287 }
288 
289 
290 extern "C" BRect
InsetByCopy__5BRectff(BRect * self,float dx,float dy)291 InsetByCopy__5BRectff(BRect* self, float dx, float dy)
292 {
293 	BRect copy(*self);
294 	copy.InsetBy(dx, dy);
295 	return copy;
296 }
297 
298 
299 extern "C" BRect
OffsetByCopy__5BRectG6BPoint(BRect * self,BPoint point)300 OffsetByCopy__5BRectG6BPoint(BRect* self, BPoint point)
301 {
302 	BRect copy(*self);
303 	copy.OffsetBy(point);
304 	return copy;
305 }
306 
307 
308 extern "C" BRect
OffsetByCopy__5BRectff(BRect * self,float dx,float dy)309 OffsetByCopy__5BRectff(BRect* self, float dx, float dy)
310 {
311 	BRect copy(*self);
312 	copy.OffsetBy(dx, dy);
313 	return copy;
314 }
315 
316 
317 extern "C" BRect
OffsetToCopy__5BRectG6BPoint(BRect * self,BPoint point)318 OffsetToCopy__5BRectG6BPoint(BRect* self, BPoint point)
319 {
320 	BRect copy(*self);
321 	copy.OffsetTo(point);
322 	return copy;
323 }
324 
325 
326 extern "C" BRect
OffsetToCopy__5BRectff(BRect * self,float dx,float dy)327 OffsetToCopy__5BRectff(BRect* self, float dx, float dy)
328 {
329 	BRect copy(*self);
330 	copy.OffsetTo(dx, dy);
331 	return copy;
332 }
333 
334 
335 #elif __GNUC__ >= 4
336 // TODO: remove this when new GCC 4 packages have to be built anyway
337 
338 
339 extern "C" BRect
_ZN5BRect11InsetByCopyE6BPoint(BRect * self,BPoint point)340 _ZN5BRect11InsetByCopyE6BPoint(BRect* self, BPoint point)
341 {
342 	BRect copy(*self);
343 	copy.InsetBy(point);
344 	return copy;
345 }
346 
347 
348 extern "C" BRect
_ZN5BRect11InsetByCopyEff(BRect * self,float dx,float dy)349 _ZN5BRect11InsetByCopyEff(BRect* self, float dx, float dy)
350 {
351 	BRect copy(*self);
352 	copy.InsetBy(dx, dy);
353 	return copy;
354 }
355 
356 
357 extern "C" BRect
_ZN5BRect12OffsetByCopyE6BPoint(BRect * self,BPoint point)358 _ZN5BRect12OffsetByCopyE6BPoint(BRect* self, BPoint point)
359 {
360 	BRect copy(*self);
361 	copy.OffsetBy(point);
362 	return copy;
363 }
364 
365 
366 extern "C" BRect
_ZN5BRect12OffsetByCopyEff(BRect * self,float dx,float dy)367 _ZN5BRect12OffsetByCopyEff(BRect* self, float dx, float dy)
368 {
369 	BRect copy(*self);
370 	copy.OffsetBy(dx, dy);
371 	return copy;
372 }
373 
374 
375 extern "C" BRect
_ZN5BRect12OffsetToCopyE6BPoint(BRect * self,BPoint point)376 _ZN5BRect12OffsetToCopyE6BPoint(BRect* self, BPoint point)
377 {
378 	BRect copy(*self);
379 	copy.OffsetTo(point);
380 	return copy;
381 }
382 
383 
384 extern "C" BRect
_ZN5BRect12OffsetToCopyEff(BRect * self,float dx,float dy)385 _ZN5BRect12OffsetToCopyEff(BRect* self, float dx, float dy)
386 {
387 	BRect copy(*self);
388 	copy.OffsetTo(dx, dy);
389 	return copy;
390 }
391 
392 
393 #endif	// __GNUC__ >= 4
394