diff options
Diffstat (limited to 'include/core/SkRect.h')
-rw-r--r-- | include/core/SkRect.h | 189 |
1 files changed, 140 insertions, 49 deletions
diff --git a/include/core/SkRect.h b/include/core/SkRect.h index 53c61ea..550c5d1 100644 --- a/include/core/SkRect.h +++ b/include/core/SkRect.h @@ -94,6 +94,10 @@ struct SK_API SkIRect { fRight = right; fBottom = bottom; } + // 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); + } void setXYWH(int32_t x, int32_t y, int32_t width, int32_t height) { fLeft = x; @@ -101,6 +105,23 @@ struct SK_API SkIRect { fRight = x + width; fBottom = y + height; } + + /** + * Make the largest representable rectangle + */ + void setLargest() { + fLeft = fTop = SK_MinS32; + fRight = fBottom = SK_MaxS32; + } + + /** + * 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; + } /** Offset set the rectangle by adding dx to its left and right, and adding dy to its top and bottom. @@ -127,6 +148,10 @@ struct SK_API SkIRect { fBottom -= dy; } + bool quickReject(int l, int t, int r, int b) const { + return l >= fRight || fLeft >= r || t >= fBottom || fTop >= b; + } + /** 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 @@ -263,6 +288,11 @@ struct SK_API SkIRect { When this returns, left <= right && top <= bottom */ void sort(); + + static const SkIRect& EmptyIRect() { + static const SkIRect gEmpty = {0,0,0,0}; + return gEmpty; + } }; /** \struct SkRect @@ -304,6 +334,10 @@ struct SK_API SkRect { */ bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; } bool hasValidCoordinates() const; + SkScalar left() const { return fLeft; } + SkScalar top() const { return fTop; } + SkScalar right() const { return fRight; } + SkScalar bottom() const { return fBottom; } SkScalar width() const { return fRight - fLeft; } SkScalar height() const { return fBottom - fTop; } SkScalar centerX() const { return SkScalarHalf(fLeft + fRight); } @@ -338,7 +372,11 @@ struct SK_API SkRect { fRight = right; fBottom = bottom; } - + // alias for set(l, t, r, b) + void setLTRB(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) { + this->set(left, top, right, bottom); + } + /** Initialize the rect with the 4 specified integers. The routine handles converting them to scalars (by calling SkIntToScalar) */ @@ -355,6 +393,11 @@ struct SK_API SkRect { */ void set(const SkPoint pts[], int count); + // alias for set(pts, count) + void setBounds(const SkPoint pts[], int count) { + this->set(pts, count); + } + void setXYWH(SkScalar x, SkScalar y, SkScalar width, SkScalar height) { fLeft = x; fTop = y; @@ -362,6 +405,23 @@ struct SK_API SkRect { fBottom = y + height; } + /** + * Make the largest representable rectangle + */ + void setLargest() { + fLeft = fTop = SK_ScalarMin; + fRight = fBottom = SK_ScalarMax; + } + + /** + * 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; + } + /** Offset set the rectangle by adding dx to its left and right, and adding dy to its top and bottom. */ @@ -400,9 +460,10 @@ struct SK_API SkRect { */ bool intersect(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom); - /** Return true if this rectangle is not empty, and the specified sides of - a rectangle are not empty, and they intersect. - */ + /** + * Return true if this rectangle is not empty, and the specified sides of + * a rectangle are not empty, and they intersect. + */ bool intersects(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) const { return // first check that both are not empty left < right && top < bottom && @@ -412,18 +473,20 @@ struct SK_API SkRect { fTop < bottom && top < fBottom; } - /** Return true if rectangles a and b are not empty and intersect. - */ + /** + * Return true if rectangles a and b are not empty and intersect. + */ static bool Intersects(const SkRect& a, const SkRect& b) { - return !a.isEmpty() && !b.isEmpty() && // check for empties - a.fLeft < b.fRight && b.fLeft < a.fRight && - a.fTop < b.fBottom && b.fTop < a.fBottom; + return !a.isEmpty() && !b.isEmpty() && + a.fLeft < b.fRight && b.fLeft < a.fRight && + a.fTop < b.fBottom && b.fTop < a.fBottom; } - /** 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. - */ + /** + * 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(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom); /** Update this rectangle to enclose itself and the specified rectangle. @@ -433,61 +496,89 @@ struct SK_API SkRect { void join(const SkRect& r) { this->join(r.fLeft, r.fTop, r.fRight, r.fBottom); } + // alias for join() + void growToInclude(const SkRect& r) { this->join(r); } + + /** + * 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. + */ + void growToInclude(SkScalar x, SkScalar y) { + fLeft = SkMinScalar(x, fLeft); + fRight = SkMaxScalar(x, fRight); + fTop = SkMinScalar(y, fTop); + fBottom = SkMaxScalar(y, fBottom); + } - /** Returns true if (p.fX,p.fY) is inside the rectangle. The left and top coordinates of - the rectangle are considered to be inside, while the right and bottom coordinates - 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. - If this rectangle is empty, return false. - */ + /** + * Returns true if (p.fX,p.fY) is inside the rectangle, and the rectangle + * is not empty. + * + * Contains treats the left and top differently from the right and bottom. + * The left and top coordinates of the rectangle are themselves considered + * to be inside, while the right and bottom are not. Thus for the rectangle + * {0, 0, 5, 10}, (0,0) is contained, but (0,10), (5,0) and (5,10) are not. + */ bool contains(const SkPoint& p) const { - return !this->isEmpty() && - fLeft <= p.fX && p.fX < fRight && - fTop <= p.fY && p.fY < fBottom; - } - - /** Returns true if (x,y) is inside the rectangle. The left and top coordinates of - the rectangle are considered to be inside, while the right and bottom coordinates - 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. - If this rectangle is empty, return false. - */ + return !this->isEmpty() && + fLeft <= p.fX && p.fX < fRight && fTop <= p.fY && p.fY < fBottom; + } + + /** + * Returns true if (x,y) is inside the rectangle, and the rectangle + * is not empty. + * + * Contains treats the left and top differently from the right and bottom. + * The left and top coordinates of the rectangle are themselves considered + * to be inside, while the right and bottom are not. Thus for the rectangle + * {0, 0, 5, 10}, (0,0) is contained, but (0,10), (5,0) and (5,10) are not. + */ bool contains(SkScalar x, SkScalar y) const { return !this->isEmpty() && - fLeft <= x && x < fRight && - fTop <= y && y < fBottom; + fLeft <= x && x < fRight && fTop <= y && y < fBottom; } - /** Return true if this rectangle contains r. - If either rectangle is empty, return false. - */ + /** + * Return true if this rectangle contains r, and if both rectangles are + * not empty. + */ bool contains(const SkRect& r) const { - return !r.isEmpty() && !this->isEmpty() && // check for empties + return !r.isEmpty() && !this->isEmpty() && fLeft <= r.fLeft && fTop <= r.fTop && fRight >= r.fRight && fBottom >= r.fBottom; } - /** Set the dst integer rectangle by rounding this rectangle's coordinates - to their nearest integer values. - */ + /** + * Set the dst rectangle by rounding this rectangle's coordinates to their + * nearest integer values using SkScalarRound. + */ void round(SkIRect* dst) const { SkASSERT(dst); - dst->set(SkScalarRound(fLeft), SkScalarRound(fTop), SkScalarRound(fRight), SkScalarRound(fBottom)); + dst->set(SkScalarRound(fLeft), SkScalarRound(fTop), + SkScalarRound(fRight), SkScalarRound(fBottom)); } - /** Set the dst integer rectangle by rounding "out" this rectangle, choosing the floor of top and left, - and the ceiling of right and bototm. - */ + /** + * Set the dst rectangle by rounding "out" this rectangle, choosing the + * SkScalarFloor of top and left, and the SkScalarCeil of right and bottom. + */ void roundOut(SkIRect* dst) const { SkASSERT(dst); - dst->set(SkScalarFloor(fLeft), SkScalarFloor(fTop), SkScalarCeil(fRight), SkScalarCeil(fBottom)); + dst->set(SkScalarFloor(fLeft), SkScalarFloor(fTop), + SkScalarCeil(fRight), SkScalarCeil(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 - */ + /** + * 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 + */ void sort(); }; |