xref: /haiku/src/kits/interface/Rect.cpp (revision b30304acc8c37e678a1bf66976d15bdab103f931)
1 /*
2  * Copyright 2001-2007, 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)
84 {
85 	BRect copy(*this);
86 	copy.InsetBy(point);
87 	return copy;
88 }
89 
90 
91 BRect
92 BRect::InsetByCopy(float dx, float dy)
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)
138 {
139 	BRect copy(*this);
140 	copy.OffsetBy(point);
141 	return copy;
142 }
143 
144 
145 BRect
146 BRect::OffsetByCopy(float dx, float dy)
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)
192 {
193 	BRect copy(*this);
194 	copy.OffsetTo(point);
195 	return copy;
196 }
197 
198 
199 BRect
200 BRect::OffsetToCopy(float dx, float dy)
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