2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkRect_DEFINED
|
|
|
|
#define SkRect_DEFINED
|
|
|
|
|
|
|
|
#include "SkPoint.h"
|
2010-02-24 01:49:13 +00:00
|
|
|
#include "SkSize.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-02-23 22:44:57 +00:00
|
|
|
struct SkRect;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** \struct SkIRect
|
|
|
|
|
|
|
|
SkIRect holds four 32 bit integer coordinates for a rectangle
|
|
|
|
*/
|
2011-03-15 21:27:08 +00:00
|
|
|
struct SK_API SkIRect {
|
2008-12-17 15:59:43 +00:00
|
|
|
int32_t fLeft, fTop, fRight, fBottom;
|
|
|
|
|
2012-05-10 14:50:44 +00:00
|
|
|
static SkIRect SK_WARN_UNUSED_RESULT MakeEmpty() {
|
2010-07-13 18:35:14 +00:00
|
|
|
SkIRect r;
|
|
|
|
r.setEmpty();
|
|
|
|
return r;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2013-07-24 22:19:24 +00:00
|
|
|
static SkIRect SK_WARN_UNUSED_RESULT MakeLargest() {
|
|
|
|
SkIRect r;
|
|
|
|
r.setLargest();
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-05-10 14:50:44 +00:00
|
|
|
static SkIRect SK_WARN_UNUSED_RESULT MakeWH(int32_t w, int32_t h) {
|
2010-07-13 18:35:14 +00:00
|
|
|
SkIRect r;
|
|
|
|
r.set(0, 0, w, h);
|
|
|
|
return r;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-10 14:50:44 +00:00
|
|
|
static SkIRect SK_WARN_UNUSED_RESULT MakeSize(const SkISize& size) {
|
2010-07-13 18:35:14 +00:00
|
|
|
SkIRect r;
|
|
|
|
r.set(0, 0, size.width(), size.height());
|
|
|
|
return r;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-10 14:50:44 +00:00
|
|
|
static SkIRect SK_WARN_UNUSED_RESULT MakeLTRB(int32_t l, int32_t t, int32_t r, int32_t b) {
|
2010-07-13 18:35:14 +00:00
|
|
|
SkIRect rect;
|
|
|
|
rect.set(l, t, r, b);
|
|
|
|
return rect;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-10 14:50:44 +00:00
|
|
|
static SkIRect SK_WARN_UNUSED_RESULT MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h) {
|
2010-07-13 18:35:14 +00:00
|
|
|
SkIRect r;
|
|
|
|
r.set(x, y, x + w, y + h);
|
|
|
|
return r;
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-10-03 20:27:14 +00:00
|
|
|
int left() const { return fLeft; }
|
|
|
|
int top() const { return fTop; }
|
|
|
|
int right() const { return fRight; }
|
|
|
|
int bottom() const { return fBottom; }
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-10-03 20:27:14 +00:00
|
|
|
/** 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.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
int width() const { return fRight - fLeft; }
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-10-03 20:27:14 +00:00
|
|
|
/**
|
|
|
|
* Returns the rectangle's height. This does not check for a valid rect
|
|
|
|
* (i.e. top <= bottom) so the result may be negative.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
int height() const { return fBottom - fTop; }
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2014-10-15 14:05:09 +00:00
|
|
|
SkISize size() const { return SkISize::Make(this->width(), this->height()); }
|
|
|
|
|
2012-11-08 22:36:19 +00:00
|
|
|
/**
|
|
|
|
* 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; }
|
2012-11-09 02:01:24 +00:00
|
|
|
|
2012-11-08 22:36:19 +00:00
|
|
|
/**
|
|
|
|
* 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; }
|
2012-11-09 02:01:24 +00:00
|
|
|
|
2011-10-03 20:27:14 +00:00
|
|
|
/**
|
|
|
|
* Return true if the rectangle's width or height are <= 0
|
|
|
|
*/
|
|
|
|
bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2013-07-24 22:19:24 +00:00
|
|
|
bool isLargest() const { return SK_MinS32 == fLeft &&
|
|
|
|
SK_MinS32 == fTop &&
|
|
|
|
SK_MaxS32 == fRight &&
|
|
|
|
SK_MaxS32 == fBottom; }
|
|
|
|
|
2011-07-20 19:55:42 +00:00
|
|
|
friend bool operator==(const SkIRect& a, const SkIRect& b) {
|
2008-12-17 15:59:43 +00:00
|
|
|
return !memcmp(&a, &b, sizeof(a));
|
|
|
|
}
|
2010-02-19 21:41:30 +00:00
|
|
|
|
2011-07-20 19:55:42 +00:00
|
|
|
friend bool operator!=(const SkIRect& a, const SkIRect& b) {
|
|
|
|
return !(a == b);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2009-11-21 02:48:11 +00:00
|
|
|
bool is16Bit() const {
|
|
|
|
return SkIsS16(fLeft) && SkIsS16(fTop) &&
|
|
|
|
SkIsS16(fRight) && SkIsS16(fBottom);
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Set the rectangle to (0,0,0,0)
|
|
|
|
*/
|
|
|
|
void setEmpty() { memset(this, 0, sizeof(*this)); }
|
|
|
|
|
2010-02-19 21:41:30 +00:00
|
|
|
void set(int32_t left, int32_t top, int32_t right, int32_t bottom) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fLeft = left;
|
|
|
|
fTop = top;
|
|
|
|
fRight = right;
|
|
|
|
fBottom = bottom;
|
|
|
|
}
|
2011-05-09 17:00:02 +00:00
|
|
|
// 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);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-03-03 13:23:35 +00:00
|
|
|
void setXYWH(int32_t x, int32_t y, int32_t width, int32_t height) {
|
|
|
|
fLeft = x;
|
|
|
|
fTop = y;
|
|
|
|
fRight = x + width;
|
|
|
|
fBottom = y + height;
|
|
|
|
}
|
2011-05-09 17:00:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make the largest representable rectangle
|
|
|
|
*/
|
|
|
|
void setLargest() {
|
|
|
|
fLeft = fTop = SK_MinS32;
|
|
|
|
fRight = fBottom = SK_MaxS32;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-05-09 17:00:02 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2014-06-02 21:38:28 +00:00
|
|
|
/**
|
|
|
|
* Return a new IRect, built as an offset of this rect.
|
|
|
|
*/
|
2015-03-13 13:08:28 +00:00
|
|
|
SkIRect makeOffset(int32_t dx, int32_t dy) const {
|
2014-06-02 21:38:28 +00:00
|
|
|
return MakeLTRB(fLeft + dx, fTop + dy, fRight + dx, fBottom + dy);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a new IRect, built as an inset of this rect.
|
|
|
|
*/
|
2015-03-13 13:08:28 +00:00
|
|
|
SkIRect makeInset(int32_t dx, int32_t dy) const {
|
2014-06-02 21:38:28 +00:00
|
|
|
return MakeLTRB(fLeft + dx, fTop + dy, fRight - dx, fBottom - dy);
|
|
|
|
}
|
|
|
|
|
2015-03-13 13:08:28 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Offset set the rectangle by adding dx to its left and right,
|
|
|
|
and adding dy to its top and bottom.
|
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
void offset(int32_t dx, int32_t dy) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fLeft += dx;
|
|
|
|
fTop += dy;
|
|
|
|
fRight += dx;
|
|
|
|
fBottom += dy;
|
|
|
|
}
|
|
|
|
|
2010-02-19 21:41:30 +00:00
|
|
|
void offset(const SkIPoint& delta) {
|
|
|
|
this->offset(delta.fX, delta.fY);
|
|
|
|
}
|
|
|
|
|
2012-11-06 22:02:51 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** 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,
|
2012-05-15 16:47:23 +00:00
|
|
|
making the rectangle wider. The same holds true for dy and the top and bottom.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
void inset(int32_t dx, int32_t dy) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fLeft += dx;
|
|
|
|
fTop += dy;
|
|
|
|
fRight -= dx;
|
|
|
|
fBottom -= dy;
|
|
|
|
}
|
2010-02-19 21:41:30 +00:00
|
|
|
|
2012-05-15 16:47:23 +00:00
|
|
|
/** 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); }
|
|
|
|
|
2011-05-09 17:00:02 +00:00
|
|
|
bool quickReject(int l, int t, int r, int b) const {
|
|
|
|
return l >= fRight || fLeft >= r || t >= fBottom || fTop >= b;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** 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.
|
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
bool contains(int32_t x, int32_t y) const {
|
2008-12-17 15:59:43 +00:00
|
|
|
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.
|
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
bool contains(int32_t left, int32_t top, int32_t right, int32_t bottom) const {
|
2008-12-17 15:59:43 +00:00
|
|
|
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.
|
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
bool contains(const SkIRect& r) const {
|
2008-12-17 15:59:43 +00:00
|
|
|
return !r.isEmpty() && !this->isEmpty() && // check for empties
|
|
|
|
fLeft <= r.fLeft && fTop <= r.fTop &&
|
|
|
|
fRight >= r.fRight && fBottom >= r.fBottom;
|
|
|
|
}
|
|
|
|
|
2015-02-23 22:44:57 +00:00
|
|
|
/** Returns true if the specified rectangle r is inside or equal to this rectangle.
|
|
|
|
*/
|
|
|
|
bool contains(const SkRect& r) const;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return true if this rectangle contains the specified rectangle.
|
2012-05-10 14:50:44 +00:00
|
|
|
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.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
bool containsNoEmptyCheck(int32_t left, int32_t top,
|
2012-05-10 14:50:44 +00:00
|
|
|
int32_t right, int32_t bottom) const {
|
|
|
|
SkASSERT(fLeft < fRight && fTop < fBottom);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(left < right && top < bottom);
|
|
|
|
|
|
|
|
return fLeft <= left && fTop <= top &&
|
2012-05-10 14:50:44 +00:00
|
|
|
fRight >= right && fBottom >= bottom;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-31 18:28:59 +00:00
|
|
|
bool containsNoEmptyCheck(const SkIRect& r) const {
|
|
|
|
return containsNoEmptyCheck(r.fLeft, r.fTop, r.fRight, r.fBottom);
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** 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.
|
|
|
|
*/
|
2015-01-07 20:16:10 +00:00
|
|
|
bool SK_WARN_UNUSED_RESULT intersect(const SkIRect& r) {
|
2008-12-17 15:59:43 +00:00
|
|
|
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.
|
|
|
|
*/
|
2015-01-07 20:16:10 +00:00
|
|
|
bool SK_WARN_UNUSED_RESULT intersect(const SkIRect& a, const SkIRect& b) {
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (!a.isEmpty() && !b.isEmpty() &&
|
2010-02-19 21:41:30 +00:00
|
|
|
a.fLeft < b.fRight && b.fLeft < a.fRight &&
|
|
|
|
a.fTop < b.fBottom && b.fTop < a.fBottom) {
|
2008-12-17 15:59:43 +00:00
|
|
|
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;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** 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.
|
|
|
|
*/
|
2015-01-07 20:16:10 +00:00
|
|
|
bool SK_WARN_UNUSED_RESULT intersectNoEmptyCheck(const SkIRect& a, const SkIRect& b) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(!a.isEmpty() && !b.isEmpty());
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (a.fLeft < b.fRight && b.fLeft < a.fRight &&
|
2010-02-19 21:41:30 +00:00
|
|
|
a.fTop < b.fBottom && b.fTop < a.fBottom) {
|
2008-12-17 15:59:43 +00:00
|
|
|
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.
|
|
|
|
*/
|
2015-01-07 20:16:10 +00:00
|
|
|
bool SK_WARN_UNUSED_RESULT intersect(int32_t left, int32_t top,
|
|
|
|
int32_t right, int32_t bottom) {
|
2008-12-17 15:59:43 +00:00
|
|
|
if (left < right && top < bottom && !this->isEmpty() &&
|
2010-02-19 21:41:30 +00:00
|
|
|
fLeft < right && left < fRight && fTop < bottom && top < fBottom) {
|
2008-12-17 15:59:43 +00:00
|
|
|
if (fLeft < left) fLeft = left;
|
|
|
|
if (fTop < top) fTop = top;
|
|
|
|
if (fRight > right) fRight = right;
|
|
|
|
if (fBottom > bottom) fBottom = bottom;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns true if a and b are not empty, and they intersect
|
2012-05-31 18:28:59 +00:00
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
static bool Intersects(const SkIRect& a, const SkIRect& b) {
|
2008-12-17 15:59:43 +00:00
|
|
|
return !a.isEmpty() && !b.isEmpty() && // check for empties
|
2015-01-07 20:16:10 +00:00
|
|
|
a.fLeft < b.fRight && b.fLeft < a.fRight &&
|
|
|
|
a.fTop < b.fBottom && b.fTop < a.fBottom;
|
2012-05-31 18:28:59 +00:00
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-31 18:28:59 +00:00
|
|
|
/**
|
|
|
|
* 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 &&
|
2008-12-17 15:59:43 +00:00
|
|
|
a.fTop < b.fBottom && b.fTop < a.fBottom;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** 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.
|
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
void join(const SkIRect& r) {
|
2008-12-17 15:59:43 +00:00
|
|
|
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();
|
2011-05-09 17:00:02 +00:00
|
|
|
|
2012-05-10 14:50:44 +00:00
|
|
|
static const SkIRect& SK_WARN_UNUSED_RESULT EmptyIRect() {
|
2011-05-16 18:24:19 +00:00
|
|
|
static const SkIRect gEmpty = { 0, 0, 0, 0 };
|
2011-05-09 17:00:02 +00:00
|
|
|
return gEmpty;
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** \struct SkRect
|
|
|
|
*/
|
2011-03-15 21:27:08 +00:00
|
|
|
struct SK_API SkRect {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkScalar fLeft, fTop, fRight, fBottom;
|
|
|
|
|
2012-05-10 14:50:44 +00:00
|
|
|
static SkRect SK_WARN_UNUSED_RESULT MakeEmpty() {
|
2010-04-12 21:21:59 +00:00
|
|
|
SkRect r;
|
|
|
|
r.setEmpty();
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-10-10 13:51:19 +00:00
|
|
|
static SkRect SK_WARN_UNUSED_RESULT MakeLargest() {
|
|
|
|
SkRect r;
|
|
|
|
r.setLargest();
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-05-10 14:50:44 +00:00
|
|
|
static SkRect SK_WARN_UNUSED_RESULT MakeWH(SkScalar w, SkScalar h) {
|
2010-04-12 21:21:59 +00:00
|
|
|
SkRect r;
|
|
|
|
r.set(0, 0, w, h);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-12-15 21:42:51 +00:00
|
|
|
static SkRect SK_WARN_UNUSED_RESULT MakeIWH(int w, int h) {
|
|
|
|
SkRect r;
|
|
|
|
r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-05-10 14:50:44 +00:00
|
|
|
static SkRect SK_WARN_UNUSED_RESULT MakeSize(const SkSize& size) {
|
2010-02-24 01:49:13 +00:00
|
|
|
SkRect r;
|
|
|
|
r.set(0, 0, size.width(), size.height());
|
|
|
|
return r;
|
|
|
|
}
|
2013-01-17 07:06:06 +00:00
|
|
|
|
2012-05-10 14:50:44 +00:00
|
|
|
static SkRect SK_WARN_UNUSED_RESULT MakeLTRB(SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
|
2010-02-24 01:49:13 +00:00
|
|
|
SkRect rect;
|
|
|
|
rect.set(l, t, r, b);
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2012-05-10 14:50:44 +00:00
|
|
|
static SkRect SK_WARN_UNUSED_RESULT MakeXYWH(SkScalar x, SkScalar y, SkScalar w, SkScalar h) {
|
2010-02-24 01:49:13 +00:00
|
|
|
SkRect r;
|
|
|
|
r.set(x, y, x + w, y + h);
|
2012-11-07 16:17:24 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:28:30 +00:00
|
|
|
SK_ATTR_DEPRECATED("use Make()")
|
2012-11-07 16:17:24 +00:00
|
|
|
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));
|
2010-02-24 01:49:13 +00:00
|
|
|
return r;
|
|
|
|
}
|
2013-01-24 07:06:13 +00:00
|
|
|
|
2013-01-23 15:37:56 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-01-24 07:06:13 +00:00
|
|
|
|
2011-12-06 18:56:37 +00:00
|
|
|
/**
|
|
|
|
* Return true if the rectangle's width or height are <= 0
|
|
|
|
*/
|
|
|
|
bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2013-10-10 13:51:19 +00:00
|
|
|
bool isLargest() const { return SK_ScalarMin == fLeft &&
|
|
|
|
SK_ScalarMin == fTop &&
|
|
|
|
SK_ScalarMax == fRight &&
|
|
|
|
SK_ScalarMax == fBottom; }
|
|
|
|
|
2011-12-06 18:56:37 +00:00
|
|
|
/**
|
|
|
|
* Returns true iff all values in the rect are finite. If any are
|
2016-02-25 18:28:11 +00:00
|
|
|
* infinite or NaN then this returns false.
|
2011-12-06 18:56:37 +00:00
|
|
|
*/
|
|
|
|
bool isFinite() const {
|
2012-05-16 13:35:36 +00:00
|
|
|
float accum = 0;
|
|
|
|
accum *= fLeft;
|
|
|
|
accum *= fTop;
|
|
|
|
accum *= fRight;
|
|
|
|
accum *= fBottom;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-16 13:35:36 +00:00
|
|
|
// accum is either NaN or it is finite (zero).
|
2014-12-15 20:08:47 +00:00
|
|
|
SkASSERT(0 == accum || SkScalarIsNaN(accum));
|
2012-05-16 13:35:36 +00:00
|
|
|
|
2011-12-06 18:56:37 +00:00
|
|
|
// value==value will be true iff value is not NaN
|
2012-05-16 13:35:36 +00:00
|
|
|
// TODO: is it faster to say !accum or accum==accum?
|
2014-12-15 20:08:47 +00:00
|
|
|
return !SkScalarIsNaN(accum);
|
2011-12-06 18:56:37 +00:00
|
|
|
}
|
|
|
|
|
2012-04-19 21:01:24 +00:00
|
|
|
SkScalar x() const { return fLeft; }
|
|
|
|
SkScalar y() const { return fTop; }
|
2011-05-09 17:00:02 +00:00
|
|
|
SkScalar left() const { return fLeft; }
|
|
|
|
SkScalar top() const { return fTop; }
|
|
|
|
SkScalar right() const { return fRight; }
|
|
|
|
SkScalar bottom() const { return fBottom; }
|
2008-12-17 15:59:43 +00:00
|
|
|
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); }
|
|
|
|
|
2011-09-01 15:34:14 +00:00
|
|
|
friend bool operator==(const SkRect& a, const SkRect& b) {
|
2012-10-31 19:59:23 +00:00
|
|
|
return SkScalarsEqual((SkScalar*)&a, (SkScalar*)&b, 4);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2010-02-19 21:41:30 +00:00
|
|
|
|
2011-09-01 15:34:14 +00:00
|
|
|
friend bool operator!=(const SkRect& a, const SkRect& b) {
|
2012-10-31 19:59:23 +00:00
|
|
|
return !SkScalarsEqual((SkScalar*)&a, (SkScalar*)&b, 4);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 21:56:37 +00:00
|
|
|
/** 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.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
void toQuad(SkPoint quad[4]) const;
|
|
|
|
|
|
|
|
/** Set this rectangle to the empty rectangle (0,0,0,0)
|
|
|
|
*/
|
|
|
|
void setEmpty() { memset(this, 0, sizeof(*this)); }
|
|
|
|
|
2010-02-19 21:41:30 +00:00
|
|
|
void set(const SkIRect& src) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fLeft = SkIntToScalar(src.fLeft);
|
|
|
|
fTop = SkIntToScalar(src.fTop);
|
|
|
|
fRight = SkIntToScalar(src.fRight);
|
|
|
|
fBottom = SkIntToScalar(src.fBottom);
|
|
|
|
}
|
|
|
|
|
2010-02-19 21:41:30 +00:00
|
|
|
void set(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fLeft = left;
|
|
|
|
fTop = top;
|
|
|
|
fRight = right;
|
|
|
|
fBottom = bottom;
|
|
|
|
}
|
2011-05-09 17:00:02 +00:00
|
|
|
// alias for set(l, t, r, b)
|
|
|
|
void setLTRB(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) {
|
|
|
|
this->set(left, top, right, bottom);
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** 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);
|
|
|
|
}
|
|
|
|
|
2013-01-16 15:15:24 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2013-01-17 07:06:06 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** 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)
|
|
|
|
*/
|
2012-07-26 15:20:36 +00:00
|
|
|
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);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-05-09 17:00:02 +00:00
|
|
|
// alias for set(pts, count)
|
|
|
|
void setBounds(const SkPoint pts[], int count) {
|
2012-07-26 15:20:36 +00:00
|
|
|
(void)this->setBoundsCheck(pts, count);
|
2011-05-09 17:00:02 +00:00
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-07-26 15:20:36 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-16 13:35:36 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-03-03 13:23:35 +00:00
|
|
|
void setXYWH(SkScalar x, SkScalar y, SkScalar width, SkScalar height) {
|
|
|
|
fLeft = x;
|
|
|
|
fTop = y;
|
|
|
|
fRight = x + width;
|
|
|
|
fBottom = y + height;
|
|
|
|
}
|
|
|
|
|
2012-10-11 19:39:09 +00:00
|
|
|
void setWH(SkScalar width, SkScalar height) {
|
|
|
|
fLeft = 0;
|
|
|
|
fTop = 0;
|
|
|
|
fRight = width;
|
|
|
|
fBottom = height;
|
|
|
|
}
|
|
|
|
|
2011-05-09 17:00:02 +00:00
|
|
|
/**
|
|
|
|
* Make the largest representable rectangle
|
|
|
|
*/
|
|
|
|
void setLargest() {
|
|
|
|
fLeft = fTop = SK_ScalarMin;
|
|
|
|
fRight = fBottom = SK_ScalarMax;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-05-09 17:00:02 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2014-06-02 21:38:28 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2015-03-13 13:08:28 +00:00
|
|
|
|
2014-06-02 21:38:28 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2015-03-13 13:08:28 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Offset set the rectangle by adding dx to its left and right,
|
|
|
|
and adding dy to its top and bottom.
|
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
void offset(SkScalar dx, SkScalar dy) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fLeft += dx;
|
|
|
|
fTop += dy;
|
|
|
|
fRight += dx;
|
|
|
|
fBottom += dy;
|
2012-08-23 18:09:54 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2010-02-19 21:41:30 +00:00
|
|
|
void offset(const SkPoint& delta) {
|
|
|
|
this->offset(delta.fX, delta.fY);
|
|
|
|
}
|
|
|
|
|
2012-11-06 22:02:51 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2012-11-08 02:03:56 +00:00
|
|
|
|
2011-08-23 14:39:01 +00:00
|
|
|
/** 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.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
void inset(SkScalar dx, SkScalar dy) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fLeft += dx;
|
|
|
|
fTop += dy;
|
|
|
|
fRight -= dx;
|
|
|
|
fBottom -= dy;
|
|
|
|
}
|
|
|
|
|
2011-08-23 14:39:01 +00:00
|
|
|
/** 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
|
2012-05-15 16:47:23 +00:00
|
|
|
sides are moved inwards, making the rectangle narrower. The same holds
|
2011-08-23 14:39:01 +00:00
|
|
|
true for dy and the top and bottom.
|
|
|
|
*/
|
|
|
|
void outset(SkScalar dx, SkScalar dy) { this->inset(-dx, -dy); }
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** 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.
|
|
|
|
*/
|
2015-01-07 20:16:10 +00:00
|
|
|
bool SK_WARN_UNUSED_RESULT intersect(const SkRect& r);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
/** 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.
|
|
|
|
*/
|
2015-01-07 20:16:10 +00:00
|
|
|
bool SK_WARN_UNUSED_RESULT intersect(SkScalar left, SkScalar top,
|
|
|
|
SkScalar right, SkScalar bottom);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-10-09 21:29:01 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-01-07 20:16:10 +00:00
|
|
|
bool SK_WARN_UNUSED_RESULT intersect(const SkRect& a, const SkRect& b);
|
2014-10-09 21:29:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2011-05-12 22:31:01 +00:00
|
|
|
/**
|
|
|
|
* Return true if this rectangle is not empty, and the specified sides of
|
|
|
|
* a rectangle are not empty, and they intersect.
|
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
bool intersects(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) const {
|
2014-10-09 21:29:01 +00:00
|
|
|
return Intersects(fLeft, fTop, fRight, fBottom, left, top, right, bottom);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-10-21 14:05:06 +00:00
|
|
|
|
2014-12-16 16:07:43 +00:00
|
|
|
bool intersects(const SkRect& r) const {
|
|
|
|
return Intersects(fLeft, fTop, fRight, fBottom,
|
|
|
|
r.fLeft, r.fTop, r.fRight, r.fBottom);
|
|
|
|
}
|
|
|
|
|
2011-05-12 22:31:01 +00:00
|
|
|
/**
|
|
|
|
* Return true if rectangles a and b are not empty and intersect.
|
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
static bool Intersects(const SkRect& a, const SkRect& b) {
|
2014-10-09 21:29:01 +00:00
|
|
|
return Intersects(a.fLeft, a.fTop, a.fRight, a.fBottom,
|
|
|
|
b.fLeft, b.fTop, b.fRight, b.fBottom);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-05-12 22:31:01 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
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.
|
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
void join(const SkRect& r) {
|
2008-12-17 15:59:43 +00:00
|
|
|
this->join(r.fLeft, r.fTop, r.fRight, r.fBottom);
|
|
|
|
}
|
2014-10-01 16:24:12 +00:00
|
|
|
|
|
|
|
void joinNonEmptyArg(const SkRect& r) {
|
|
|
|
SkASSERT(!r.isEmpty());
|
|
|
|
// if we are empty, just assign
|
|
|
|
if (fLeft >= fRight || fTop >= fBottom) {
|
|
|
|
*this = r;
|
|
|
|
} else {
|
2015-05-01 15:51:48 +00:00
|
|
|
this->joinPossiblyEmptyRect(r);
|
2014-10-01 16:24:12 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-09 17:00:02 +00:00
|
|
|
|
2015-05-01 15:51:48 +00:00
|
|
|
/**
|
|
|
|
* Joins the rectangle with another without checking if either are empty (may produce unexpected
|
|
|
|
* results if either rect is inverted).
|
|
|
|
*/
|
|
|
|
void joinPossiblyEmptyRect(const SkRect& r) {
|
|
|
|
fLeft = SkMinScalar(fLeft, r.left());
|
|
|
|
fTop = SkMinScalar(fTop, r.top());
|
|
|
|
fRight = SkMaxScalar(fRight, r.right());
|
|
|
|
fBottom = SkMaxScalar(fBottom, r.bottom());
|
|
|
|
}
|
|
|
|
|
2011-05-12 22:31:01 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-05-09 17:00:02 +00:00
|
|
|
void growToInclude(SkScalar x, SkScalar y) {
|
|
|
|
fLeft = SkMinScalar(x, fLeft);
|
2011-05-09 22:32:52 +00:00
|
|
|
fRight = SkMaxScalar(x, fRight);
|
|
|
|
fTop = SkMinScalar(y, fTop);
|
2011-05-09 17:00:02 +00:00
|
|
|
fBottom = SkMaxScalar(y, fBottom);
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2013-09-03 14:56:17 +00:00
|
|
|
/** 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 22:31:01 +00:00
|
|
|
/**
|
|
|
|
* Return true if this rectangle contains r, and if both rectangles are
|
|
|
|
* not empty.
|
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
bool contains(const SkRect& r) const {
|
2013-10-30 17:39:43 +00:00
|
|
|
// todo: can we eliminate the this->isEmpty check?
|
2011-05-12 22:31:01 +00:00
|
|
|
return !r.isEmpty() && !this->isEmpty() &&
|
2008-12-17 15:59:43 +00:00
|
|
|
fLeft <= r.fLeft && fTop <= r.fTop &&
|
|
|
|
fRight >= r.fRight && fBottom >= r.fBottom;
|
|
|
|
}
|
|
|
|
|
2015-02-25 21:19:48 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2011-05-12 22:31:01 +00:00
|
|
|
/**
|
|
|
|
* Set the dst rectangle by rounding this rectangle's coordinates to their
|
2013-11-07 16:06:53 +00:00
|
|
|
* nearest integer values using SkScalarRoundToInt.
|
2011-05-12 22:31:01 +00:00
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
void round(SkIRect* dst) const {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(dst);
|
2012-11-06 22:02:51 +00:00
|
|
|
dst->set(SkScalarRoundToInt(fLeft), SkScalarRoundToInt(fTop),
|
|
|
|
SkScalarRoundToInt(fRight), SkScalarRoundToInt(fBottom));
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2011-05-12 22:31:01 +00:00
|
|
|
/**
|
|
|
|
* Set the dst rectangle by rounding "out" this rectangle, choosing the
|
|
|
|
* SkScalarFloor of top and left, and the SkScalarCeil of right and bottom.
|
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
void roundOut(SkIRect* dst) const {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(dst);
|
2012-11-06 22:02:51 +00:00
|
|
|
dst->set(SkScalarFloorToInt(fLeft), SkScalarFloorToInt(fTop),
|
|
|
|
SkScalarCeilToInt(fRight), SkScalarCeilToInt(fBottom));
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2011-08-01 20:53:19 +00:00
|
|
|
/**
|
2014-11-19 13:03:18 +00:00
|
|
|
* Set the dst rectangle by rounding "out" this rectangle, choosing the
|
|
|
|
* SkScalarFloorToScalar of top and left, and the SkScalarCeilToScalar of right and bottom.
|
2014-11-19 02:06:45 +00:00
|
|
|
*
|
|
|
|
* It is safe for this == dst
|
2011-08-01 20:53:19 +00:00
|
|
|
*/
|
2014-11-19 02:06:45 +00:00
|
|
|
void roundOut(SkRect* dst) const {
|
|
|
|
dst->set(SkScalarFloorToScalar(fLeft),
|
|
|
|
SkScalarFloorToScalar(fTop),
|
|
|
|
SkScalarCeilToScalar(fRight),
|
|
|
|
SkScalarCeilToScalar(fBottom));
|
2011-08-01 20:53:19 +00:00
|
|
|
}
|
|
|
|
|
2012-11-06 22:02:51 +00:00
|
|
|
/**
|
|
|
|
* 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));
|
|
|
|
}
|
2012-11-08 02:03:56 +00:00
|
|
|
|
2014-11-19 13:03:18 +00:00
|
|
|
//! Returns the result of calling round(&dst)
|
2013-11-07 16:06:53 +00:00
|
|
|
SkIRect round() const {
|
|
|
|
SkIRect ir;
|
|
|
|
this->round(&ir);
|
|
|
|
return ir;
|
|
|
|
}
|
2014-11-19 13:03:18 +00:00
|
|
|
|
|
|
|
//! Returns the result of calling roundOut(&dst)
|
|
|
|
SkIRect roundOut() const {
|
|
|
|
SkIRect ir;
|
|
|
|
this->roundOut(&ir);
|
|
|
|
return ir;
|
|
|
|
}
|
|
|
|
|
2011-05-12 22:31:01 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2014-10-10 12:50:15 +00:00
|
|
|
void sort() {
|
2014-11-03 22:34:30 +00:00
|
|
|
if (fLeft > fRight) {
|
|
|
|
SkTSwap<SkScalar>(fLeft, fRight);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fTop > fBottom) {
|
|
|
|
SkTSwap<SkScalar>(fTop, fBottom);
|
|
|
|
}
|
2014-10-10 12:50:15 +00:00
|
|
|
}
|
2013-03-09 07:01:15 +00:00
|
|
|
|
2013-03-08 16:07:54 +00:00
|
|
|
/**
|
|
|
|
* cast-safe way to treat the rect as an array of (4) SkScalars.
|
|
|
|
*/
|
|
|
|
const SkScalar* asScalars() const { return &fLeft; }
|
2014-06-09 14:59:06 +00:00
|
|
|
|
2014-12-15 15:59:53 +00:00
|
|
|
void dump(bool asHex) const;
|
|
|
|
void dump() const { this->dump(false); }
|
|
|
|
void dumpHex() const { this->dump(true); }
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2015-02-23 22:44:57 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|