00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef SkRect_DEFINED
00018 #define SkRect_DEFINED
00019
00020 #include "SkPoint.h"
00021
00026 struct SkIRect {
00027 int32_t fLeft, fTop, fRight, fBottom;
00028
00031 bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
00032
00036 int width() const { return fRight - fLeft; }
00037
00041 int height() const { return fBottom - fTop; }
00042
00043 friend int operator==(const SkIRect& a, const SkIRect& b)
00044 {
00045 return !memcmp(&a, &b, sizeof(a));
00046 }
00047 friend int operator!=(const SkIRect& a, const SkIRect& b)
00048 {
00049 return memcmp(&a, &b, sizeof(a));
00050 }
00051
00054 void setEmpty() { memset(this, 0, sizeof(*this)); }
00055
00056 void set(int32_t left, int32_t top, int32_t right, int32_t bottom)
00057 {
00058 fLeft = left;
00059 fTop = top;
00060 fRight = right;
00061 fBottom = bottom;
00062 }
00063
00067 void offset(int32_t dx, int32_t dy)
00068 {
00069 fLeft += dx;
00070 fTop += dy;
00071 fRight += dx;
00072 fBottom += dy;
00073 }
00074
00079 void inset(int32_t dx, int32_t dy)
00080 {
00081 fLeft += dx;
00082 fTop += dy;
00083 fRight -= dx;
00084 fBottom -= dy;
00085 }
00091 bool contains(int32_t x, int32_t y) const
00092 {
00093 return (unsigned)(x - fLeft) < (unsigned)(fRight - fLeft) &&
00094 (unsigned)(y - fTop) < (unsigned)(fBottom - fTop);
00095 }
00096
00100 bool contains(int32_t left, int32_t top, int32_t right, int32_t bottom) const
00101 {
00102 return left < right && top < bottom && !this->isEmpty() &&
00103 fLeft <= left && fTop <= top &&
00104 fRight >= right && fBottom >= bottom;
00105 }
00106
00109 bool contains(const SkIRect& r) const
00110 {
00111 return !r.isEmpty() && !this->isEmpty() &&
00112 fLeft <= r.fLeft && fTop <= r.fTop &&
00113 fRight >= r.fRight && fBottom >= r.fBottom;
00114 }
00115
00122 bool containsNoEmptyCheck(int32_t left, int32_t top,
00123 int32_t right, int32_t bottom) const
00124 {
00125 SkASSERT(fLeft < fRight && fTop < fBottom);
00126 SkASSERT(left < right && top < bottom);
00127
00128 return fLeft <= left && fTop <= top &&
00129 fRight >= right && fBottom >= bottom;
00130 }
00131
00136 bool intersect(const SkIRect& r)
00137 {
00138 SkASSERT(&r);
00139 return this->intersect(r.fLeft, r.fTop, r.fRight, r.fBottom);
00140 }
00141
00146 bool intersect(const SkIRect& a, const SkIRect& b)
00147 {
00148 SkASSERT(&a && &b);
00149
00150 if (!a.isEmpty() && !b.isEmpty() &&
00151 a.fLeft < b.fRight && b.fLeft < a.fRight &&
00152 a.fTop < b.fBottom && b.fTop < a.fBottom)
00153 {
00154 fLeft = SkMax32(a.fLeft, b.fLeft);
00155 fTop = SkMax32(a.fTop, b.fTop);
00156 fRight = SkMin32(a.fRight, b.fRight);
00157 fBottom = SkMin32(a.fBottom, b.fBottom);
00158 return true;
00159 }
00160 return false;
00161 }
00162
00169 bool intersectNoEmptyCheck(const SkIRect& a, const SkIRect& b)
00170 {
00171 SkASSERT(&a && &b);
00172 SkASSERT(!a.isEmpty() && !b.isEmpty());
00173
00174 if (a.fLeft < b.fRight && b.fLeft < a.fRight &&
00175 a.fTop < b.fBottom && b.fTop < a.fBottom)
00176 {
00177 fLeft = SkMax32(a.fLeft, b.fLeft);
00178 fTop = SkMax32(a.fTop, b.fTop);
00179 fRight = SkMin32(a.fRight, b.fRight);
00180 fBottom = SkMin32(a.fBottom, b.fBottom);
00181 return true;
00182 }
00183 return false;
00184 }
00185
00191 bool intersect(int32_t left, int32_t top, int32_t right, int32_t bottom)
00192 {
00193 if (left < right && top < bottom && !this->isEmpty() &&
00194 fLeft < right && left < fRight && fTop < bottom && top < fBottom)
00195 {
00196 if (fLeft < left) fLeft = left;
00197 if (fTop < top) fTop = top;
00198 if (fRight > right) fRight = right;
00199 if (fBottom > bottom) fBottom = bottom;
00200 return true;
00201 }
00202 return false;
00203 }
00204
00207 static bool Intersects(const SkIRect& a, const SkIRect& b)
00208 {
00209 return !a.isEmpty() && !b.isEmpty() &&
00210 a.fLeft < b.fRight && b.fLeft < a.fRight &&
00211 a.fTop < b.fBottom && b.fTop < a.fBottom;
00212 }
00213
00218 void join(int32_t left, int32_t top, int32_t right, int32_t bottom);
00219
00224 void join(const SkIRect& r)
00225 {
00226 this->join(r.fLeft, r.fTop, r.fRight, r.fBottom);
00227 }
00228
00234 void sort();
00235 };
00236
00239 struct SkRect {
00240 SkScalar fLeft, fTop, fRight, fBottom;
00241
00244 bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
00245 SkScalar width() const { return fRight - fLeft; }
00246 SkScalar height() const { return fBottom - fTop; }
00247 SkScalar centerX() const { return SkScalarHalf(fLeft + fRight); }
00248 SkScalar centerY() const { return SkScalarHalf(fTop + fBottom); }
00249
00250 friend int operator==(const SkRect& a, const SkRect& b)
00251 {
00252 return !memcmp(&a, &b, sizeof(a));
00253 }
00254 friend int operator!=(const SkRect& a, const SkRect& b)
00255 {
00256 return memcmp(&a, &b, sizeof(a));
00257 }
00258
00261 void toQuad(SkPoint quad[4]) const;
00262
00265 void setEmpty() { memset(this, 0, sizeof(*this)); }
00266
00267 void set(const SkIRect& src)
00268 {
00269 fLeft = SkIntToScalar(src.fLeft);
00270 fTop = SkIntToScalar(src.fTop);
00271 fRight = SkIntToScalar(src.fRight);
00272 fBottom = SkIntToScalar(src.fBottom);
00273 }
00274
00275 void set(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom)
00276 {
00277 fLeft = left;
00278 fTop = top;
00279 fRight = right;
00280 fBottom = bottom;
00281 }
00282
00286 void iset(int left, int top, int right, int bottom) {
00287 fLeft = SkIntToScalar(left);
00288 fTop = SkIntToScalar(top);
00289 fRight = SkIntToScalar(right);
00290 fBottom = SkIntToScalar(bottom);
00291 }
00292
00297 void set(const SkPoint pts[], int count);
00298
00302 void offset(SkScalar dx, SkScalar dy)
00303 {
00304 fLeft += dx;
00305 fTop += dy;
00306 fRight += dx;
00307 fBottom += dy;
00308 }
00309
00314 void inset(SkScalar dx, SkScalar dy)
00315 {
00316 fLeft += dx;
00317 fTop += dy;
00318 fRight -= dx;
00319 fBottom -= dy;
00320 }
00321
00326 bool intersect(const SkRect& r);
00327
00333 bool intersect(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom);
00334
00338 bool intersects(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) const
00339 {
00340 return
00341 left < right && top < bottom &&
00342 fLeft < fRight && fTop < fBottom &&
00343
00344 fLeft < right && left < fRight &&
00345 fTop < bottom && top < fBottom;
00346 }
00347
00350 static bool Intersects(const SkRect& a, const SkRect& b)
00351 {
00352 return !a.isEmpty() && !b.isEmpty() &&
00353 a.fLeft < b.fRight && b.fLeft < a.fRight &&
00354 a.fTop < b.fBottom && b.fTop < a.fBottom;
00355 }
00356
00361 void join(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom);
00362
00367 void join(const SkRect& r)
00368 {
00369 this->join(r.fLeft, r.fTop, r.fRight, r.fBottom);
00370 }
00371
00378 bool contains(const SkPoint& p) const
00379 {
00380 return !this->isEmpty() &&
00381 fLeft <= p.fX && p.fX < fRight &&
00382 fTop <= p.fY && p.fY < fBottom;
00383 }
00384
00391 bool contains(SkScalar x, SkScalar y) const
00392 {
00393 return !this->isEmpty() &&
00394 fLeft <= x && x < fRight &&
00395 fTop <= y && y < fBottom;
00396 }
00397
00401 bool contains(const SkRect& r) const
00402 {
00403 return !r.isEmpty() && !this->isEmpty() &&
00404 fLeft <= r.fLeft && fTop <= r.fTop &&
00405 fRight >= r.fRight && fBottom >= r.fBottom;
00406 }
00407
00411 void round(SkIRect* dst) const
00412 {
00413 SkASSERT(dst);
00414 dst->set(SkScalarRound(fLeft), SkScalarRound(fTop), SkScalarRound(fRight), SkScalarRound(fBottom));
00415 }
00416
00420 void roundOut(SkIRect* dst) const
00421 {
00422 SkASSERT(dst);
00423 dst->set(SkScalarFloor(fLeft), SkScalarFloor(fTop), SkScalarCeil(fRight), SkScalarCeil(fBottom));
00424 }
00425
00431 void sort();
00432 };
00433
00434 #endif
00435