1/* 2 * Copyright 2001-2012, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5#ifndef _RECT_H 6#define _RECT_H 7 8 9#include <math.h> 10 11#include <Point.h> 12#include <Size.h> 13 14 15class BRect { 16public: 17 float left; 18 float top; 19 float right; 20 float bottom; 21 22 BRect(); 23 BRect(const BRect& other); 24 BRect(float left, float top, float right, 25 float bottom); 26 BRect(BPoint leftTop, BPoint rightBottom); 27 BRect(BPoint leftTop, BSize size); 28 BRect(float side); 29 30 BRect& operator=(const BRect& other); 31 void Set(float left, float top, float right, 32 float bottom); 33 34 void PrintToStream() const; 35 36 BPoint LeftTop() const; 37 BPoint RightBottom() const; 38 BPoint LeftBottom() const; 39 BPoint RightTop() const; 40 41 void SetLeftTop(const BPoint point); 42 void SetRightBottom(const BPoint point); 43 void SetLeftBottom(const BPoint point); 44 void SetRightTop(const BPoint point); 45 46 // Transformation 47 void InsetBy(BPoint inset); 48 void InsetBy(float dx, float dy); 49 void OffsetBy(BPoint delta); 50 void OffsetBy(float dx, float dy); 51 void OffsetTo(BPoint offset); 52 void OffsetTo(float x, float y); 53 54 // Expression transformations 55 BRect& InsetBySelf(BPoint inset); 56 BRect& InsetBySelf(float dx, float dy); 57 BRect InsetByCopy(BPoint inset) const; 58 BRect InsetByCopy(float dx, float dy) const; 59 BRect& OffsetBySelf(BPoint offset); 60 BRect& OffsetBySelf(float dx, float dy); 61 BRect OffsetByCopy(BPoint offset) const; 62 BRect OffsetByCopy(float dx, float dy) const; 63 BRect& OffsetToSelf(BPoint offset); 64 BRect& OffsetToSelf(float x, float y); 65 BRect OffsetToCopy(BPoint offset) const; 66 BRect OffsetToCopy(float x, float y) const; 67 68 // Comparison 69 bool operator==(BRect other) const; 70 bool operator!=(BRect other) const; 71 72 // Intersection and union 73 BRect operator&(BRect other) const; 74 BRect operator|(BRect other) const; 75 76 bool IsValid() const; 77 float Width() const; 78 int32 IntegerWidth() const; 79 float Height() const; 80 int32 IntegerHeight() const; 81 BSize Size() const; 82 83 bool Intersects(BRect rect) const; 84 bool Contains(BPoint point) const; 85 bool Contains(BRect rect) const; 86}; 87 88 89// #pragma mark - inline definitions 90 91inline BPoint 92BRect::LeftTop() const 93{ 94 return *(const BPoint*)&left; 95} 96 97 98inline BPoint 99BRect::RightBottom() const 100{ 101 return *(const BPoint*)&right; 102} 103 104 105inline BPoint 106BRect::LeftBottom() const 107{ 108 return BPoint(left, bottom); 109} 110 111 112inline BPoint 113BRect::RightTop() const 114{ 115 return BPoint(right, top); 116} 117 118 119inline 120BRect::BRect() 121 : 122 left(0), 123 top(0), 124 right(-1), 125 bottom(-1) 126{ 127} 128 129 130inline 131BRect::BRect(float left, float top, float right, float bottom) 132 : 133 left(left), 134 top(top), 135 right(right), 136 bottom(bottom) 137{ 138} 139 140 141inline 142BRect::BRect(const BRect& other) 143 : 144 left(other.left), 145 top(other.top), 146 right(other.right), 147 bottom(other.bottom) 148{ 149} 150 151 152inline 153BRect::BRect(BPoint leftTop, BPoint rightBottom) 154 : 155 left(leftTop.x), 156 top(leftTop.y), 157 right(rightBottom.x), 158 bottom(rightBottom.y) 159{ 160} 161 162 163inline 164BRect::BRect(BPoint leftTop, BSize size) 165 : 166 left(leftTop.x), 167 top(leftTop.y), 168 right(leftTop.x + size.width), 169 bottom(leftTop.y + size.height) 170{ 171} 172 173 174inline 175BRect::BRect(float side) 176 : 177 left(0), 178 top(0), 179 right(side - 1), 180 bottom(side - 1) 181{ 182} 183 184 185inline BRect& 186BRect::operator=(const BRect& other) 187{ 188 left = other.left; 189 top = other.top; 190 right = other.right; 191 bottom = other.bottom; 192 return *this; 193} 194 195 196inline void 197BRect::Set(float left, float top, float right, float bottom) 198{ 199 this->left = left; 200 this->top = top; 201 this->right = right; 202 this->bottom = bottom; 203} 204 205 206inline bool 207BRect::IsValid() const 208{ 209 return left <= right && top <= bottom; 210} 211 212 213inline int32 214BRect::IntegerWidth() const 215{ 216 return (int32)ceil(right - left); 217} 218 219 220inline float 221BRect::Width() const 222{ 223 return right - left; 224} 225 226 227inline int32 228BRect::IntegerHeight() const 229{ 230 return (int32)ceil(bottom - top); 231} 232 233 234inline float 235BRect::Height() const 236{ 237 return bottom - top; 238} 239 240 241inline BSize 242BRect::Size() const 243{ 244 return BSize(right - left, bottom - top); 245} 246 247 248#endif // _RECT_H 249