xref: /haiku/src/servers/app/IntRect.cpp (revision ff3409e005096a604d862a776e470aae2089865d)
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:%" B_PRId32 ", t:%" B_PRId32 ", r:%" B_PRId32 ", b:%"
213 		B_PRId32 ")\n", left, top, right, bottom);
214 }
215 
216 
217 bool
218 IntRect::operator==(const IntRect& rect) const
219 {
220 	 return left == rect.left && right == rect.right &&
221 	 		top == rect.top && bottom == rect.bottom;
222 }
223 
224 
225 bool
226 IntRect::operator!=(const IntRect& rect) const
227 {
228 	 return !(*this == rect);
229 }
230 
231 
232 IntRect
233 IntRect::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 
240 IntRect
241 IntRect::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 
248 bool
249 IntRect::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 
259 bool
260 IntRect::Contains(const IntPoint& point) const
261 {
262 	return point.x >= left && point.x <= right
263 			&& point.y >= top && point.y <= bottom;
264 }
265 
266 
267 bool
268 IntRect::Contains(const IntRect& rect) const
269 {
270 	return rect.left >= left && rect.right <= right
271 			&& rect.top >= top && rect.bottom <= bottom;
272 }
273 
274