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