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