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