xref: /haiku/src/kits/interface/Rect.cpp (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2004, Haiku, Inc.
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		Rect.cpp
23 //	Author:			Frans van Nispen (xlr8@tref.nl)
24 //	Description:	BRect represents a rectangular area.
25 //------------------------------------------------------------------------------
26 
27 // Standard Includes -----------------------------------------------------------
28 #include <stdio.h>
29 
30 // System Includes -------------------------------------------------------------
31 #include <Rect.h>
32 
33 // Project Includes ------------------------------------------------------------
34 
35 // Local Includes --------------------------------------------------------------
36 
37 // Local Defines ---------------------------------------------------------------
38 
39 // Globals ---------------------------------------------------------------------
40 
41 
42 void
43 BRect::SetLeftTop(const BPoint p)
44 {
45 	left = p.x;
46 	top = p.y;
47 }
48 //------------------------------------------------------------------------------
49 void
50 BRect::SetRightBottom(const BPoint p)
51 {
52 	right = p.x;
53 	bottom = p.y;
54 }
55 //------------------------------------------------------------------------------
56 void
57 BRect::SetLeftBottom(const BPoint p)
58 {
59 	left = p.x;
60 	bottom = p.y;
61 }
62 //------------------------------------------------------------------------------
63 void
64 BRect::SetRightTop(const BPoint p)
65 {
66 	right = p.x;
67 	top = p.y;
68 }
69 //------------------------------------------------------------------------------
70 void
71 BRect::InsetBy(BPoint point)
72 {
73 	 left += point.x;
74 	 right -= point.x;
75 	 top += point.y;
76 	 bottom -= point.y;
77 }
78 //------------------------------------------------------------------------------
79 void
80 BRect::InsetBy(float dx, float dy)
81 {
82 	 left += dx;
83 	 right -= dx;
84 	 top += dy;
85 	 bottom -= dy;
86 }
87 //------------------------------------------------------------------------------
88 BRect&
89 BRect::InsetBySelf(BPoint point)
90 {
91 	InsetBy(point);
92 	return *this;
93 }
94 //------------------------------------------------------------------------------
95 BRect&
96 BRect::InsetBySelf(float dx, float dy)
97 {
98 	InsetBy(dx, dy);
99 	return *this;
100 }
101 //------------------------------------------------------------------------------
102 BRect
103 BRect::InsetByCopy(BPoint point)
104 {
105 	BRect copy(*this);
106 	copy.InsetBy(point);
107 	return copy;
108 }
109 //------------------------------------------------------------------------------
110 BRect
111 BRect::InsetByCopy(float dx, float dy)
112 {
113 	BRect copy(*this);
114 	copy.InsetBy(dx, dy);
115 	return copy;
116 }
117 //------------------------------------------------------------------------------
118 void
119 BRect::OffsetBy(BPoint point)
120 {
121 	 left += point.x;
122 	 right += point.x;
123 	 top += point.y;
124 	 bottom += point.y;
125 }
126 //------------------------------------------------------------------------------
127 void
128 BRect::OffsetBy(float dx, float dy)
129 {
130 	 left += dx;
131 	 right += dx;
132 	 top += dy;
133 	 bottom += dy;
134 }
135 //------------------------------------------------------------------------------
136 BRect&
137 BRect::OffsetBySelf(BPoint point)
138 {
139 	OffsetBy(point);
140 	return *this;
141 }
142 //------------------------------------------------------------------------------
143 BRect&
144 BRect::OffsetBySelf(float dx, float dy)
145 {
146 	OffsetBy(dx, dy);
147 	return *this;
148 }
149 //------------------------------------------------------------------------------
150 BRect
151 BRect::OffsetByCopy(BPoint point)
152 {
153 	BRect copy(*this);
154 	copy.OffsetBy(point);
155 	return copy;
156 }
157 //------------------------------------------------------------------------------
158 BRect
159 BRect::OffsetByCopy(float dx, float dy)
160 {
161 	BRect copy(*this);
162 	copy.OffsetBy(dx, dy);
163 	return copy;
164 }
165 //------------------------------------------------------------------------------
166 void
167 BRect::OffsetTo(BPoint point)
168 {
169 	 right = (right - left) + point.x;
170 	 left = point.x;
171 	 bottom = (bottom - top) + point.y;
172 	 top = point.y;
173 }
174 //------------------------------------------------------------------------------
175 void
176 BRect::OffsetTo(float x, float y)
177 {
178 	 right = (right - left) + x;
179 	 left = x;
180 	 bottom = (bottom - top) + y;
181 	 top=y;
182 }
183 //------------------------------------------------------------------------------
184 BRect&
185 BRect::OffsetToSelf(BPoint point)
186 {
187 	OffsetTo(point);
188 	return *this;
189 }
190 //------------------------------------------------------------------------------
191 BRect&
192 BRect::OffsetToSelf(float dx, float dy)
193 {
194 	OffsetTo(dx, dy);
195 	return *this;
196 }
197 //------------------------------------------------------------------------------
198 BRect
199 BRect::OffsetToCopy(BPoint point)
200 {
201 	BRect copy(*this);
202 	copy.OffsetTo(point);
203 	return copy;
204 }
205 //------------------------------------------------------------------------------
206 BRect
207 BRect::OffsetToCopy(float dx, float dy)
208 {
209 	BRect copy(*this);
210 	copy.OffsetTo(dx, dy);
211 	return copy;
212 }
213 //------------------------------------------------------------------------------
214 void
215 BRect::PrintToStream() const
216 {
217 	printf("BRect(l:%.1f, t:%.1f, r:%.1f, b:%.1f)\n", left, top, right, bottom);
218 }
219 //------------------------------------------------------------------------------
220 bool
221 BRect::operator==(BRect rect) const
222 {
223 	 return left == rect.left && right == rect.right &&
224 	 		top == rect.top && bottom == rect.bottom;
225 }
226 //------------------------------------------------------------------------------
227 bool
228 BRect::operator!=(BRect rect) const
229 {
230 	 return !(*this == rect);
231 }
232 //------------------------------------------------------------------------------
233 BRect
234 BRect::operator&(BRect rect) const
235 {
236 	 return BRect(max_c(left, rect.left), max_c(top, rect.top),
237 	 			  min_c(right, rect.right), min_c(bottom, rect.bottom));
238 }
239 //------------------------------------------------------------------------------
240 BRect
241 BRect::operator|(BRect rect) const
242 {
243 	 return BRect(min_c(left, rect.left), min_c(top, rect.top),
244 	 			  max_c(right, rect.right), max_c(bottom, rect.bottom));
245 }
246 //------------------------------------------------------------------------------
247 bool
248 BRect::Intersects(BRect 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 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 bool
265 BRect::Contains(BRect rect) const
266 {
267 	return rect.left >= left && rect.right <= right
268 			&& rect.top >= top && rect.bottom <= bottom;
269 }
270 //------------------------------------------------------------------------------
271 
272 
273 /*
274  * $Log $
275  *
276  * $Id  $
277  *
278  */
279 
280