michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef SkRect_DEFINED michael@0: #define SkRect_DEFINED michael@0: michael@0: #include "SkPoint.h" michael@0: #include "SkSize.h" michael@0: michael@0: /** \struct SkIRect michael@0: michael@0: SkIRect holds four 32 bit integer coordinates for a rectangle michael@0: */ michael@0: struct SK_API SkIRect { michael@0: int32_t fLeft, fTop, fRight, fBottom; michael@0: michael@0: static SkIRect SK_WARN_UNUSED_RESULT MakeEmpty() { michael@0: SkIRect r; michael@0: r.setEmpty(); michael@0: return r; michael@0: } michael@0: michael@0: static SkIRect SK_WARN_UNUSED_RESULT MakeLargest() { michael@0: SkIRect r; michael@0: r.setLargest(); michael@0: return r; michael@0: } michael@0: michael@0: static SkIRect SK_WARN_UNUSED_RESULT MakeWH(int32_t w, int32_t h) { michael@0: SkIRect r; michael@0: r.set(0, 0, w, h); michael@0: return r; michael@0: } michael@0: michael@0: static SkIRect SK_WARN_UNUSED_RESULT MakeSize(const SkISize& size) { michael@0: SkIRect r; michael@0: r.set(0, 0, size.width(), size.height()); michael@0: return r; michael@0: } michael@0: michael@0: static SkIRect SK_WARN_UNUSED_RESULT MakeLTRB(int32_t l, int32_t t, int32_t r, int32_t b) { michael@0: SkIRect rect; michael@0: rect.set(l, t, r, b); michael@0: return rect; michael@0: } michael@0: michael@0: static SkIRect SK_WARN_UNUSED_RESULT MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h) { michael@0: SkIRect r; michael@0: r.set(x, y, x + w, y + h); michael@0: return r; michael@0: } michael@0: michael@0: int left() const { return fLeft; } michael@0: int top() const { return fTop; } michael@0: int right() const { return fRight; } michael@0: int bottom() const { return fBottom; } michael@0: michael@0: /** return the left edge of the rect */ michael@0: int x() const { return fLeft; } michael@0: /** return the top edge of the rect */ michael@0: int y() const { return fTop; } michael@0: /** michael@0: * Returns the rectangle's width. This does not check for a valid rect michael@0: * (i.e. left <= right) so the result may be negative. michael@0: */ michael@0: int width() const { return fRight - fLeft; } michael@0: michael@0: /** michael@0: * Returns the rectangle's height. This does not check for a valid rect michael@0: * (i.e. top <= bottom) so the result may be negative. michael@0: */ michael@0: int height() const { return fBottom - fTop; } michael@0: michael@0: /** michael@0: * Since the center of an integer rect may fall on a factional value, this michael@0: * method is defined to return (right + left) >> 1. michael@0: * michael@0: * This is a specific "truncation" of the average, which is different than michael@0: * (right + left) / 2 when the sum is negative. michael@0: */ michael@0: int centerX() const { return (fRight + fLeft) >> 1; } michael@0: michael@0: /** michael@0: * Since the center of an integer rect may fall on a factional value, this michael@0: * method is defined to return (bottom + top) >> 1 michael@0: * michael@0: * This is a specific "truncation" of the average, which is different than michael@0: * (bottom + top) / 2 when the sum is negative. michael@0: */ michael@0: int centerY() const { return (fBottom + fTop) >> 1; } michael@0: michael@0: /** michael@0: * Return true if the rectangle's width or height are <= 0 michael@0: */ michael@0: bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; } michael@0: michael@0: bool isLargest() const { return SK_MinS32 == fLeft && michael@0: SK_MinS32 == fTop && michael@0: SK_MaxS32 == fRight && michael@0: SK_MaxS32 == fBottom; } michael@0: michael@0: friend bool operator==(const SkIRect& a, const SkIRect& b) { michael@0: return !memcmp(&a, &b, sizeof(a)); michael@0: } michael@0: michael@0: friend bool operator!=(const SkIRect& a, const SkIRect& b) { michael@0: return !(a == b); michael@0: } michael@0: michael@0: bool is16Bit() const { michael@0: return SkIsS16(fLeft) && SkIsS16(fTop) && michael@0: SkIsS16(fRight) && SkIsS16(fBottom); michael@0: } michael@0: michael@0: /** Set the rectangle to (0,0,0,0) michael@0: */ michael@0: void setEmpty() { memset(this, 0, sizeof(*this)); } michael@0: michael@0: void set(int32_t left, int32_t top, int32_t right, int32_t bottom) { michael@0: fLeft = left; michael@0: fTop = top; michael@0: fRight = right; michael@0: fBottom = bottom; michael@0: } michael@0: // alias for set(l, t, r, b) michael@0: void setLTRB(int32_t left, int32_t top, int32_t right, int32_t bottom) { michael@0: this->set(left, top, right, bottom); michael@0: } michael@0: michael@0: void setXYWH(int32_t x, int32_t y, int32_t width, int32_t height) { michael@0: fLeft = x; michael@0: fTop = y; michael@0: fRight = x + width; michael@0: fBottom = y + height; michael@0: } michael@0: michael@0: /** michael@0: * Make the largest representable rectangle michael@0: */ michael@0: void setLargest() { michael@0: fLeft = fTop = SK_MinS32; michael@0: fRight = fBottom = SK_MaxS32; michael@0: } michael@0: michael@0: /** michael@0: * Make the largest representable rectangle, but inverted (e.g. fLeft will michael@0: * be max 32bit and right will be min 32bit). michael@0: */ michael@0: void setLargestInverted() { michael@0: fLeft = fTop = SK_MaxS32; michael@0: fRight = fBottom = SK_MinS32; michael@0: } michael@0: michael@0: /** Offset set the rectangle by adding dx to its left and right, michael@0: and adding dy to its top and bottom. michael@0: */ michael@0: void offset(int32_t dx, int32_t dy) { michael@0: fLeft += dx; michael@0: fTop += dy; michael@0: fRight += dx; michael@0: fBottom += dy; michael@0: } michael@0: michael@0: void offset(const SkIPoint& delta) { michael@0: this->offset(delta.fX, delta.fY); michael@0: } michael@0: michael@0: /** michael@0: * Offset this rect such its new x() and y() will equal newX and newY. michael@0: */ michael@0: void offsetTo(int32_t newX, int32_t newY) { michael@0: fRight += newX - fLeft; michael@0: fBottom += newY - fTop; michael@0: fLeft = newX; michael@0: fTop = newY; michael@0: } michael@0: michael@0: /** Inset the rectangle by (dx,dy). If dx is positive, then the sides are moved inwards, michael@0: making the rectangle narrower. If dx is negative, then the sides are moved outwards, michael@0: making the rectangle wider. The same holds true for dy and the top and bottom. michael@0: */ michael@0: void inset(int32_t dx, int32_t dy) { michael@0: fLeft += dx; michael@0: fTop += dy; michael@0: fRight -= dx; michael@0: fBottom -= dy; michael@0: } michael@0: michael@0: /** Outset the rectangle by (dx,dy). If dx is positive, then the sides are michael@0: moved outwards, making the rectangle wider. If dx is negative, then the michael@0: sides are moved inwards, making the rectangle narrower. The same holds michael@0: true for dy and the top and bottom. michael@0: */ michael@0: void outset(int32_t dx, int32_t dy) { this->inset(-dx, -dy); } michael@0: michael@0: bool quickReject(int l, int t, int r, int b) const { michael@0: return l >= fRight || fLeft >= r || t >= fBottom || fTop >= b; michael@0: } michael@0: michael@0: /** Returns true if (x,y) is inside the rectangle and the rectangle is not michael@0: empty. The left and top are considered to be inside, while the right michael@0: and bottom are not. Thus for the rectangle (0, 0, 5, 10), the michael@0: points (0,0) and (0,9) are inside, while (-1,0) and (5,9) are not. michael@0: */ michael@0: bool contains(int32_t x, int32_t y) const { michael@0: return (unsigned)(x - fLeft) < (unsigned)(fRight - fLeft) && michael@0: (unsigned)(y - fTop) < (unsigned)(fBottom - fTop); michael@0: } michael@0: michael@0: /** Returns true if the 4 specified sides of a rectangle are inside or equal to this rectangle. michael@0: If either rectangle is empty, contains() returns false. michael@0: */ michael@0: bool contains(int32_t left, int32_t top, int32_t right, int32_t bottom) const { michael@0: return left < right && top < bottom && !this->isEmpty() && // check for empties michael@0: fLeft <= left && fTop <= top && michael@0: fRight >= right && fBottom >= bottom; michael@0: } michael@0: michael@0: /** Returns true if the specified rectangle r is inside or equal to this rectangle. michael@0: */ michael@0: bool contains(const SkIRect& r) const { michael@0: return !r.isEmpty() && !this->isEmpty() && // check for empties michael@0: fLeft <= r.fLeft && fTop <= r.fTop && michael@0: fRight >= r.fRight && fBottom >= r.fBottom; michael@0: } michael@0: michael@0: /** Return true if this rectangle contains the specified rectangle. michael@0: For speed, this method does not check if either this or the specified michael@0: rectangles are empty, and if either is, its return value is undefined. michael@0: In the debugging build however, we assert that both this and the michael@0: specified rectangles are non-empty. michael@0: */ michael@0: bool containsNoEmptyCheck(int32_t left, int32_t top, michael@0: int32_t right, int32_t bottom) const { michael@0: SkASSERT(fLeft < fRight && fTop < fBottom); michael@0: SkASSERT(left < right && top < bottom); michael@0: michael@0: return fLeft <= left && fTop <= top && michael@0: fRight >= right && fBottom >= bottom; michael@0: } michael@0: michael@0: bool containsNoEmptyCheck(const SkIRect& r) const { michael@0: return containsNoEmptyCheck(r.fLeft, r.fTop, r.fRight, r.fBottom); michael@0: } michael@0: michael@0: /** If r intersects this rectangle, return true and set this rectangle to that michael@0: intersection, otherwise return false and do not change this rectangle. michael@0: If either rectangle is empty, do nothing and return false. michael@0: */ michael@0: bool intersect(const SkIRect& r) { michael@0: SkASSERT(&r); michael@0: return this->intersect(r.fLeft, r.fTop, r.fRight, r.fBottom); michael@0: } michael@0: michael@0: /** If rectangles a and b intersect, return true and set this rectangle to michael@0: that intersection, otherwise return false and do not change this michael@0: rectangle. If either rectangle is empty, do nothing and return false. michael@0: */ michael@0: bool intersect(const SkIRect& a, const SkIRect& b) { michael@0: SkASSERT(&a && &b); michael@0: michael@0: if (!a.isEmpty() && !b.isEmpty() && michael@0: a.fLeft < b.fRight && b.fLeft < a.fRight && michael@0: a.fTop < b.fBottom && b.fTop < a.fBottom) { michael@0: fLeft = SkMax32(a.fLeft, b.fLeft); michael@0: fTop = SkMax32(a.fTop, b.fTop); michael@0: fRight = SkMin32(a.fRight, b.fRight); michael@0: fBottom = SkMin32(a.fBottom, b.fBottom); michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: /** If rectangles a and b intersect, return true and set this rectangle to michael@0: that intersection, otherwise return false and do not change this michael@0: rectangle. For speed, no check to see if a or b are empty is performed. michael@0: If either is, then the return result is undefined. In the debug build, michael@0: we assert that both rectangles are non-empty. michael@0: */ michael@0: bool intersectNoEmptyCheck(const SkIRect& a, const SkIRect& b) { michael@0: SkASSERT(&a && &b); michael@0: SkASSERT(!a.isEmpty() && !b.isEmpty()); michael@0: michael@0: if (a.fLeft < b.fRight && b.fLeft < a.fRight && michael@0: a.fTop < b.fBottom && b.fTop < a.fBottom) { michael@0: fLeft = SkMax32(a.fLeft, b.fLeft); michael@0: fTop = SkMax32(a.fTop, b.fTop); michael@0: fRight = SkMin32(a.fRight, b.fRight); michael@0: fBottom = SkMin32(a.fBottom, b.fBottom); michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: /** If the rectangle specified by left,top,right,bottom intersects this rectangle, michael@0: return true and set this rectangle to that intersection, michael@0: otherwise return false and do not change this rectangle. michael@0: If either rectangle is empty, do nothing and return false. michael@0: */ michael@0: bool intersect(int32_t left, int32_t top, int32_t right, int32_t bottom) { michael@0: if (left < right && top < bottom && !this->isEmpty() && michael@0: fLeft < right && left < fRight && fTop < bottom && top < fBottom) { michael@0: if (fLeft < left) fLeft = left; michael@0: if (fTop < top) fTop = top; michael@0: if (fRight > right) fRight = right; michael@0: if (fBottom > bottom) fBottom = bottom; michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: /** Returns true if a and b are not empty, and they intersect michael@0: */ michael@0: static bool Intersects(const SkIRect& a, const SkIRect& b) { michael@0: return !a.isEmpty() && !b.isEmpty() && // check for empties michael@0: a.fLeft < b.fRight && b.fLeft < a.fRight && michael@0: a.fTop < b.fBottom && b.fTop < a.fBottom; michael@0: } michael@0: michael@0: /** michael@0: * Returns true if a and b intersect. debug-asserts that neither are empty. michael@0: */ michael@0: static bool IntersectsNoEmptyCheck(const SkIRect& a, const SkIRect& b) { michael@0: SkASSERT(!a.isEmpty()); michael@0: SkASSERT(!b.isEmpty()); michael@0: return a.fLeft < b.fRight && b.fLeft < a.fRight && michael@0: a.fTop < b.fBottom && b.fTop < a.fBottom; michael@0: } michael@0: michael@0: /** Update this rectangle to enclose itself and the specified rectangle. michael@0: If this rectangle is empty, just set it to the specified rectangle. If the specified michael@0: rectangle is empty, do nothing. michael@0: */ michael@0: void join(int32_t left, int32_t top, int32_t right, int32_t bottom); michael@0: michael@0: /** Update this rectangle to enclose itself and the specified rectangle. michael@0: If this rectangle is empty, just set it to the specified rectangle. If the specified michael@0: rectangle is empty, do nothing. michael@0: */ michael@0: void join(const SkIRect& r) { michael@0: this->join(r.fLeft, r.fTop, r.fRight, r.fBottom); michael@0: } michael@0: michael@0: /** Swap top/bottom or left/right if there are flipped. michael@0: This can be called if the edges are computed separately, michael@0: and may have crossed over each other. michael@0: When this returns, left <= right && top <= bottom michael@0: */ michael@0: void sort(); michael@0: michael@0: static const SkIRect& SK_WARN_UNUSED_RESULT EmptyIRect() { michael@0: static const SkIRect gEmpty = { 0, 0, 0, 0 }; michael@0: return gEmpty; michael@0: } michael@0: }; michael@0: michael@0: /** \struct SkRect michael@0: */ michael@0: struct SK_API SkRect { michael@0: SkScalar fLeft, fTop, fRight, fBottom; michael@0: michael@0: static SkRect SK_WARN_UNUSED_RESULT MakeEmpty() { michael@0: SkRect r; michael@0: r.setEmpty(); michael@0: return r; michael@0: } michael@0: michael@0: static SkRect SK_WARN_UNUSED_RESULT MakeLargest() { michael@0: SkRect r; michael@0: r.setLargest(); michael@0: return r; michael@0: } michael@0: michael@0: static SkRect SK_WARN_UNUSED_RESULT MakeWH(SkScalar w, SkScalar h) { michael@0: SkRect r; michael@0: r.set(0, 0, w, h); michael@0: return r; michael@0: } michael@0: michael@0: static SkRect SK_WARN_UNUSED_RESULT MakeSize(const SkSize& size) { michael@0: SkRect r; michael@0: r.set(0, 0, size.width(), size.height()); michael@0: return r; michael@0: } michael@0: michael@0: static SkRect SK_WARN_UNUSED_RESULT MakeLTRB(SkScalar l, SkScalar t, SkScalar r, SkScalar b) { michael@0: SkRect rect; michael@0: rect.set(l, t, r, b); michael@0: return rect; michael@0: } michael@0: michael@0: static SkRect SK_WARN_UNUSED_RESULT MakeXYWH(SkScalar x, SkScalar y, SkScalar w, SkScalar h) { michael@0: SkRect r; michael@0: r.set(x, y, x + w, y + h); michael@0: return r; michael@0: } michael@0: michael@0: SK_ATTR_DEPRECATED("use Make()") michael@0: static SkRect SK_WARN_UNUSED_RESULT MakeFromIRect(const SkIRect& irect) { michael@0: SkRect r; michael@0: r.set(SkIntToScalar(irect.fLeft), michael@0: SkIntToScalar(irect.fTop), michael@0: SkIntToScalar(irect.fRight), michael@0: SkIntToScalar(irect.fBottom)); michael@0: return r; michael@0: } michael@0: michael@0: static SkRect SK_WARN_UNUSED_RESULT Make(const SkIRect& irect) { michael@0: SkRect r; michael@0: r.set(SkIntToScalar(irect.fLeft), michael@0: SkIntToScalar(irect.fTop), michael@0: SkIntToScalar(irect.fRight), michael@0: SkIntToScalar(irect.fBottom)); michael@0: return r; michael@0: } michael@0: michael@0: /** michael@0: * Return true if the rectangle's width or height are <= 0 michael@0: */ michael@0: bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; } michael@0: michael@0: bool isLargest() const { return SK_ScalarMin == fLeft && michael@0: SK_ScalarMin == fTop && michael@0: SK_ScalarMax == fRight && michael@0: SK_ScalarMax == fBottom; } michael@0: michael@0: /** michael@0: * Returns true iff all values in the rect are finite. If any are michael@0: * infinite or NaN (or SK_FixedNaN when SkScalar is fixed) then this michael@0: * returns false. michael@0: */ michael@0: bool isFinite() const { michael@0: float accum = 0; michael@0: accum *= fLeft; michael@0: accum *= fTop; michael@0: accum *= fRight; michael@0: accum *= fBottom; michael@0: michael@0: // accum is either NaN or it is finite (zero). michael@0: SkASSERT(0 == accum || !(accum == accum)); michael@0: michael@0: // value==value will be true iff value is not NaN michael@0: // TODO: is it faster to say !accum or accum==accum? michael@0: return accum == accum; michael@0: } michael@0: michael@0: SkScalar x() const { return fLeft; } michael@0: SkScalar y() const { return fTop; } michael@0: SkScalar left() const { return fLeft; } michael@0: SkScalar top() const { return fTop; } michael@0: SkScalar right() const { return fRight; } michael@0: SkScalar bottom() const { return fBottom; } michael@0: SkScalar width() const { return fRight - fLeft; } michael@0: SkScalar height() const { return fBottom - fTop; } michael@0: SkScalar centerX() const { return SkScalarHalf(fLeft + fRight); } michael@0: SkScalar centerY() const { return SkScalarHalf(fTop + fBottom); } michael@0: michael@0: friend bool operator==(const SkRect& a, const SkRect& b) { michael@0: return SkScalarsEqual((SkScalar*)&a, (SkScalar*)&b, 4); michael@0: } michael@0: michael@0: friend bool operator!=(const SkRect& a, const SkRect& b) { michael@0: return !SkScalarsEqual((SkScalar*)&a, (SkScalar*)&b, 4); michael@0: } michael@0: michael@0: /** return the 4 points that enclose the rectangle (top-left, top-right, bottom-right, michael@0: bottom-left). TODO: Consider adding param to control whether quad is CW or CCW. michael@0: */ michael@0: void toQuad(SkPoint quad[4]) const; michael@0: michael@0: /** Set this rectangle to the empty rectangle (0,0,0,0) michael@0: */ michael@0: void setEmpty() { memset(this, 0, sizeof(*this)); } michael@0: michael@0: void set(const SkIRect& src) { michael@0: fLeft = SkIntToScalar(src.fLeft); michael@0: fTop = SkIntToScalar(src.fTop); michael@0: fRight = SkIntToScalar(src.fRight); michael@0: fBottom = SkIntToScalar(src.fBottom); michael@0: } michael@0: michael@0: void set(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) { michael@0: fLeft = left; michael@0: fTop = top; michael@0: fRight = right; michael@0: fBottom = bottom; michael@0: } michael@0: // alias for set(l, t, r, b) michael@0: void setLTRB(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) { michael@0: this->set(left, top, right, bottom); michael@0: } michael@0: michael@0: /** Initialize the rect with the 4 specified integers. The routine handles michael@0: converting them to scalars (by calling SkIntToScalar) michael@0: */ michael@0: void iset(int left, int top, int right, int bottom) { michael@0: fLeft = SkIntToScalar(left); michael@0: fTop = SkIntToScalar(top); michael@0: fRight = SkIntToScalar(right); michael@0: fBottom = SkIntToScalar(bottom); michael@0: } michael@0: michael@0: /** michael@0: * Set this rectangle to be left/top at 0,0, and have the specified width michael@0: * and height (automatically converted to SkScalar). michael@0: */ michael@0: void isetWH(int width, int height) { michael@0: fLeft = fTop = 0; michael@0: fRight = SkIntToScalar(width); michael@0: fBottom = SkIntToScalar(height); michael@0: } michael@0: michael@0: /** Set this rectangle to be the bounds of the array of points. michael@0: If the array is empty (count == 0), then set this rectangle michael@0: to the empty rectangle (0,0,0,0) michael@0: */ michael@0: void set(const SkPoint pts[], int count) { michael@0: // set() had been checking for non-finite values, so keep that behavior michael@0: // for now. Now that we have setBoundsCheck(), we may decide to make michael@0: // set() be simpler/faster, and not check for those. michael@0: (void)this->setBoundsCheck(pts, count); michael@0: } michael@0: michael@0: // alias for set(pts, count) michael@0: void setBounds(const SkPoint pts[], int count) { michael@0: (void)this->setBoundsCheck(pts, count); michael@0: } michael@0: michael@0: /** michael@0: * Compute the bounds of the array of points, and set this rect to that michael@0: * bounds and return true... unless a non-finite value is encountered, michael@0: * in which case this rect is set to empty and false is returned. michael@0: */ michael@0: bool setBoundsCheck(const SkPoint pts[], int count); michael@0: michael@0: void set(const SkPoint& p0, const SkPoint& p1) { michael@0: fLeft = SkMinScalar(p0.fX, p1.fX); michael@0: fRight = SkMaxScalar(p0.fX, p1.fX); michael@0: fTop = SkMinScalar(p0.fY, p1.fY); michael@0: fBottom = SkMaxScalar(p0.fY, p1.fY); michael@0: } michael@0: michael@0: void setXYWH(SkScalar x, SkScalar y, SkScalar width, SkScalar height) { michael@0: fLeft = x; michael@0: fTop = y; michael@0: fRight = x + width; michael@0: fBottom = y + height; michael@0: } michael@0: michael@0: void setWH(SkScalar width, SkScalar height) { michael@0: fLeft = 0; michael@0: fTop = 0; michael@0: fRight = width; michael@0: fBottom = height; michael@0: } michael@0: michael@0: /** michael@0: * Make the largest representable rectangle michael@0: */ michael@0: void setLargest() { michael@0: fLeft = fTop = SK_ScalarMin; michael@0: fRight = fBottom = SK_ScalarMax; michael@0: } michael@0: michael@0: /** michael@0: * Make the largest representable rectangle, but inverted (e.g. fLeft will michael@0: * be max and right will be min). michael@0: */ michael@0: void setLargestInverted() { michael@0: fLeft = fTop = SK_ScalarMax; michael@0: fRight = fBottom = SK_ScalarMin; michael@0: } michael@0: michael@0: /** Offset set the rectangle by adding dx to its left and right, michael@0: and adding dy to its top and bottom. michael@0: */ michael@0: void offset(SkScalar dx, SkScalar dy) { michael@0: fLeft += dx; michael@0: fTop += dy; michael@0: fRight += dx; michael@0: fBottom += dy; michael@0: } michael@0: michael@0: void offset(const SkPoint& delta) { michael@0: this->offset(delta.fX, delta.fY); michael@0: } michael@0: michael@0: /** michael@0: * Offset this rect such its new x() and y() will equal newX and newY. michael@0: */ michael@0: void offsetTo(SkScalar newX, SkScalar newY) { michael@0: fRight += newX - fLeft; michael@0: fBottom += newY - fTop; michael@0: fLeft = newX; michael@0: fTop = newY; michael@0: } michael@0: michael@0: /** Inset the rectangle by (dx,dy). If dx is positive, then the sides are michael@0: moved inwards, making the rectangle narrower. If dx is negative, then michael@0: the sides are moved outwards, making the rectangle wider. The same holds michael@0: true for dy and the top and bottom. michael@0: */ michael@0: void inset(SkScalar dx, SkScalar dy) { michael@0: fLeft += dx; michael@0: fTop += dy; michael@0: fRight -= dx; michael@0: fBottom -= dy; michael@0: } michael@0: michael@0: /** Outset the rectangle by (dx,dy). If dx is positive, then the sides are michael@0: moved outwards, making the rectangle wider. If dx is negative, then the michael@0: sides are moved inwards, making the rectangle narrower. The same holds michael@0: true for dy and the top and bottom. michael@0: */ michael@0: void outset(SkScalar dx, SkScalar dy) { this->inset(-dx, -dy); } michael@0: michael@0: /** If this rectangle intersects r, return true and set this rectangle to that michael@0: intersection, otherwise return false and do not change this rectangle. michael@0: If either rectangle is empty, do nothing and return false. michael@0: */ michael@0: bool intersect(const SkRect& r); michael@0: bool intersect2(const SkRect& r); michael@0: michael@0: /** If this rectangle intersects the rectangle specified by left, top, right, bottom, michael@0: return true and set this rectangle to that intersection, otherwise return false michael@0: and do not change this rectangle. michael@0: If either rectangle is empty, do nothing and return false. michael@0: */ michael@0: bool intersect(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom); michael@0: michael@0: /** michael@0: * Return true if this rectangle is not empty, and the specified sides of michael@0: * a rectangle are not empty, and they intersect. michael@0: */ michael@0: bool intersects(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) const { michael@0: return // first check that both are not empty michael@0: left < right && top < bottom && michael@0: fLeft < fRight && fTop < fBottom && michael@0: // now check for intersection michael@0: fLeft < right && left < fRight && michael@0: fTop < bottom && top < fBottom; michael@0: } michael@0: michael@0: /** If rectangles a and b intersect, return true and set this rectangle to michael@0: * that intersection, otherwise return false and do not change this michael@0: * rectangle. If either rectangle is empty, do nothing and return false. michael@0: */ michael@0: bool intersect(const SkRect& a, const SkRect& b); michael@0: michael@0: /** michael@0: * Return true if rectangles a and b are not empty and intersect. michael@0: */ michael@0: static bool Intersects(const SkRect& a, const SkRect& b) { michael@0: return !a.isEmpty() && !b.isEmpty() && michael@0: a.fLeft < b.fRight && b.fLeft < a.fRight && michael@0: a.fTop < b.fBottom && b.fTop < a.fBottom; michael@0: } michael@0: michael@0: /** michael@0: * Update this rectangle to enclose itself and the specified rectangle. michael@0: * If this rectangle is empty, just set it to the specified rectangle. michael@0: * If the specified rectangle is empty, do nothing. michael@0: */ michael@0: void join(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom); michael@0: michael@0: /** Update this rectangle to enclose itself and the specified rectangle. michael@0: If this rectangle is empty, just set it to the specified rectangle. If the specified michael@0: rectangle is empty, do nothing. michael@0: */ michael@0: void join(const SkRect& r) { michael@0: this->join(r.fLeft, r.fTop, r.fRight, r.fBottom); michael@0: } michael@0: // alias for join() michael@0: void growToInclude(const SkRect& r) { this->join(r); } michael@0: michael@0: /** michael@0: * Grow the rect to include the specified (x,y). After this call, the michael@0: * following will be true: fLeft <= x <= fRight && fTop <= y <= fBottom. michael@0: * michael@0: * This is close, but not quite the same contract as contains(), since michael@0: * contains() treats the left and top different from the right and bottom. michael@0: * contains(x,y) -> fLeft <= x < fRight && fTop <= y < fBottom. Also note michael@0: * that contains(x,y) always returns false if the rect is empty. michael@0: */ michael@0: void growToInclude(SkScalar x, SkScalar y) { michael@0: fLeft = SkMinScalar(x, fLeft); michael@0: fRight = SkMaxScalar(x, fRight); michael@0: fTop = SkMinScalar(y, fTop); michael@0: fBottom = SkMaxScalar(y, fBottom); michael@0: } michael@0: michael@0: /** Bulk version of growToInclude */ michael@0: void growToInclude(const SkPoint pts[], int count) { michael@0: this->growToInclude(pts, sizeof(SkPoint), count); michael@0: } michael@0: michael@0: /** Bulk version of growToInclude with stride. */ michael@0: void growToInclude(const SkPoint pts[], size_t stride, int count) { michael@0: SkASSERT(count >= 0); michael@0: SkASSERT(stride >= sizeof(SkPoint)); michael@0: const SkPoint* end = (const SkPoint*)((intptr_t)pts + count * stride); michael@0: for (; pts < end; pts = (const SkPoint*)((intptr_t)pts + stride)) { michael@0: this->growToInclude(pts->fX, pts->fY); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Return true if this rectangle contains r, and if both rectangles are michael@0: * not empty. michael@0: */ michael@0: bool contains(const SkRect& r) const { michael@0: // todo: can we eliminate the this->isEmpty check? michael@0: return !r.isEmpty() && !this->isEmpty() && michael@0: fLeft <= r.fLeft && fTop <= r.fTop && michael@0: fRight >= r.fRight && fBottom >= r.fBottom; michael@0: } michael@0: michael@0: /** michael@0: * Set the dst rectangle by rounding this rectangle's coordinates to their michael@0: * nearest integer values using SkScalarRoundToInt. michael@0: */ michael@0: void round(SkIRect* dst) const { michael@0: SkASSERT(dst); michael@0: dst->set(SkScalarRoundToInt(fLeft), SkScalarRoundToInt(fTop), michael@0: SkScalarRoundToInt(fRight), SkScalarRoundToInt(fBottom)); michael@0: } michael@0: michael@0: /** michael@0: * Set the dst rectangle by rounding "out" this rectangle, choosing the michael@0: * SkScalarFloor of top and left, and the SkScalarCeil of right and bottom. michael@0: */ michael@0: void roundOut(SkIRect* dst) const { michael@0: SkASSERT(dst); michael@0: dst->set(SkScalarFloorToInt(fLeft), SkScalarFloorToInt(fTop), michael@0: SkScalarCeilToInt(fRight), SkScalarCeilToInt(fBottom)); michael@0: } michael@0: michael@0: /** michael@0: * Expand this rectangle by rounding its coordinates "out", choosing the michael@0: * floor of top and left, and the ceil of right and bottom. If this rect michael@0: * is already on integer coordinates, then it will be unchanged. michael@0: */ michael@0: void roundOut() { michael@0: this->set(SkScalarFloorToScalar(fLeft), michael@0: SkScalarFloorToScalar(fTop), michael@0: SkScalarCeilToScalar(fRight), michael@0: SkScalarCeilToScalar(fBottom)); michael@0: } michael@0: michael@0: /** michael@0: * Set the dst rectangle by rounding "in" this rectangle, choosing the michael@0: * ceil of top and left, and the floor of right and bottom. This does *not* michael@0: * call sort(), so it is possible that the resulting rect is inverted... michael@0: * e.g. left >= right or top >= bottom. Call isEmpty() to detect that. michael@0: */ michael@0: void roundIn(SkIRect* dst) const { michael@0: SkASSERT(dst); michael@0: dst->set(SkScalarCeilToInt(fLeft), SkScalarCeilToInt(fTop), michael@0: SkScalarFloorToInt(fRight), SkScalarFloorToInt(fBottom)); michael@0: } michael@0: michael@0: /** michael@0: * Return a new SkIRect which is contains the rounded coordinates of this michael@0: * rect using SkScalarRoundToInt. michael@0: */ michael@0: SkIRect round() const { michael@0: SkIRect ir; michael@0: this->round(&ir); michael@0: return ir; michael@0: } michael@0: michael@0: /** michael@0: * Swap top/bottom or left/right if there are flipped (i.e. if width() michael@0: * or height() would have returned a negative value.) This should be called michael@0: * if the edges are computed separately, and may have crossed over each michael@0: * other. When this returns, left <= right && top <= bottom michael@0: */ michael@0: void sort(); michael@0: michael@0: /** michael@0: * cast-safe way to treat the rect as an array of (4) SkScalars. michael@0: */ michael@0: const SkScalar* asScalars() const { return &fLeft; } michael@0: }; michael@0: michael@0: #endif