SkRect.h 31.3 KB
Newer Older
reed@android.com's avatar
reed@android.com committed
1
/*
2
 * Copyright 2006 The Android Open Source Project
reed@android.com's avatar
reed@android.com committed
3
 *
4 5
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
reed@android.com's avatar
reed@android.com committed
6 7 8 9 10 11
 */

#ifndef SkRect_DEFINED
#define SkRect_DEFINED

#include "SkPoint.h"
12
#include "SkSize.h"
reed@android.com's avatar
reed@android.com committed
13

joshualitt's avatar
joshualitt committed
14 15
struct SkRect;

reed@android.com's avatar
reed@android.com committed
16 17 18 19
/** \struct SkIRect

    SkIRect holds four 32 bit integer coordinates for a rectangle
*/
20
struct SK_API SkIRect {
reed@android.com's avatar
reed@android.com committed
21 22
    int32_t fLeft, fTop, fRight, fBottom;

23
    static SkIRect SK_WARN_UNUSED_RESULT MakeEmpty() {
24 25 26 27
        SkIRect r;
        r.setEmpty();
        return r;
    }
28

29 30 31 32 33 34
    static SkIRect SK_WARN_UNUSED_RESULT MakeLargest() {
        SkIRect r;
        r.setLargest();
        return r;
    }

35
    static SkIRect SK_WARN_UNUSED_RESULT MakeWH(int32_t w, int32_t h) {
36 37 38 39
        SkIRect r;
        r.set(0, 0, w, h);
        return r;
    }
40

41
    static SkIRect SK_WARN_UNUSED_RESULT MakeSize(const SkISize& size) {
42 43 44 45
        SkIRect r;
        r.set(0, 0, size.width(), size.height());
        return r;
    }
46

47
    static SkIRect SK_WARN_UNUSED_RESULT MakeLTRB(int32_t l, int32_t t, int32_t r, int32_t b) {
48 49 50 51
        SkIRect rect;
        rect.set(l, t, r, b);
        return rect;
    }
52

53
    static SkIRect SK_WARN_UNUSED_RESULT MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h) {
54 55 56 57
        SkIRect r;
        r.set(x, y, x + w, y + h);
        return r;
    }
reed@android.com's avatar
reed@android.com committed
58

reed@google.com's avatar
reed@google.com committed
59 60 61 62
    int left() const { return fLeft; }
    int top() const { return fTop; }
    int right() const { return fRight; }
    int bottom() const { return fBottom; }
63

reed@google.com's avatar
reed@google.com committed
64 65 66 67 68 69 70 71
    /** return the left edge of the rect */
    int x() const { return fLeft; }
    /** return the top edge of the rect */
    int y() const { return fTop; }
    /**
     *  Returns the rectangle's width. This does not check for a valid rect
     *  (i.e. left <= right) so the result may be negative.
     */
reed@android.com's avatar
reed@android.com committed
72
    int width() const { return fRight - fLeft; }
73

reed@google.com's avatar
reed@google.com committed
74 75 76 77
    /**
     *  Returns the rectangle's height. This does not check for a valid rect
     *  (i.e. top <= bottom) so the result may be negative.
     */
reed@android.com's avatar
reed@android.com committed
78
    int height() const { return fBottom - fTop; }
79

80 81
    SkISize size() const { return SkISize::Make(this->width(), this->height()); }

82 83 84 85 86 87 88 89
    /**
     *  Since the center of an integer rect may fall on a factional value, this
     *  method is defined to return (right + left) >> 1.
     *
     *  This is a specific "truncation" of the average, which is different than
     *  (right + left) / 2 when the sum is negative.
     */
    int centerX() const { return (fRight + fLeft) >> 1; }
90

91 92 93 94 95 96 97 98
    /**
     *  Since the center of an integer rect may fall on a factional value, this
     *  method is defined to return (bottom + top) >> 1
     *
     *  This is a specific "truncation" of the average, which is different than
     *  (bottom + top) / 2 when the sum is negative.
     */
    int centerY() const { return (fBottom + fTop) >> 1; }
99

reed@google.com's avatar
reed@google.com committed
100 101 102 103
    /**
     *  Return true if the rectangle's width or height are <= 0
     */
    bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
104

105 106 107 108 109
    bool isLargest() const { return SK_MinS32 == fLeft &&
                                    SK_MinS32 == fTop &&
                                    SK_MaxS32 == fRight &&
                                    SK_MaxS32 == fBottom; }

110
    friend bool operator==(const SkIRect& a, const SkIRect& b) {
reed@android.com's avatar
reed@android.com committed
111 112
        return !memcmp(&a, &b, sizeof(a));
    }
113

114 115
    friend bool operator!=(const SkIRect& a, const SkIRect& b) {
        return !(a == b);
reed@android.com's avatar
reed@android.com committed
116 117
    }

118 119 120 121 122
    bool is16Bit() const {
        return  SkIsS16(fLeft) && SkIsS16(fTop) &&
                SkIsS16(fRight) && SkIsS16(fBottom);
    }

reed@android.com's avatar
reed@android.com committed
123 124 125 126
    /** Set the rectangle to (0,0,0,0)
    */
    void setEmpty() { memset(this, 0, sizeof(*this)); }

127
    void set(int32_t left, int32_t top, int32_t right, int32_t bottom) {
reed@android.com's avatar
reed@android.com committed
128 129 130 131 132
        fLeft   = left;
        fTop    = top;
        fRight  = right;
        fBottom = bottom;
    }
reed@google.com's avatar
reed@google.com committed
133 134 135 136
    // alias for set(l, t, r, b)
    void setLTRB(int32_t left, int32_t top, int32_t right, int32_t bottom) {
        this->set(left, top, right, bottom);
    }
reed@android.com's avatar
reed@android.com committed
137

reed@google.com's avatar
reed@google.com committed
138 139 140 141 142 143
    void setXYWH(int32_t x, int32_t y, int32_t width, int32_t height) {
        fLeft = x;
        fTop = y;
        fRight = x + width;
        fBottom = y + height;
    }
reed@google.com's avatar
reed@google.com committed
144 145 146 147 148 149 150 151

    /**
     *  Make the largest representable rectangle
     */
    void setLargest() {
        fLeft = fTop = SK_MinS32;
        fRight = fBottom = SK_MaxS32;
    }
152

reed@google.com's avatar
reed@google.com committed
153 154 155 156 157 158 159 160
    /**
     *  Make the largest representable rectangle, but inverted (e.g. fLeft will
     *  be max 32bit and right will be min 32bit).
     */
    void setLargestInverted() {
        fLeft = fTop = SK_MaxS32;
        fRight = fBottom = SK_MinS32;
    }
161

reed's avatar
reed committed
162 163 164
    /**
     *  Return a new IRect, built as an offset of this rect.
     */
reed's avatar
reed committed
165
    SkIRect makeOffset(int32_t dx, int32_t dy) const {
reed's avatar
reed committed
166 167 168 169 170 171
        return MakeLTRB(fLeft + dx, fTop + dy, fRight + dx, fBottom + dy);
    }

    /**
     *  Return a new IRect, built as an inset of this rect.
     */
reed's avatar
reed committed
172
    SkIRect makeInset(int32_t dx, int32_t dy) const {
reed's avatar
reed committed
173 174 175
        return MakeLTRB(fLeft + dx, fTop + dy, fRight - dx, fBottom - dy);
    }

reed's avatar
reed committed
176 177 178 179 180 181 182
    /**
     *  Return a new Rect, built as an outset of this rect.
     */
    SkIRect makeOutset(int32_t dx, int32_t dy) const {
        return MakeLTRB(fLeft - dx, fTop - dy, fRight + dx, fBottom + dy);
    }

reed@android.com's avatar
reed@android.com committed
183 184 185
    /** Offset set the rectangle by adding dx to its left and right,
        and adding dy to its top and bottom.
    */
186
    void offset(int32_t dx, int32_t dy) {
reed@android.com's avatar
reed@android.com committed
187 188 189 190 191 192
        fLeft   += dx;
        fTop    += dy;
        fRight  += dx;
        fBottom += dy;
    }

193 194 195 196
    void offset(const SkIPoint& delta) {
        this->offset(delta.fX, delta.fY);
    }

197 198 199 200 201 202 203 204 205 206
    /**
     *  Offset this rect such its new x() and y() will equal newX and newY.
     */
    void offsetTo(int32_t newX, int32_t newY) {
        fRight += newX - fLeft;
        fBottom += newY - fTop;
        fLeft = newX;
        fTop = newY;
    }

reed@android.com's avatar
reed@android.com committed
207 208
    /** Inset the rectangle by (dx,dy). If dx is positive, then the sides are moved inwards,
        making the rectangle narrower. If dx is negative, then the sides are moved outwards,
209
        making the rectangle wider. The same holds true for dy and the top and bottom.
reed@android.com's avatar
reed@android.com committed
210
    */
211
    void inset(int32_t dx, int32_t dy) {
reed@android.com's avatar
reed@android.com committed
212 213 214 215 216
        fLeft   += dx;
        fTop    += dy;
        fRight  -= dx;
        fBottom -= dy;
    }
217

218 219 220 221 222 223 224
   /** Outset the rectangle by (dx,dy). If dx is positive, then the sides are
       moved outwards, making the rectangle wider. If dx is negative, then the
       sides are moved inwards, making the rectangle narrower. The same holds
       true for dy and the top and bottom.
    */
    void outset(int32_t dx, int32_t dy)  { this->inset(-dx, -dy); }

reed@google.com's avatar
reed@google.com committed
225 226 227
    bool quickReject(int l, int t, int r, int b) const {
        return l >= fRight || fLeft >= r || t >= fBottom || fTop >= b;
    }
228

reed@android.com's avatar
reed@android.com committed
229 230 231 232 233
    /** Returns true if (x,y) is inside the rectangle and the rectangle is not
        empty. The left and top are considered to be inside, while the right
        and bottom are not. Thus for the rectangle (0, 0, 5, 10), the
        points (0,0) and (0,9) are inside, while (-1,0) and (5,9) are not.
    */
234
    bool contains(int32_t x, int32_t y) const {
reed@android.com's avatar
reed@android.com committed
235 236 237 238 239 240 241
        return  (unsigned)(x - fLeft) < (unsigned)(fRight - fLeft) &&
                (unsigned)(y - fTop) < (unsigned)(fBottom - fTop);
    }

    /** Returns true if the 4 specified sides of a rectangle are inside or equal to this rectangle.
        If either rectangle is empty, contains() returns false.
    */
242
    bool contains(int32_t left, int32_t top, int32_t right, int32_t bottom) const {
reed@android.com's avatar
reed@android.com committed
243 244 245 246 247 248 249
        return  left < right && top < bottom && !this->isEmpty() && // check for empties
                fLeft <= left && fTop <= top &&
                fRight >= right && fBottom >= bottom;
    }

    /** Returns true if the specified rectangle r is inside or equal to this rectangle.
    */
250
    bool contains(const SkIRect& r) const {
reed@android.com's avatar
reed@android.com committed
251 252 253 254 255
        return  !r.isEmpty() && !this->isEmpty() &&     // check for empties
                fLeft <= r.fLeft && fTop <= r.fTop &&
                fRight >= r.fRight && fBottom >= r.fBottom;
    }

joshualitt's avatar
joshualitt committed
256 257 258 259
    /** Returns true if the specified rectangle r is inside or equal to this rectangle.
    */
    bool contains(const SkRect& r) const;

reed@android.com's avatar
reed@android.com committed
260
    /** Return true if this rectangle contains the specified rectangle.
261 262 263 264
        For speed, this method does not check if either this or the specified
        rectangles are empty, and if either is, its return value is undefined.
        In the debugging build however, we assert that both this and the
        specified rectangles are non-empty.
reed@android.com's avatar
reed@android.com committed
265 266
    */
    bool containsNoEmptyCheck(int32_t left, int32_t top,
267 268
                              int32_t right, int32_t bottom) const {
        SkASSERT(fLeft < fRight && fTop < fBottom);
reed@android.com's avatar
reed@android.com committed
269 270 271
        SkASSERT(left < right && top < bottom);

        return fLeft <= left && fTop <= top &&
272
               fRight >= right && fBottom >= bottom;
reed@android.com's avatar
reed@android.com committed
273
    }
274

275 276 277
    bool containsNoEmptyCheck(const SkIRect& r) const {
        return containsNoEmptyCheck(r.fLeft, r.fTop, r.fRight, r.fBottom);
    }
278

reed@android.com's avatar
reed@android.com committed
279 280 281 282
    /** If r intersects this rectangle, return true and set this rectangle to that
        intersection, otherwise return false and do not change this rectangle.
        If either rectangle is empty, do nothing and return false.
    */
283
    bool SK_WARN_UNUSED_RESULT intersect(const SkIRect& r) {
reed@android.com's avatar
reed@android.com committed
284 285 286 287 288 289 290 291
        SkASSERT(&r);
        return this->intersect(r.fLeft, r.fTop, r.fRight, r.fBottom);
    }

    /** If rectangles a and b intersect, return true and set this rectangle to
        that intersection, otherwise return false and do not change this
        rectangle. If either rectangle is empty, do nothing and return false.
    */
292
    bool SK_WARN_UNUSED_RESULT intersect(const SkIRect& a, const SkIRect& b) {
293

reed@android.com's avatar
reed@android.com committed
294
        if (!a.isEmpty() && !b.isEmpty() &&
295 296
                a.fLeft < b.fRight && b.fLeft < a.fRight &&
                a.fTop < b.fBottom && b.fTop < a.fBottom) {
reed@android.com's avatar
reed@android.com committed
297 298 299 300 301 302 303 304
            fLeft   = SkMax32(a.fLeft,   b.fLeft);
            fTop    = SkMax32(a.fTop,    b.fTop);
            fRight  = SkMin32(a.fRight,  b.fRight);
            fBottom = SkMin32(a.fBottom, b.fBottom);
            return true;
        }
        return false;
    }
305

reed@android.com's avatar
reed@android.com committed
306 307 308 309 310 311
    /** If rectangles a and b intersect, return true and set this rectangle to
        that intersection, otherwise return false and do not change this
        rectangle. For speed, no check to see if a or b are empty is performed.
        If either is, then the return result is undefined. In the debug build,
        we assert that both rectangles are non-empty.
    */
312
    bool SK_WARN_UNUSED_RESULT intersectNoEmptyCheck(const SkIRect& a, const SkIRect& b) {
reed@android.com's avatar
reed@android.com committed
313
        SkASSERT(!a.isEmpty() && !b.isEmpty());
314

reed@android.com's avatar
reed@android.com committed
315
        if (a.fLeft < b.fRight && b.fLeft < a.fRight &&
316
                a.fTop < b.fBottom && b.fTop < a.fBottom) {
reed@android.com's avatar
reed@android.com committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330
            fLeft   = SkMax32(a.fLeft,   b.fLeft);
            fTop    = SkMax32(a.fTop,    b.fTop);
            fRight  = SkMin32(a.fRight,  b.fRight);
            fBottom = SkMin32(a.fBottom, b.fBottom);
            return true;
        }
        return false;
    }

    /** If the rectangle specified by left,top,right,bottom intersects this rectangle,
        return true and set this rectangle to that intersection,
        otherwise return false and do not change this rectangle.
        If either rectangle is empty, do nothing and return false.
    */
331 332
    bool SK_WARN_UNUSED_RESULT intersect(int32_t left, int32_t top, 
                                         int32_t right, int32_t bottom) {
reed@android.com's avatar
reed@android.com committed
333
        if (left < right && top < bottom && !this->isEmpty() &&
334
                fLeft < right && left < fRight && fTop < bottom && top < fBottom) {
reed@android.com's avatar
reed@android.com committed
335 336 337 338 339 340 341 342
            if (fLeft < left) fLeft = left;
            if (fTop < top) fTop = top;
            if (fRight > right) fRight = right;
            if (fBottom > bottom) fBottom = bottom;
            return true;
        }
        return false;
    }
343

reed@android.com's avatar
reed@android.com committed
344
    /** Returns true if a and b are not empty, and they intersect
345
     */
346
    static bool Intersects(const SkIRect& a, const SkIRect& b) {
reed@android.com's avatar
reed@android.com committed
347
        return  !a.isEmpty() && !b.isEmpty() &&              // check for empties
348 349
                a.fLeft < b.fRight && b.fLeft < a.fRight &&
                a.fTop < b.fBottom && b.fTop < a.fBottom;
350
    }
351

352 353 354 355 356 357 358
    /**
     *  Returns true if a and b intersect. debug-asserts that neither are empty.
     */
    static bool IntersectsNoEmptyCheck(const SkIRect& a, const SkIRect& b) {
        SkASSERT(!a.isEmpty());
        SkASSERT(!b.isEmpty());
        return  a.fLeft < b.fRight && b.fLeft < a.fRight &&
reed@android.com's avatar
reed@android.com committed
359 360
                a.fTop < b.fBottom && b.fTop < a.fBottom;
    }
361

reed@android.com's avatar
reed@android.com committed
362 363 364 365 366 367 368 369 370 371
    /** Update this rectangle to enclose itself and the specified rectangle.
        If this rectangle is empty, just set it to the specified rectangle. If the specified
        rectangle is empty, do nothing.
    */
    void join(int32_t left, int32_t top, int32_t right, int32_t bottom);

    /** Update this rectangle to enclose itself and the specified rectangle.
        If this rectangle is empty, just set it to the specified rectangle. If the specified
        rectangle is empty, do nothing.
    */
372
    void join(const SkIRect& r) {
reed@android.com's avatar
reed@android.com committed
373 374 375 376 377 378 379 380 381
        this->join(r.fLeft, r.fTop, r.fRight, r.fBottom);
    }

    /** Swap top/bottom or left/right if there are flipped.
        This can be called if the edges are computed separately,
        and may have crossed over each other.
        When this returns, left <= right && top <= bottom
    */
    void sort();
reed@google.com's avatar
reed@google.com committed
382

383
    static const SkIRect& SK_WARN_UNUSED_RESULT EmptyIRect() {
384
        static const SkIRect gEmpty = { 0, 0, 0, 0 };
reed@google.com's avatar
reed@google.com committed
385 386
        return gEmpty;
    }
reed@android.com's avatar
reed@android.com committed
387 388 389 390
};

/** \struct SkRect
*/
391
struct SK_API SkRect {
reed@android.com's avatar
reed@android.com committed
392 393
    SkScalar    fLeft, fTop, fRight, fBottom;

394
    static SkRect SK_WARN_UNUSED_RESULT MakeEmpty() {
395 396 397 398 399
        SkRect r;
        r.setEmpty();
        return r;
    }

400 401 402 403 404 405
    static SkRect SK_WARN_UNUSED_RESULT MakeLargest() {
        SkRect r;
        r.setLargest();
        return r;
    }

406
    static SkRect SK_WARN_UNUSED_RESULT MakeWH(SkScalar w, SkScalar h) {
407 408 409 410 411
        SkRect r;
        r.set(0, 0, w, h);
        return r;
    }

412 413 414 415 416 417
    static SkRect SK_WARN_UNUSED_RESULT MakeIWH(int w, int h) {
        SkRect r;
        r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
        return r;
    }

418
    static SkRect SK_WARN_UNUSED_RESULT MakeSize(const SkSize& size) {
419 420 421 422
        SkRect r;
        r.set(0, 0, size.width(), size.height());
        return r;
    }
423

424
    static SkRect SK_WARN_UNUSED_RESULT MakeLTRB(SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
425 426 427 428 429
        SkRect rect;
        rect.set(l, t, r, b);
        return rect;
    }

430
    static SkRect SK_WARN_UNUSED_RESULT MakeXYWH(SkScalar x, SkScalar y, SkScalar w, SkScalar h) {
431 432
        SkRect r;
        r.set(x, y, x + w, y + h);
433 434 435
        return r;
    }

436
    SK_ATTR_DEPRECATED("use Make()")
437 438 439 440 441 442
    static SkRect SK_WARN_UNUSED_RESULT MakeFromIRect(const SkIRect& irect) {
        SkRect r;
        r.set(SkIntToScalar(irect.fLeft),
              SkIntToScalar(irect.fTop),
              SkIntToScalar(irect.fRight),
              SkIntToScalar(irect.fBottom));
443 444
        return r;
    }
445

446 447 448 449 450 451 452 453
    static SkRect SK_WARN_UNUSED_RESULT Make(const SkIRect& irect) {
        SkRect r;
        r.set(SkIntToScalar(irect.fLeft),
              SkIntToScalar(irect.fTop),
              SkIntToScalar(irect.fRight),
              SkIntToScalar(irect.fBottom));
        return r;
    }
454

455 456 457 458
    /**
     *  Return true if the rectangle's width or height are <= 0
     */
    bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
459

460 461 462 463 464
    bool isLargest() const { return SK_ScalarMin == fLeft &&
                                    SK_ScalarMin == fTop &&
                                    SK_ScalarMax == fRight &&
                                    SK_ScalarMax == fBottom; }

465 466 467 468 469 470
    /**
     *  Returns true iff all values in the rect are finite. If any are
     *  infinite or NaN (or SK_FixedNaN when SkScalar is fixed) then this
     *  returns false.
     */
    bool isFinite() const {
471 472 473 474 475
        float accum = 0;
        accum *= fLeft;
        accum *= fTop;
        accum *= fRight;
        accum *= fBottom;
476

477
        // accum is either NaN or it is finite (zero).
478
        SkASSERT(0 == accum || SkScalarIsNaN(accum));
479

480
        // value==value will be true iff value is not NaN
481
        // TODO: is it faster to say !accum or accum==accum?
482
        return !SkScalarIsNaN(accum);
483 484
    }

485 486
    SkScalar    x() const { return fLeft; }
    SkScalar    y() const { return fTop; }
reed@google.com's avatar
reed@google.com committed
487 488 489 490
    SkScalar    left() const { return fLeft; }
    SkScalar    top() const { return fTop; }
    SkScalar    right() const { return fRight; }
    SkScalar    bottom() const { return fBottom; }
reed@android.com's avatar
reed@android.com committed
491 492 493 494 495
    SkScalar    width() const { return fRight - fLeft; }
    SkScalar    height() const { return fBottom - fTop; }
    SkScalar    centerX() const { return SkScalarHalf(fLeft + fRight); }
    SkScalar    centerY() const { return SkScalarHalf(fTop + fBottom); }

496
    friend bool operator==(const SkRect& a, const SkRect& b) {
497
        return SkScalarsEqual((SkScalar*)&a, (SkScalar*)&b, 4);
reed@android.com's avatar
reed@android.com committed
498
    }
499

500
    friend bool operator!=(const SkRect& a, const SkRect& b) {
501
        return !SkScalarsEqual((SkScalar*)&a, (SkScalar*)&b, 4);
reed@android.com's avatar
reed@android.com committed
502 503
    }

504 505 506
    /** return the 4 points that enclose the rectangle (top-left, top-right, bottom-right,
        bottom-left). TODO: Consider adding param to control whether quad is CW or CCW.
     */
reed@android.com's avatar
reed@android.com committed
507 508 509 510 511 512
    void toQuad(SkPoint quad[4]) const;

    /** Set this rectangle to the empty rectangle (0,0,0,0)
    */
    void setEmpty() { memset(this, 0, sizeof(*this)); }

513
    void set(const SkIRect& src) {
reed@android.com's avatar
reed@android.com committed
514 515 516 517 518 519
        fLeft   = SkIntToScalar(src.fLeft);
        fTop    = SkIntToScalar(src.fTop);
        fRight  = SkIntToScalar(src.fRight);
        fBottom = SkIntToScalar(src.fBottom);
    }

520
    void set(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) {
reed@android.com's avatar
reed@android.com committed
521 522 523 524 525
        fLeft   = left;
        fTop    = top;
        fRight  = right;
        fBottom = bottom;
    }
reed@google.com's avatar
reed@google.com committed
526 527 528 529 530
    // alias for set(l, t, r, b)
    void setLTRB(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) {
        this->set(left, top, right, bottom);
    }

reed@android.com's avatar
reed@android.com committed
531 532 533 534 535 536 537 538 539 540
    /** Initialize the rect with the 4 specified integers. The routine handles
        converting them to scalars (by calling SkIntToScalar)
     */
    void iset(int left, int top, int right, int bottom) {
        fLeft   = SkIntToScalar(left);
        fTop    = SkIntToScalar(top);
        fRight  = SkIntToScalar(right);
        fBottom = SkIntToScalar(bottom);
    }

541 542 543 544 545 546 547 548 549
    /**
     *  Set this rectangle to be left/top at 0,0, and have the specified width
     *  and height (automatically converted to SkScalar).
     */
    void isetWH(int width, int height) {
        fLeft = fTop = 0;
        fRight = SkIntToScalar(width);
        fBottom = SkIntToScalar(height);
    }
550

reed@android.com's avatar
reed@android.com committed
551 552 553 554
    /** Set this rectangle to be the bounds of the array of points.
        If the array is empty (count == 0), then set this rectangle
        to the empty rectangle (0,0,0,0)
    */
555 556 557 558 559 560
    void set(const SkPoint pts[], int count) {
        // set() had been checking for non-finite values, so keep that behavior
        // for now. Now that we have setBoundsCheck(), we may decide to make
        // set() be simpler/faster, and not check for those.
        (void)this->setBoundsCheck(pts, count);
    }
reed@android.com's avatar
reed@android.com committed
561

reed@google.com's avatar
reed@google.com committed
562 563
    // alias for set(pts, count)
    void setBounds(const SkPoint pts[], int count) {
564
        (void)this->setBoundsCheck(pts, count);
reed@google.com's avatar
reed@google.com committed
565
    }
566

567 568 569 570 571 572
    /**
     *  Compute the bounds of the array of points, and set this rect to that
     *  bounds and return true... unless a non-finite value is encountered,
     *  in which case this rect is set to empty and false is returned.
     */
    bool setBoundsCheck(const SkPoint pts[], int count);
573

574 575 576 577 578 579 580
    void set(const SkPoint& p0, const SkPoint& p1) {
        fLeft =   SkMinScalar(p0.fX, p1.fX);
        fRight =  SkMaxScalar(p0.fX, p1.fX);
        fTop =    SkMinScalar(p0.fY, p1.fY);
        fBottom = SkMaxScalar(p0.fY, p1.fY);
    }

reed@google.com's avatar
reed@google.com committed
581 582 583 584 585 586 587
    void setXYWH(SkScalar x, SkScalar y, SkScalar width, SkScalar height) {
        fLeft = x;
        fTop = y;
        fRight = x + width;
        fBottom = y + height;
    }

588 589 590 591 592 593 594
    void setWH(SkScalar width, SkScalar height) {
        fLeft = 0;
        fTop = 0;
        fRight = width;
        fBottom = height;
    }

reed@google.com's avatar
reed@google.com committed
595 596 597 598 599 600 601
    /**
     *  Make the largest representable rectangle
     */
    void setLargest() {
        fLeft = fTop = SK_ScalarMin;
        fRight = fBottom = SK_ScalarMax;
    }
602

reed@google.com's avatar
reed@google.com committed
603 604 605 606 607 608 609 610 611
    /**
     *  Make the largest representable rectangle, but inverted (e.g. fLeft will
     *  be max and right will be min).
     */
    void setLargestInverted() {
        fLeft = fTop = SK_ScalarMax;
        fRight = fBottom = SK_ScalarMin;
    }

reed's avatar
reed committed
612 613 614 615 616 617
    /**
     *  Return a new Rect, built as an offset of this rect.
     */
    SkRect makeOffset(SkScalar dx, SkScalar dy) const {
        return MakeLTRB(fLeft + dx, fTop + dy, fRight + dx, fBottom + dy);
    }
reed's avatar
reed committed
618
    
reed's avatar
reed committed
619 620 621 622 623 624 625
    /**
     *  Return a new Rect, built as an inset of this rect.
     */
    SkRect makeInset(SkScalar dx, SkScalar dy) const {
        return MakeLTRB(fLeft + dx, fTop + dy, fRight - dx, fBottom - dy);
    }

reed's avatar
reed committed
626 627 628 629 630 631 632
    /**
     *  Return a new Rect, built as an outset of this rect.
     */
    SkRect makeOutset(SkScalar dx, SkScalar dy) const {
        return MakeLTRB(fLeft - dx, fTop - dy, fRight + dx, fBottom + dy);
    }

reed@android.com's avatar
reed@android.com committed
633 634 635
    /** Offset set the rectangle by adding dx to its left and right,
        and adding dy to its top and bottom.
    */
636
    void offset(SkScalar dx, SkScalar dy) {
reed@android.com's avatar
reed@android.com committed
637 638 639 640
        fLeft   += dx;
        fTop    += dy;
        fRight  += dx;
        fBottom += dy;
641
    }
reed@android.com's avatar
reed@android.com committed
642

643 644 645 646
    void offset(const SkPoint& delta) {
        this->offset(delta.fX, delta.fY);
    }

647 648 649 650 651 652 653 654 655
    /**
     *  Offset this rect such its new x() and y() will equal newX and newY.
     */
    void offsetTo(SkScalar newX, SkScalar newY) {
        fRight += newX - fLeft;
        fBottom += newY - fTop;
        fLeft = newX;
        fTop = newY;
    }
656

657 658 659 660
    /** Inset the rectangle by (dx,dy). If dx is positive, then the sides are
        moved inwards, making the rectangle narrower. If dx is negative, then
        the sides are moved outwards, making the rectangle wider. The same holds
         true for dy and the top and bottom.
reed@android.com's avatar
reed@android.com committed
661
    */
662
    void inset(SkScalar dx, SkScalar dy)  {
reed@android.com's avatar
reed@android.com committed
663 664 665 666 667 668
        fLeft   += dx;
        fTop    += dy;
        fRight  -= dx;
        fBottom -= dy;
    }

669 670
   /** Outset the rectangle by (dx,dy). If dx is positive, then the sides are
       moved outwards, making the rectangle wider. If dx is negative, then the
671
       sides are moved inwards, making the rectangle narrower. The same holds
672 673 674 675
       true for dy and the top and bottom.
    */
    void outset(SkScalar dx, SkScalar dy)  { this->inset(-dx, -dy); }

reed@android.com's avatar
reed@android.com committed
676 677 678 679
    /** If this rectangle intersects r, return true and set this rectangle to that
        intersection, otherwise return false and do not change this rectangle.
        If either rectangle is empty, do nothing and return false.
    */
680
    bool SK_WARN_UNUSED_RESULT intersect(const SkRect& r);
reed@android.com's avatar
reed@android.com committed
681 682 683 684 685 686

    /** If this rectangle intersects the rectangle specified by left, top, right, bottom,
        return true and set this rectangle to that intersection, otherwise return false
        and do not change this rectangle.
        If either rectangle is empty, do nothing and return false.
    */
687 688
    bool SK_WARN_UNUSED_RESULT intersect(SkScalar left, SkScalar top, 
                                         SkScalar right, SkScalar bottom);
reed@android.com's avatar
reed@android.com committed
689

690 691 692 693 694
    /**
     *  If rectangles a and b intersect, return true and set this rectangle to
     *  that intersection, otherwise return false and do not change this
     *  rectangle. If either rectangle is empty, do nothing and return false.
     */
695
    bool SK_WARN_UNUSED_RESULT intersect(const SkRect& a, const SkRect& b);
696 697 698 699 700 701 702 703 704 705 706 707 708


private:
    static bool Intersects(SkScalar al, SkScalar at, SkScalar ar, SkScalar ab,
                           SkScalar bl, SkScalar bt, SkScalar br, SkScalar bb) {
        SkScalar L = SkMaxScalar(al, bl);
        SkScalar R = SkMinScalar(ar, br);
        SkScalar T = SkMaxScalar(at, bt);
        SkScalar B = SkMinScalar(ab, bb);
        return L < R && T < B;
    }

public:
reed@google.com's avatar
reed@google.com committed
709 710 711 712
    /**
     *  Return true if this rectangle is not empty, and the specified sides of
     *  a rectangle are not empty, and they intersect.
     */
713
    bool intersects(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) const {
714
        return Intersects(fLeft, fTop, fRight, fBottom, left, top, right, bottom);
reed@android.com's avatar
reed@android.com committed
715
    }
716

reed's avatar
reed committed
717 718 719 720 721
    bool intersects(const SkRect& r) const {
        return Intersects(fLeft, fTop, fRight, fBottom,
                          r.fLeft, r.fTop, r.fRight, r.fBottom);
    }

reed@google.com's avatar
reed@google.com committed
722 723 724
    /**
     *  Return true if rectangles a and b are not empty and intersect.
     */
725
    static bool Intersects(const SkRect& a, const SkRect& b) {
726 727
        return Intersects(a.fLeft, a.fTop, a.fRight, a.fBottom,
                          b.fLeft, b.fTop, b.fRight, b.fBottom);
reed@android.com's avatar
reed@android.com committed
728
    }
729

reed@google.com's avatar
reed@google.com committed
730 731 732 733 734
    /**
     *  Update this rectangle to enclose itself and the specified rectangle.
     *  If this rectangle is empty, just set it to the specified rectangle.
     *  If the specified rectangle is empty, do nothing.
     */
reed@android.com's avatar
reed@android.com committed
735 736 737 738 739 740
    void join(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom);

    /** Update this rectangle to enclose itself and the specified rectangle.
        If this rectangle is empty, just set it to the specified rectangle. If the specified
        rectangle is empty, do nothing.
    */
741
    void join(const SkRect& r) {
reed@android.com's avatar
reed@android.com committed
742 743
        this->join(r.fLeft, r.fTop, r.fRight, r.fBottom);
    }
744 745 746 747 748 749 750 751 752 753 754 755 756

    void joinNonEmptyArg(const SkRect& r) {
        SkASSERT(!r.isEmpty());
        // if we are empty, just assign
        if (fLeft >= fRight || fTop >= fBottom) {
            *this = r;
        } else {
            fLeft   = SkMinScalar(fLeft, r.left());
            fTop    = SkMinScalar(fTop, r.top());
            fRight  = SkMaxScalar(fRight, r.right());
            fBottom = SkMaxScalar(fBottom, r.bottom());
        }
    }
reed@google.com's avatar
reed@google.com committed
757

reed@google.com's avatar
reed@google.com committed
758 759 760 761 762 763 764 765 766
    /**
     *  Grow the rect to include the specified (x,y). After this call, the
     *  following will be true: fLeft <= x <= fRight && fTop <= y <= fBottom.
     *
     *  This is close, but not quite the same contract as contains(), since
     *  contains() treats the left and top different from the right and bottom.
     *  contains(x,y) -> fLeft <= x < fRight && fTop <= y < fBottom. Also note
     *  that contains(x,y) always returns false if the rect is empty.
     */
reed@google.com's avatar
reed@google.com committed
767 768
    void growToInclude(SkScalar x, SkScalar y) {
        fLeft  = SkMinScalar(x, fLeft);
769 770
        fRight = SkMaxScalar(x, fRight);
        fTop    = SkMinScalar(y, fTop);
reed@google.com's avatar
reed@google.com committed
771 772
        fBottom = SkMaxScalar(y, fBottom);
    }
773

774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
    /** Bulk version of growToInclude */
    void growToInclude(const SkPoint pts[], int count) {
        this->growToInclude(pts, sizeof(SkPoint), count);
    }

    /** Bulk version of growToInclude with stride. */
    void growToInclude(const SkPoint pts[], size_t stride, int count) {
        SkASSERT(count >= 0);
        SkASSERT(stride >= sizeof(SkPoint));
        const SkPoint* end = (const SkPoint*)((intptr_t)pts + count * stride);
        for (; pts < end; pts = (const SkPoint*)((intptr_t)pts + stride)) {
            this->growToInclude(pts->fX, pts->fY);
        }
    }

reed@google.com's avatar
reed@google.com committed
789 790 791 792
    /**
     *  Return true if this rectangle contains r, and if both rectangles are
     *  not empty.
     */
793
    bool contains(const SkRect& r) const {
794
        // todo: can we eliminate the this->isEmpty check?
reed@google.com's avatar
reed@google.com committed
795
        return  !r.isEmpty() && !this->isEmpty() &&
reed@android.com's avatar
reed@android.com committed
796 797 798 799
                fLeft <= r.fLeft && fTop <= r.fTop &&
                fRight >= r.fRight && fBottom >= r.fBottom;
    }

800 801 802 803 804 805 806 807 808 809
    /**
     * Returns true if the specified rectangle r is inside or equal to this rectangle.
     */
    bool contains(const SkIRect& r) const {
        // todo: can we eliminate the this->isEmpty check?
        return  !r.isEmpty() && !this->isEmpty() &&
                fLeft <= SkIntToScalar(r.fLeft) && fTop <= SkIntToScalar(r.fTop) &&
                fRight >= SkIntToScalar(r.fRight) && fBottom >= SkIntToScalar(r.fBottom);
    }

reed@google.com's avatar
reed@google.com committed
810 811
    /**
     *  Set the dst rectangle by rounding this rectangle's coordinates to their
812
     *  nearest integer values using SkScalarRoundToInt.
reed@google.com's avatar
reed@google.com committed
813
     */
814
    void round(SkIRect* dst) const {
reed@android.com's avatar
reed@android.com committed
815
        SkASSERT(dst);
816 817
        dst->set(SkScalarRoundToInt(fLeft), SkScalarRoundToInt(fTop),
                 SkScalarRoundToInt(fRight), SkScalarRoundToInt(fBottom));
reed@android.com's avatar
reed@android.com committed
818 819
    }

820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
    /**
     *  Variant of round() that explicitly performs the rounding step (i.e. floor(x + 0.5)) using
     *  double instead of SkScalar (float). It does this by calling SkDScalarRoundToInt(), which
     *  may be slower than calling SkScalarRountToInt(), but gives slightly more accurate results.
     *
     *  e.g.
     *      SkScalar x = 0.49999997f;
     *      int ix = SkScalarRoundToInt(x);
     *      SkASSERT(0 == ix);  // <--- fails
     *      ix = SkDScalarRoundToInt(x);
     *      SkASSERT(0 == ix);  // <--- succeeds
     */
    void dround(SkIRect* dst) const {
        SkASSERT(dst);
        dst->set(SkDScalarRoundToInt(fLeft), SkDScalarRoundToInt(fTop),
                 SkDScalarRoundToInt(fRight), SkDScalarRoundToInt(fBottom));
    }

reed@google.com's avatar
reed@google.com committed
838 839 840 841
    /**
     *  Set the dst rectangle by rounding "out" this rectangle, choosing the
     *  SkScalarFloor of top and left, and the SkScalarCeil of right and bottom.
     */
842
    void roundOut(SkIRect* dst) const {
reed@android.com's avatar
reed@android.com committed
843
        SkASSERT(dst);
844 845
        dst->set(SkScalarFloorToInt(fLeft), SkScalarFloorToInt(fTop),
                 SkScalarCeilToInt(fRight), SkScalarCeilToInt(fBottom));
reed@android.com's avatar
reed@android.com committed
846 847
    }

reed@google.com's avatar
reed@google.com committed
848
    /**
reed's avatar
reed committed
849 850
     *  Set the dst rectangle by rounding "out" this rectangle, choosing the
     *  SkScalarFloorToScalar of top and left, and the SkScalarCeilToScalar of right and bottom.
851 852
     *
     *  It is safe for this == dst
reed@google.com's avatar
reed@google.com committed
853
     */
854 855 856 857 858
    void roundOut(SkRect* dst) const {
        dst->set(SkScalarFloorToScalar(fLeft),
                 SkScalarFloorToScalar(fTop),
                 SkScalarCeilToScalar(fRight),
                 SkScalarCeilToScalar(fBottom));
reed@google.com's avatar
reed@google.com committed
859 860
    }

861 862 863 864 865 866 867 868 869 870 871
    /**
     *  Set the dst rectangle by rounding "in" this rectangle, choosing the
     *  ceil of top and left, and the floor of right and bottom. This does *not*
     *  call sort(), so it is possible that the resulting rect is inverted...
     *  e.g. left >= right or top >= bottom. Call isEmpty() to detect that.
     */
    void roundIn(SkIRect* dst) const {
        SkASSERT(dst);
        dst->set(SkScalarCeilToInt(fLeft), SkScalarCeilToInt(fTop),
                 SkScalarFloorToInt(fRight), SkScalarFloorToInt(fBottom));
    }
872

reed's avatar
reed committed
873
    //! Returns the result of calling round(&dst)
874 875 876 877 878
    SkIRect round() const {
        SkIRect ir;
        this->round(&ir);
        return ir;
    }
reed's avatar
reed committed
879 880 881 882 883 884 885 886
    
    //! Returns the result of calling roundOut(&dst)
    SkIRect roundOut() const {
        SkIRect ir;
        this->roundOut(&ir);
        return ir;
    }
    
reed@google.com's avatar
reed@google.com committed
887 888 889 890 891 892
    /**
     *  Swap top/bottom or left/right if there are flipped (i.e. if width()
     *  or height() would have returned a negative value.) This should be called
     *  if the edges are computed separately, and may have crossed over each
     *  other. When this returns, left <= right && top <= bottom
     */
reed's avatar
reed committed
893
    void sort() {
qiankun.miao's avatar
qiankun.miao committed
894 895 896 897 898 899 900
        if (fLeft > fRight) {
            SkTSwap<SkScalar>(fLeft, fRight);
        }

        if (fTop > fBottom) {
            SkTSwap<SkScalar>(fTop, fBottom);
        }
reed's avatar
reed committed
901
    }
902

903 904 905 906
    /**
     *  cast-safe way to treat the rect as an array of (4) SkScalars.
     */
    const SkScalar* asScalars() const { return &fLeft; }
907

908 909 910
    void dump(bool asHex) const;
    void dump() const { this->dump(false); }
    void dumpHex() const { this->dump(true); }
reed@android.com's avatar
reed@android.com committed
911 912
};

joshualitt's avatar
joshualitt committed
913 914 915 916 917 918
inline bool SkIRect::contains(const SkRect& r) const {
    return  !r.isEmpty() && !this->isEmpty() &&     // check for empties
            (SkScalar)fLeft <= r.fLeft && (SkScalar)fTop <= r.fTop &&
            (SkScalar)fRight >= r.fRight && (SkScalar)fBottom >= r.fBottom;
}

reed@android.com's avatar
reed@android.com committed
919
#endif