1 //------------------------------------------------------------------------------ 2 // Copyright (c) 2001-2002, OpenBeOS 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 bool TestLineIntersect(const BRect& r, float x1, float y1, float x2, float y2, 42 bool vertical = true); 43 44 //------------------------------------------------------------------------------ 45 void BRect::InsetBy(BPoint p) 46 { 47 left += p.x; 48 right -= p.x; 49 top += p.y; 50 bottom -= p.y; 51 } 52 //------------------------------------------------------------------------------ 53 void BRect::InsetBy(float dx, float dy) 54 { 55 left += dx; 56 right -= dx; 57 top += dy; 58 bottom -= dy; 59 } 60 //------------------------------------------------------------------------------ 61 BRect& BRect::InsetBySelf(BPoint p) 62 { 63 this->InsetBy(p); 64 return *this; 65 } 66 //------------------------------------------------------------------------------ 67 BRect& BRect::InsetBySelf(float dx, float dy) 68 { 69 this->InsetBy(dx, dy); 70 return *this; 71 } 72 //------------------------------------------------------------------------------ 73 BRect BRect::InsetByCopy(BPoint p) 74 { 75 BRect copy(*this); 76 copy.InsetBy(p); 77 return copy; 78 } 79 //------------------------------------------------------------------------------ 80 BRect BRect::InsetByCopy(float dx, float dy) 81 { 82 BRect copy(*this); 83 copy.InsetBy(dx, dy); 84 return copy; 85 } 86 //------------------------------------------------------------------------------ 87 void BRect::OffsetBy(BPoint p) 88 { 89 left += p.x; 90 right += p.x; 91 top += p.y; 92 bottom += p.y; 93 } 94 //------------------------------------------------------------------------------ 95 void BRect::OffsetBy(float dx, float dy) 96 { 97 left += dx; 98 right += dx; 99 top += dy; 100 bottom += dy; 101 } 102 //------------------------------------------------------------------------------ 103 BRect& BRect::OffsetBySelf(BPoint p) 104 { 105 this->OffsetBy(p); 106 return *this; 107 } 108 //------------------------------------------------------------------------------ 109 BRect& BRect::OffsetBySelf(float dx, float dy) 110 { 111 this->OffsetBy(dx, dy); 112 return *this; 113 } 114 //------------------------------------------------------------------------------ 115 BRect BRect::OffsetByCopy(BPoint p) 116 { 117 BRect copy(*this); 118 copy.OffsetBy(p); 119 return copy; 120 } 121 //------------------------------------------------------------------------------ 122 BRect BRect::OffsetByCopy(float dx, float dy) 123 { 124 BRect copy(*this); 125 copy.OffsetBy(dx, dy); 126 return copy; 127 } 128 //------------------------------------------------------------------------------ 129 void BRect::OffsetTo(BPoint p) 130 { 131 right = (right - left) + p.x; 132 left = p.x; 133 bottom = (bottom - top) + p.y; 134 top = p.y; 135 } 136 //------------------------------------------------------------------------------ 137 void BRect::OffsetTo(float x, float y) 138 { 139 right = (right - left) + x; 140 left = x; 141 bottom = (bottom - top) + y; 142 top=y; 143 } 144 //------------------------------------------------------------------------------ 145 BRect& BRect::OffsetToSelf(BPoint p) 146 { 147 this->OffsetTo(p); 148 return *this; 149 } 150 //------------------------------------------------------------------------------ 151 BRect& BRect::OffsetToSelf(float dx, float dy) 152 { 153 this->OffsetTo(dx, dy); 154 return *this; 155 } 156 //------------------------------------------------------------------------------ 157 BRect BRect::OffsetToCopy(BPoint p) 158 { 159 BRect copy(*this); 160 copy.OffsetTo(p); 161 return copy; 162 } 163 //------------------------------------------------------------------------------ 164 BRect BRect::OffsetToCopy(float dx, float dy) 165 { 166 BRect copy(*this); 167 copy.OffsetTo(dx, dy); 168 return copy; 169 } 170 //------------------------------------------------------------------------------ 171 void BRect::PrintToStream() const 172 { 173 printf("(l:%.1f t:%.1f r:%.1f b:%.1f)\n", left, top, right, bottom); 174 } 175 //------------------------------------------------------------------------------ 176 bool BRect::operator==(BRect r) const 177 { 178 return left == r.left && right == r.right && 179 top == r.top && bottom == r.bottom; 180 } 181 //------------------------------------------------------------------------------ 182 bool BRect::operator!=(BRect r) const 183 { 184 return !(*this == r); 185 } 186 //------------------------------------------------------------------------------ 187 BRect BRect::operator&(BRect r) const 188 { 189 return BRect(max_c(left, r.left), max_c(top, r.top), 190 min_c(right, r.right), min_c(bottom, r.bottom)); 191 } 192 //------------------------------------------------------------------------------ 193 BRect BRect::operator|(BRect r) const 194 { 195 return BRect(min_c(left, r.left), min_c(top, r.top), 196 max_c(right, r.right), max_c(bottom, r.bottom)); 197 } 198 //------------------------------------------------------------------------------ 199 bool BRect::Intersects(BRect r) const 200 { 201 return TestLineIntersect(*this, r.left, r.top, r.left, r.bottom) || 202 TestLineIntersect(*this, r.left, r.top, r.right, r.top, false) || 203 TestLineIntersect(*this, r.right, r.top, r.right, r.bottom) || 204 TestLineIntersect(*this, r.left, r.bottom, r.right, r.bottom, false); 205 } 206 //------------------------------------------------------------------------------ 207 bool BRect::Contains(BPoint p) const 208 { 209 return p.x >= left && p.x <= right && p.y >= top && p.y <= bottom; 210 } 211 //------------------------------------------------------------------------------ 212 bool BRect::Contains(BRect r) const 213 { 214 return r.left >= left && r.right <= right && 215 r.top >= top && r.bottom <= bottom; 216 } 217 //------------------------------------------------------------------------------ 218 219 220 //------------------------------------------------------------------------------ 221 bool TestLineIntersect(const BRect& r, float x1, float y1, float x2, float y2, 222 bool vertical) 223 { 224 if (vertical) 225 { 226 return (x1 >= r.left && x1 <= r.right) && 227 ((y1 >= r.top && y1 <= r.bottom) || 228 (y2 >= r.top && y2 <= r.bottom)); 229 } 230 else 231 { 232 return (y1 >= r.top && y1 <= r.bottom) && 233 ((x1 >= r.left && x1 <= r.right) || 234 (x2 >= r.left && x2 <= r.right)); 235 } 236 } 237 //------------------------------------------------------------------------------ 238 239 /* 240 * $Log $ 241 * 242 * $Id $ 243 * 244 */ 245 246