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