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
18void
19BRect::SetLeftTop(const BPoint point)
20{
21	left = point.x;
22	top = point.y;
23}
24
25
26void
27BRect::SetRightBottom(const BPoint point)
28{
29	right = point.x;
30	bottom = point.y;
31}
32
33
34void
35BRect::SetLeftBottom(const BPoint point)
36{
37	left = point.x;
38	bottom = point.y;
39}
40
41
42void
43BRect::SetRightTop(const BPoint point)
44{
45	right = point.x;
46	top = point.y;
47}
48
49
50void
51BRect::InsetBy(BPoint point)
52{
53	left += point.x;
54	right -= point.x;
55	top += point.y;
56	bottom -= point.y;
57}
58
59
60void
61BRect::InsetBy(float dx, float dy)
62{
63	left += dx;
64	right -= dx;
65	top += dy;
66	bottom -= dy;
67}
68
69
70BRect&
71BRect::InsetBySelf(BPoint point)
72{
73	InsetBy(point);
74	return *this;
75}
76
77
78BRect&
79BRect::InsetBySelf(float dx, float dy)
80{
81	InsetBy(dx, dy);
82	return *this;
83}
84
85
86BRect
87BRect::InsetByCopy(BPoint point) const
88{
89	BRect copy(*this);
90	copy.InsetBy(point);
91	return copy;
92}
93
94
95BRect
96BRect::InsetByCopy(float dx, float dy) const
97{
98	BRect copy(*this);
99	copy.InsetBy(dx, dy);
100	return copy;
101}
102
103
104void
105BRect::OffsetBy(BPoint point)
106{
107	left += point.x;
108	right += point.x;
109	top += point.y;
110	bottom += point.y;
111}
112
113
114void
115BRect::OffsetBy(float dx, float dy)
116{
117	left += dx;
118	right += dx;
119	top += dy;
120	bottom += dy;
121}
122
123
124BRect&
125BRect::OffsetBySelf(BPoint point)
126{
127	OffsetBy(point);
128	return *this;
129}
130
131
132BRect&
133BRect::OffsetBySelf(float dx, float dy)
134{
135	OffsetBy(dx, dy);
136	return *this;
137}
138
139
140BRect
141BRect::OffsetByCopy(BPoint point) const
142{
143	BRect copy(*this);
144	copy.OffsetBy(point);
145	return copy;
146}
147
148
149BRect
150BRect::OffsetByCopy(float dx, float dy) const
151{
152	BRect copy(*this);
153	copy.OffsetBy(dx, dy);
154	return copy;
155}
156
157
158void
159BRect::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
168void
169BRect::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
178BRect&
179BRect::OffsetToSelf(BPoint point)
180{
181	OffsetTo(point);
182	return *this;
183}
184
185
186BRect&
187BRect::OffsetToSelf(float x, float y)
188{
189	OffsetTo(x, y);
190	return *this;
191}
192
193
194BRect
195BRect::OffsetToCopy(BPoint point) const
196{
197	BRect copy(*this);
198	copy.OffsetTo(point);
199	return copy;
200}
201
202
203BRect
204BRect::OffsetToCopy(float x, float y) const
205{
206	BRect copy(*this);
207	copy.OffsetTo(x, y);
208	return copy;
209}
210
211
212void
213BRect::PrintToStream() const
214{
215	printf("BRect(l:%.1f, t:%.1f, r:%.1f, b:%.1f)\n", left, top, right, bottom);
216}
217
218
219bool
220BRect::operator==(BRect other) const
221{
222	return left == other.left && right == other.right &&
223		top == other.top && bottom == other.bottom;
224}
225
226
227bool
228BRect::operator!=(BRect other) const
229{
230	return !(*this == other);
231}
232
233
234BRect
235BRect::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
242BRect
243BRect::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
250bool
251BRect::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
261bool
262BRect::Contains(BPoint point) const
263{
264	return point.x >= left && point.x <= right
265		&& point.y >= top && point.y <= bottom;
266}
267
268
269bool
270BRect::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
281extern "C" BRect
282InsetByCopy__5BRectG6BPoint(BRect* self, BPoint point)
283{
284	BRect copy(*self);
285	copy.InsetBy(point);
286	return copy;
287}
288
289
290extern "C" BRect
291InsetByCopy__5BRectff(BRect* self, float dx, float dy)
292{
293	BRect copy(*self);
294	copy.InsetBy(dx, dy);
295	return copy;
296}
297
298
299extern "C" BRect
300OffsetByCopy__5BRectG6BPoint(BRect* self, BPoint point)
301{
302	BRect copy(*self);
303	copy.OffsetBy(point);
304	return copy;
305}
306
307
308extern "C" BRect
309OffsetByCopy__5BRectff(BRect* self, float dx, float dy)
310{
311	BRect copy(*self);
312	copy.OffsetBy(dx, dy);
313	return copy;
314}
315
316
317extern "C" BRect
318OffsetToCopy__5BRectG6BPoint(BRect* self, BPoint point)
319{
320	BRect copy(*self);
321	copy.OffsetTo(point);
322	return copy;
323}
324
325
326extern "C" BRect
327OffsetToCopy__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
339extern "C" BRect
340_ZN5BRect11InsetByCopyE6BPoint(BRect* self, BPoint point)
341{
342	BRect copy(*self);
343	copy.InsetBy(point);
344	return copy;
345}
346
347
348extern "C" BRect
349_ZN5BRect11InsetByCopyEff(BRect* self, float dx, float dy)
350{
351	BRect copy(*self);
352	copy.InsetBy(dx, dy);
353	return copy;
354}
355
356
357extern "C" BRect
358_ZN5BRect12OffsetByCopyE6BPoint(BRect* self, BPoint point)
359{
360	BRect copy(*self);
361	copy.OffsetBy(point);
362	return copy;
363}
364
365
366extern "C" BRect
367_ZN5BRect12OffsetByCopyEff(BRect* self, float dx, float dy)
368{
369	BRect copy(*self);
370	copy.OffsetBy(dx, dy);
371	return copy;
372}
373
374
375extern "C" BRect
376_ZN5BRect12OffsetToCopyE6BPoint(BRect* self, BPoint point)
377{
378	BRect copy(*self);
379	copy.OffsetTo(point);
380	return copy;
381}
382
383
384extern "C" BRect
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