1/*
2 * Copyright 2001-2006, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Frans van Nispen
7 *		Stephan Aßmus <superstippi@gmx.de>
8 */
9
10#include "IntRect.h"
11
12#include <stdio.h>
13
14
15void
16IntRect::SetLeftTop(const IntPoint& p)
17{
18	left = p.x;
19	top = p.y;
20}
21
22
23void
24IntRect::SetRightBottom(const IntPoint& p)
25{
26	right = p.x;
27	bottom = p.y;
28}
29
30
31void
32IntRect::SetLeftBottom(const IntPoint& p)
33{
34	left = p.x;
35	bottom = p.y;
36}
37
38
39void
40IntRect::SetRightTop(const IntPoint& p)
41{
42	right = p.x;
43	top = p.y;
44}
45
46
47void
48IntRect::InsetBy(const IntPoint& point)
49{
50	 left += point.x;
51	 right -= point.x;
52	 top += point.y;
53	 bottom -= point.y;
54}
55
56
57void
58IntRect::InsetBy(int32 dx, int32 dy)
59{
60	 left += dx;
61	 right -= dx;
62	 top += dy;
63	 bottom -= dy;
64}
65
66
67IntRect&
68IntRect::InsetBySelf(const IntPoint& point)
69{
70	InsetBy(point);
71	return *this;
72}
73
74
75IntRect&
76IntRect::InsetBySelf(int32 dx, int32 dy)
77{
78	InsetBy(dx, dy);
79	return *this;
80}
81
82
83IntRect
84IntRect::InsetByCopy(const IntPoint& point)
85{
86	IntRect copy(*this);
87	copy.InsetBy(point);
88	return copy;
89}
90
91
92IntRect
93IntRect::InsetByCopy(int32 dx, int32 dy)
94{
95	IntRect copy(*this);
96	copy.InsetBy(dx, dy);
97	return copy;
98}
99
100
101void
102IntRect::OffsetBy(const IntPoint& point)
103{
104	 left += point.x;
105	 right += point.x;
106	 top += point.y;
107	 bottom += point.y;
108}
109
110
111void
112IntRect::OffsetBy(int32 dx, int32 dy)
113{
114	 left += dx;
115	 right += dx;
116	 top += dy;
117	 bottom += dy;
118}
119
120
121IntRect&
122IntRect::OffsetBySelf(const IntPoint& point)
123{
124	OffsetBy(point);
125	return *this;
126}
127
128
129IntRect&
130IntRect::OffsetBySelf(int32 dx, int32 dy)
131{
132	OffsetBy(dx, dy);
133	return *this;
134}
135
136
137IntRect
138IntRect::OffsetByCopy(const IntPoint& point)
139{
140	IntRect copy(*this);
141	copy.OffsetBy(point);
142	return copy;
143}
144
145
146IntRect
147IntRect::OffsetByCopy(int32 dx, int32 dy)
148{
149	IntRect copy(*this);
150	copy.OffsetBy(dx, dy);
151	return copy;
152}
153
154
155void
156IntRect::OffsetTo(const IntPoint& point)
157{
158	 right = (right - left) + point.x;
159	 left = point.x;
160	 bottom = (bottom - top) + point.y;
161	 top = point.y;
162}
163
164
165void
166IntRect::OffsetTo(int32 x, int32 y)
167{
168	 right = (right - left) + x;
169	 left = x;
170	 bottom = (bottom - top) + y;
171	 top=y;
172}
173
174
175IntRect&
176IntRect::OffsetToSelf(const IntPoint& point)
177{
178	OffsetTo(point);
179	return *this;
180}
181
182
183IntRect&
184IntRect::OffsetToSelf(int32 dx, int32 dy)
185{
186	OffsetTo(dx, dy);
187	return *this;
188}
189
190
191IntRect
192IntRect::OffsetToCopy(const IntPoint& point)
193{
194	IntRect copy(*this);
195	copy.OffsetTo(point);
196	return copy;
197}
198
199
200IntRect
201IntRect::OffsetToCopy(int32 dx, int32 dy)
202{
203	IntRect copy(*this);
204	copy.OffsetTo(dx, dy);
205	return copy;
206}
207
208
209void
210IntRect::PrintToStream() const
211{
212	printf("IntRect(l:%" B_PRId32 ", t:%" B_PRId32 ", r:%" B_PRId32 ", b:%"
213		B_PRId32 ")\n", left, top, right, bottom);
214}
215
216
217bool
218IntRect::operator==(const IntRect& rect) const
219{
220	 return left == rect.left && right == rect.right &&
221	 		top == rect.top && bottom == rect.bottom;
222}
223
224
225bool
226IntRect::operator!=(const IntRect& rect) const
227{
228	 return !(*this == rect);
229}
230
231
232IntRect
233IntRect::operator&(const IntRect& rect) const
234{
235	 return IntRect(max_c(left, rect.left), max_c(top, rect.top),
236	 				min_c(right, rect.right), min_c(bottom, rect.bottom));
237}
238
239
240IntRect
241IntRect::operator|(const IntRect& rect) const
242{
243	 return IntRect(min_c(left, rect.left), min_c(top, rect.top),
244	 				max_c(right, rect.right), max_c(bottom, rect.bottom));
245}
246
247
248bool
249IntRect::Intersects(const IntRect& rect) const
250{
251	if (!IsValid() || !rect.IsValid())
252		return false;
253
254	return !(rect.left > right || rect.right < left
255			|| rect.top > bottom || rect.bottom < top);
256}
257
258
259bool
260IntRect::Contains(const IntPoint& point) const
261{
262	return point.x >= left && point.x <= right
263			&& point.y >= top && point.y <= bottom;
264}
265
266
267bool
268IntRect::Contains(const IntRect& rect) const
269{
270	return rect.left >= left && rect.right <= right
271			&& rect.top >= top && rect.bottom <= bottom;
272}
273
274