1/* 2 * Copyright 2001-2012, 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 14void 15BRect::SetLeftTop(const BPoint p) 16{ 17 left = p.x; 18 top = p.y; 19} 20 21 22void 23BRect::SetRightBottom(const BPoint p) 24{ 25 right = p.x; 26 bottom = p.y; 27} 28 29 30void 31BRect::SetLeftBottom(const BPoint p) 32{ 33 left = p.x; 34 bottom = p.y; 35} 36 37 38void 39BRect::SetRightTop(const BPoint p) 40{ 41 right = p.x; 42 top = p.y; 43} 44 45 46void 47BRect::InsetBy(BPoint point) 48{ 49 left += point.x; 50 right -= point.x; 51 top += point.y; 52 bottom -= point.y; 53} 54 55 56void 57BRect::InsetBy(float dx, float dy) 58{ 59 left += dx; 60 right -= dx; 61 top += dy; 62 bottom -= dy; 63} 64 65 66BRect& 67BRect::InsetBySelf(BPoint point) 68{ 69 InsetBy(point); 70 return *this; 71} 72 73 74BRect& 75BRect::InsetBySelf(float dx, float dy) 76{ 77 InsetBy(dx, dy); 78 return *this; 79} 80 81 82BRect 83BRect::InsetByCopy(BPoint point) const 84{ 85 BRect copy(*this); 86 copy.InsetBy(point); 87 return copy; 88} 89 90 91BRect 92BRect::InsetByCopy(float dx, float dy) const 93{ 94 BRect copy(*this); 95 copy.InsetBy(dx, dy); 96 return copy; 97} 98 99 100void 101BRect::OffsetBy(BPoint point) 102{ 103 left += point.x; 104 right += point.x; 105 top += point.y; 106 bottom += point.y; 107} 108 109 110void 111BRect::OffsetBy(float dx, float dy) 112{ 113 left += dx; 114 right += dx; 115 top += dy; 116 bottom += dy; 117} 118 119 120BRect& 121BRect::OffsetBySelf(BPoint point) 122{ 123 OffsetBy(point); 124 return *this; 125} 126 127 128BRect& 129BRect::OffsetBySelf(float dx, float dy) 130{ 131 OffsetBy(dx, dy); 132 return *this; 133} 134 135 136BRect 137BRect::OffsetByCopy(BPoint point) const 138{ 139 BRect copy(*this); 140 copy.OffsetBy(point); 141 return copy; 142} 143 144 145BRect 146BRect::OffsetByCopy(float dx, float dy) const 147{ 148 BRect copy(*this); 149 copy.OffsetBy(dx, dy); 150 return copy; 151} 152 153 154void 155BRect::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 164void 165BRect::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 174BRect& 175BRect::OffsetToSelf(BPoint point) 176{ 177 OffsetTo(point); 178 return *this; 179} 180 181 182BRect& 183BRect::OffsetToSelf(float dx, float dy) 184{ 185 OffsetTo(dx, dy); 186 return *this; 187} 188 189 190BRect 191BRect::OffsetToCopy(BPoint point) const 192{ 193 BRect copy(*this); 194 copy.OffsetTo(point); 195 return copy; 196} 197 198 199BRect 200BRect::OffsetToCopy(float dx, float dy) const 201{ 202 BRect copy(*this); 203 copy.OffsetTo(dx, dy); 204 return copy; 205} 206 207 208void 209BRect::PrintToStream() const 210{ 211 printf("BRect(l:%.1f, t:%.1f, r:%.1f, b:%.1f)\n", left, top, right, bottom); 212} 213 214 215bool 216BRect::operator==(BRect rect) const 217{ 218 return left == rect.left && right == rect.right && 219 top == rect.top && bottom == rect.bottom; 220} 221 222 223bool 224BRect::operator!=(BRect rect) const 225{ 226 return !(*this == rect); 227} 228 229 230BRect 231BRect::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 238BRect 239BRect::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 246bool 247BRect::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 257bool 258BRect::Contains(BPoint point) const 259{ 260 return point.x >= left && point.x <= right 261 && point.y >= top && point.y <= bottom; 262} 263 264 265bool 266BRect::Contains(BRect rect) const 267{ 268 return rect.left >= left && rect.right <= right 269 && rect.top >= top && rect.bottom <= bottom; 270} 271 272 273// #pragma mark - BeOS compatibility only 274#if __GNUC__ == 2 275 276 277extern "C" BRect 278InsetByCopy__5BRectG6BPoint(BRect* self, BPoint point) 279{ 280 BRect copy(*self); 281 copy.InsetBy(point); 282 return copy; 283} 284 285 286extern "C" BRect 287InsetByCopy__5BRectff(BRect* self, float dx, float dy) 288{ 289 BRect copy(*self); 290 copy.InsetBy(dx, dy); 291 return copy; 292} 293 294 295extern "C" BRect 296OffsetByCopy__5BRectG6BPoint(BRect* self, BPoint point) 297{ 298 BRect copy(*self); 299 copy.OffsetBy(point); 300 return copy; 301} 302 303 304extern "C" BRect 305OffsetByCopy__5BRectff(BRect* self, float dx, float dy) 306{ 307 BRect copy(*self); 308 copy.OffsetBy(dx, dy); 309 return copy; 310} 311 312 313extern "C" BRect 314OffsetToCopy__5BRectG6BPoint(BRect* self, BPoint point) 315{ 316 BRect copy(*self); 317 copy.OffsetTo(point); 318 return copy; 319} 320 321 322extern "C" BRect 323OffsetToCopy__5BRectff(BRect* self, float dx, float dy) 324{ 325 BRect copy(*self); 326 copy.OffsetTo(dx, dy); 327 return copy; 328} 329 330 331#elif __GNUC__ == 4 332// TODO: remove this when new GCC 4 packages have to be built anyway 333 334 335extern "C" BRect 336_ZN5BRect11InsetByCopyE6BPoint(BRect* self, BPoint point) 337{ 338 BRect copy(*self); 339 copy.InsetBy(point); 340 return copy; 341} 342 343 344extern "C" BRect 345_ZN5BRect11InsetByCopyEff(BRect* self, float dx, float dy) 346{ 347 BRect copy(*self); 348 copy.InsetBy(dx, dy); 349 return copy; 350} 351 352 353extern "C" BRect 354_ZN5BRect12OffsetByCopyE6BPoint(BRect* self, BPoint point) 355{ 356 BRect copy(*self); 357 copy.OffsetBy(point); 358 return copy; 359} 360 361 362extern "C" BRect 363_ZN5BRect12OffsetByCopyEff(BRect* self, float dx, float dy) 364{ 365 BRect copy(*self); 366 copy.OffsetBy(dx, dy); 367 return copy; 368} 369 370 371extern "C" BRect 372_ZN5BRect12OffsetToCopyE6BPoint(BRect* self, BPoint point) 373{ 374 BRect copy(*self); 375 copy.OffsetTo(point); 376 return copy; 377} 378 379 380extern "C" BRect 381_ZN5BRect12OffsetToCopyEff(BRect* self, float dx, float dy) 382{ 383 BRect copy(*self); 384 copy.OffsetTo(dx, dy); 385 return copy; 386} 387 388 389#endif // __GNUC__ == 4 390