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
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/private/SkSafe32.h"
|
|
|
|
#include "include/private/SkTFitsIn.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2020-02-05 18:34:09 +00:00
|
|
|
#include <algorithm>
|
2018-06-18 19:11:00 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2015-02-23 22:44:57 +00:00
|
|
|
struct SkRect;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** \struct SkIRect
|
2018-05-30 13:20:29 +00:00
|
|
|
SkIRect holds four 32-bit integer coordinates describing the upper and
|
2017-10-30 15:48:35 +00:00
|
|
|
lower bounds of a rectangle. SkIRect may be created from outer bounds or
|
|
|
|
from position, width, and height. SkIRect describes an area; if its right
|
|
|
|
is less than or equal to its left, or if its bottom is less than or equal to
|
|
|
|
its top, it is considered empty.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2011-03-15 21:27:08 +00:00
|
|
|
struct SK_API SkIRect {
|
2018-05-30 13:20:29 +00:00
|
|
|
int32_t fLeft; //!< smaller x-axis bounds
|
|
|
|
int32_t fTop; //!< smaller y-axis bounds
|
|
|
|
int32_t fRight; //!< larger x-axis bounds
|
|
|
|
int32_t fBottom; //!< larger y-axis bounds
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns constructed SkIRect set to (0, 0, 0, 0).
|
|
|
|
Many other rectangles are empty; if left is equal to or greater than right,
|
|
|
|
or if top is equal to or greater than bottom. Setting all members to zero
|
|
|
|
is a convenience, but does not designate a special empty rectangle.
|
|
|
|
|
|
|
|
@return bounds (0, 0, 0, 0)
|
|
|
|
*/
|
2017-10-05 15:27:53 +00:00
|
|
|
static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeEmpty() {
|
|
|
|
return SkIRect{0, 0, 0, 0};
|
2010-07-13 18:35:14 +00:00
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns constructed SkIRect set to (0, 0, w, h). Does not validate input; w or h
|
|
|
|
may be negative.
|
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
@param w width of constructed SkIRect
|
|
|
|
@param h height of constructed SkIRect
|
2017-10-30 15:48:35 +00:00
|
|
|
@return bounds (0, 0, w, h)
|
|
|
|
*/
|
2017-10-05 15:27:53 +00:00
|
|
|
static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeWH(int32_t w, int32_t h) {
|
|
|
|
return SkIRect{0, 0, w, h};
|
2010-07-13 18:35:14 +00:00
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns constructed SkIRect set to (0, 0, size.width(), size.height()).
|
|
|
|
Does not validate input; size.width() or size.height() may be negative.
|
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
@param size values for SkIRect width and height
|
2017-10-30 15:48:35 +00:00
|
|
|
@return bounds (0, 0, size.width(), size.height())
|
|
|
|
*/
|
2017-10-05 15:27:53 +00:00
|
|
|
static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeSize(const SkISize& size) {
|
|
|
|
return SkIRect{0, 0, size.fWidth, size.fHeight};
|
2010-07-13 18:35:14 +00:00
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2020-12-16 16:44:47 +00:00
|
|
|
/** Returns constructed SkIRect set to (pt.x(), pt.y(), pt.x() + size.width(),
|
|
|
|
pt.y() + size.height()). Does not validate input; size.width() or size.height() may be
|
|
|
|
negative.
|
|
|
|
|
|
|
|
@param pt values for SkIRect fLeft and fTop
|
|
|
|
@param size values for SkIRect width and height
|
|
|
|
@return bounds at pt with width and height of size
|
|
|
|
*/
|
|
|
|
static constexpr SkIRect SK_WARN_UNUSED_RESULT MakePtSize(SkIPoint pt, SkISize size) {
|
|
|
|
return MakeXYWH(pt.x(), pt.y(), size.width(), size.height());
|
|
|
|
}
|
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
/** Returns constructed SkIRect set to (l, t, r, b). Does not sort input; SkIRect may
|
2017-10-30 15:48:35 +00:00
|
|
|
result in fLeft greater than fRight, or fTop greater than fBottom.
|
|
|
|
|
|
|
|
@param l integer stored in fLeft
|
|
|
|
@param t integer stored in fTop
|
|
|
|
@param r integer stored in fRight
|
|
|
|
@param b integer stored in fBottom
|
|
|
|
@return bounds (l, t, r, b)
|
|
|
|
*/
|
2018-01-30 21:01:33 +00:00
|
|
|
static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeLTRB(int32_t l, int32_t t,
|
|
|
|
int32_t r, int32_t b) {
|
2017-10-05 15:27:53 +00:00
|
|
|
return SkIRect{l, t, r, b};
|
2010-07-13 18:35:14 +00:00
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2018-09-13 18:08:12 +00:00
|
|
|
/** Returns constructed SkIRect set to: (x, y, x + w, y + h).
|
|
|
|
Does not validate input; w or h may be negative.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
@param x stored in fLeft
|
|
|
|
@param y stored in fTop
|
|
|
|
@param w added to x and stored in fRight
|
|
|
|
@param h added to y and stored in fBottom
|
|
|
|
@return bounds at (x, y) with width w and height h
|
|
|
|
*/
|
2018-01-30 21:01:33 +00:00
|
|
|
static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeXYWH(int32_t x, int32_t y,
|
|
|
|
int32_t w, int32_t h) {
|
|
|
|
return { x, y, Sk32_sat_add(x, w), Sk32_sat_add(y, h) };
|
2010-07-13 18:35:14 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns left edge of SkIRect, if sorted.
|
|
|
|
Call sort() to reverse fLeft and fRight if needed.
|
|
|
|
|
|
|
|
@return fLeft
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr int32_t left() const { return fLeft; }
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
/** Returns top edge of SkIRect, if sorted. Call isEmpty() to see if SkIRect may be invalid,
|
|
|
|
and sort() to reverse fTop and fBottom if needed.
|
|
|
|
|
|
|
|
@return fTop
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr int32_t top() const { return fTop; }
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
/** Returns right edge of SkIRect, if sorted.
|
|
|
|
Call sort() to reverse fLeft and fRight if needed.
|
|
|
|
|
|
|
|
@return fRight
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr int32_t right() const { return fRight; }
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
/** Returns bottom edge of SkIRect, if sorted. Call isEmpty() to see if SkIRect may be invalid,
|
|
|
|
and sort() to reverse fTop and fBottom if needed.
|
|
|
|
|
|
|
|
@return fBottom
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr int32_t bottom() const { return fBottom; }
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns left edge of SkIRect, if sorted. Call isEmpty() to see if SkIRect may be invalid,
|
|
|
|
and sort() to reverse fLeft and fRight if needed.
|
|
|
|
|
|
|
|
@return fLeft
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr int32_t x() const { return fLeft; }
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
/** Returns top edge of SkIRect, if sorted. Call isEmpty() to see if SkIRect may be invalid,
|
|
|
|
and sort() to reverse fTop and fBottom if needed.
|
|
|
|
|
|
|
|
@return fTop
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr int32_t y() const { return fTop; }
|
2017-10-30 15:48:35 +00:00
|
|
|
|
2018-12-18 22:38:18 +00:00
|
|
|
// Experimental
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr SkIPoint topLeft() const { return {fLeft, fTop}; }
|
2018-12-18 22:38:18 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns span on the x-axis. This does not check if SkIRect is sorted, or if
|
|
|
|
result fits in 32-bit signed integer; result may be negative.
|
|
|
|
|
|
|
|
@return fRight minus fLeft
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr int32_t width() const { return Sk32_can_overflow_sub(fRight, fLeft); }
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns span on the y-axis. This does not check if SkIRect is sorted, or if
|
|
|
|
result fits in 32-bit signed integer; result may be negative.
|
|
|
|
|
|
|
|
@return fBottom minus fTop
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr int32_t height() const { return Sk32_can_overflow_sub(fBottom, fTop); }
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns spans on the x-axis and y-axis. This does not check if SkIRect is sorted,
|
|
|
|
or if result fits in 32-bit signed integer; result may be negative.
|
|
|
|
|
|
|
|
@return SkISize (width, height)
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr SkISize size() const { return SkISize::Make(this->width(), this->height()); }
|
2014-10-15 14:05:09 +00:00
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
/** Returns span on the x-axis. This does not check if SkIRect is sorted, so the
|
|
|
|
result may be negative. This is safer than calling width() since width() might
|
|
|
|
overflow in its calculation.
|
|
|
|
|
|
|
|
@return fRight minus fLeft cast to int64_t
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr int64_t width64() const { return (int64_t)fRight - (int64_t)fLeft; }
|
2018-02-27 15:22:04 +00:00
|
|
|
|
|
|
|
/** Returns span on the y-axis. This does not check if SkIRect is sorted, so the
|
|
|
|
result may be negative. This is safer than calling height() since height() might
|
|
|
|
overflow in its calculation.
|
|
|
|
|
|
|
|
@return fBottom minus fTop cast to int64_t
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr int64_t height64() const { return (int64_t)fBottom - (int64_t)fTop; }
|
2018-01-09 16:31:53 +00:00
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
/** Returns true if fLeft is equal to or greater than fRight, or if fTop is equal
|
|
|
|
to or greater than fBottom. Call sort() to reverse rectangles with negative
|
|
|
|
width64() or height64().
|
|
|
|
|
|
|
|
@return true if width64() or height64() are zero or negative
|
|
|
|
*/
|
2018-01-10 19:31:18 +00:00
|
|
|
bool isEmpty64() const { return fRight <= fLeft || fBottom <= fTop; }
|
|
|
|
|
2018-09-25 22:10:21 +00:00
|
|
|
/** Returns true if width() or height() are zero or negative.
|
2018-02-27 15:22:04 +00:00
|
|
|
|
|
|
|
@return true if width() or height() are zero or negative
|
|
|
|
*/
|
2018-01-09 16:31:53 +00:00
|
|
|
bool isEmpty() const {
|
|
|
|
int64_t w = this->width64();
|
|
|
|
int64_t h = this->height64();
|
|
|
|
if (w <= 0 || h <= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Return true if either exceeds int32_t
|
2017-09-20 15:56:00 +00:00
|
|
|
return !SkTFitsIn<int32_t>(w | h);
|
2018-01-09 16:31:53 +00:00
|
|
|
}
|
2013-07-24 22:19:24 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns true if all members in a: fLeft, fTop, fRight, and fBottom; are
|
|
|
|
identical to corresponding members in b.
|
|
|
|
|
|
|
|
@param a SkIRect to compare
|
|
|
|
@param b SkIRect to compare
|
|
|
|
@return true if members are equal
|
|
|
|
*/
|
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
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns true if any member in a: fLeft, fTop, fRight, and fBottom; is not
|
|
|
|
identical to the corresponding member in b.
|
|
|
|
|
|
|
|
@param a SkIRect to compare
|
|
|
|
@param b SkIRect to compare
|
|
|
|
@return true if members are not equal
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Sets SkIRect to (0, 0, 0, 0).
|
|
|
|
|
|
|
|
Many other rectangles are empty; if left is equal to or greater than right,
|
|
|
|
or if top is equal to or greater than bottom. Setting all members to zero
|
|
|
|
is a convenience, but does not designate a special empty rectangle.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
void setEmpty() { memset(this, 0, sizeof(*this)); }
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Sets SkIRect to (left, top, right, bottom).
|
|
|
|
left and right are not sorted; left is not necessarily less than right.
|
|
|
|
top and bottom are not sorted; top is not necessarily less than bottom.
|
|
|
|
|
|
|
|
@param left stored in fLeft
|
|
|
|
@param top stored in fTop
|
|
|
|
@param right stored in fRight
|
|
|
|
@param bottom stored in fBottom
|
|
|
|
*/
|
2011-05-09 17:00:02 +00:00
|
|
|
void setLTRB(int32_t left, int32_t top, int32_t right, int32_t bottom) {
|
2019-08-24 23:39:13 +00:00
|
|
|
fLeft = left;
|
|
|
|
fTop = top;
|
|
|
|
fRight = right;
|
|
|
|
fBottom = bottom;
|
2011-05-09 17:00:02 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2018-09-13 18:08:12 +00:00
|
|
|
/** Sets SkIRect to: (x, y, x + width, y + height).
|
|
|
|
Does not validate input; width or height may be negative.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
@param x stored in fLeft
|
|
|
|
@param y stored in fTop
|
|
|
|
@param width added to x and stored in fRight
|
|
|
|
@param height added to y and stored in fBottom
|
|
|
|
*/
|
2011-03-03 13:23:35 +00:00
|
|
|
void setXYWH(int32_t x, int32_t y, int32_t width, int32_t height) {
|
2018-01-30 21:01:33 +00:00
|
|
|
fLeft = x;
|
|
|
|
fTop = y;
|
|
|
|
fRight = Sk32_sat_add(x, width);
|
|
|
|
fBottom = Sk32_sat_add(y, height);
|
2011-03-03 13:23:35 +00:00
|
|
|
}
|
2011-05-09 17:00:02 +00:00
|
|
|
|
2019-08-24 23:39:13 +00:00
|
|
|
void setWH(int32_t width, int32_t height) {
|
|
|
|
fLeft = 0;
|
|
|
|
fTop = 0;
|
|
|
|
fRight = width;
|
|
|
|
fBottom = height;
|
|
|
|
}
|
|
|
|
|
2021-04-16 15:31:58 +00:00
|
|
|
void setSize(SkISize size) {
|
|
|
|
fLeft = 0;
|
|
|
|
fTop = 0;
|
|
|
|
fRight = size.width();
|
|
|
|
fBottom = size.height();
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns SkIRect offset by (dx, dy).
|
|
|
|
|
|
|
|
If dx is negative, SkIRect returned is moved to the left.
|
|
|
|
If dx is positive, SkIRect returned is moved to the right.
|
|
|
|
If dy is negative, SkIRect returned is moved upward.
|
|
|
|
If dy is positive, SkIRect returned is moved downward.
|
|
|
|
|
|
|
|
@param dx offset added to fLeft and fRight
|
|
|
|
@param dy offset added to fTop and fBottom
|
2018-06-14 17:21:08 +00:00
|
|
|
@return SkIRect offset by dx and dy, with original width and height
|
2017-10-30 15:48:35 +00:00
|
|
|
*/
|
2019-10-03 18:57:07 +00:00
|
|
|
constexpr SkIRect makeOffset(int32_t dx, int32_t dy) const {
|
2018-01-30 21:01:33 +00:00
|
|
|
return {
|
|
|
|
Sk32_sat_add(fLeft, dx), Sk32_sat_add(fTop, dy),
|
|
|
|
Sk32_sat_add(fRight, dx), Sk32_sat_add(fBottom, dy),
|
|
|
|
};
|
2014-06-02 21:38:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 18:57:07 +00:00
|
|
|
/** Returns SkIRect offset by (offset.x(), offset.y()).
|
|
|
|
|
|
|
|
If offset.x() is negative, SkIRect returned is moved to the left.
|
|
|
|
If offset.x() is positive, SkIRect returned is moved to the right.
|
|
|
|
If offset.y() is negative, SkIRect returned is moved upward.
|
|
|
|
If offset.y() is positive, SkIRect returned is moved downward.
|
|
|
|
|
|
|
|
@param offset translation vector
|
|
|
|
@return SkIRect translated by offset, with original width and height
|
|
|
|
*/
|
|
|
|
constexpr SkIRect makeOffset(SkIVector offset) const {
|
|
|
|
return this->makeOffset(offset.x(), offset.y());
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns SkIRect, inset by (dx, dy).
|
|
|
|
|
|
|
|
If dx is negative, SkIRect returned is wider.
|
|
|
|
If dx is positive, SkIRect returned is narrower.
|
|
|
|
If dy is negative, SkIRect returned is taller.
|
|
|
|
If dy is positive, SkIRect returned is shorter.
|
|
|
|
|
|
|
|
@param dx offset added to fLeft and subtracted from fRight
|
|
|
|
@param dy offset added to fTop and subtracted from fBottom
|
2018-02-27 15:22:04 +00:00
|
|
|
@return SkIRect inset symmetrically left and right, top and bottom
|
2017-10-30 15:48:35 +00:00
|
|
|
*/
|
2015-03-13 13:08:28 +00:00
|
|
|
SkIRect makeInset(int32_t dx, int32_t dy) const {
|
2018-01-30 21:01:33 +00:00
|
|
|
return {
|
|
|
|
Sk32_sat_add(fLeft, dx), Sk32_sat_add(fTop, dy),
|
|
|
|
Sk32_sat_sub(fRight, dx), Sk32_sat_sub(fBottom, dy),
|
|
|
|
};
|
2014-06-02 21:38:28 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns SkIRect, outset by (dx, dy).
|
|
|
|
|
|
|
|
If dx is negative, SkIRect returned is narrower.
|
|
|
|
If dx is positive, SkIRect returned is wider.
|
|
|
|
If dy is negative, SkIRect returned is shorter.
|
|
|
|
If dy is positive, SkIRect returned is taller.
|
|
|
|
|
|
|
|
@param dx offset subtracted to fLeft and added from fRight
|
|
|
|
@param dy offset subtracted to fTop and added from fBottom
|
2018-02-27 15:22:04 +00:00
|
|
|
@return SkIRect outset symmetrically left and right, top and bottom
|
2017-10-30 15:48:35 +00:00
|
|
|
*/
|
2015-03-13 13:08:28 +00:00
|
|
|
SkIRect makeOutset(int32_t dx, int32_t dy) const {
|
2018-01-30 21:01:33 +00:00
|
|
|
return {
|
|
|
|
Sk32_sat_sub(fLeft, dx), Sk32_sat_sub(fTop, dy),
|
|
|
|
Sk32_sat_add(fRight, dx), Sk32_sat_add(fBottom, dy),
|
|
|
|
};
|
2015-03-13 13:08:28 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Offsets SkIRect by adding dx to fLeft, fRight; and by adding dy to fTop, fBottom.
|
|
|
|
|
|
|
|
If dx is negative, moves SkIRect returned to the left.
|
|
|
|
If dx is positive, moves SkIRect returned to the right.
|
|
|
|
If dy is negative, moves SkIRect returned upward.
|
|
|
|
If dy is positive, moves SkIRect returned downward.
|
|
|
|
|
|
|
|
@param dx offset added to fLeft and fRight
|
|
|
|
@param dy offset added to fTop and fBottom
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
void offset(int32_t dx, int32_t dy) {
|
2018-01-30 21:01:33 +00:00
|
|
|
fLeft = Sk32_sat_add(fLeft, dx);
|
|
|
|
fTop = Sk32_sat_add(fTop, dy);
|
|
|
|
fRight = Sk32_sat_add(fRight, dx);
|
|
|
|
fBottom = Sk32_sat_add(fBottom, dy);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Offsets SkIRect by adding delta.fX to fLeft, fRight; and by adding delta.fY to
|
|
|
|
fTop, fBottom.
|
|
|
|
|
|
|
|
If delta.fX is negative, moves SkIRect returned to the left.
|
|
|
|
If delta.fX is positive, moves SkIRect returned to the right.
|
|
|
|
If delta.fY is negative, moves SkIRect returned upward.
|
|
|
|
If delta.fY is positive, moves SkIRect returned downward.
|
|
|
|
|
|
|
|
@param delta offset added to SkIRect
|
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
void offset(const SkIPoint& delta) {
|
|
|
|
this->offset(delta.fX, delta.fY);
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Offsets SkIRect so that fLeft equals newX, and fTop equals newY. width and height
|
|
|
|
are unchanged.
|
|
|
|
|
|
|
|
@param newX stored in fLeft, preserving width()
|
|
|
|
@param newY stored in fTop, preserving height()
|
|
|
|
*/
|
2012-11-06 22:02:51 +00:00
|
|
|
void offsetTo(int32_t newX, int32_t newY) {
|
2018-01-30 21:01:33 +00:00
|
|
|
fRight = Sk64_pin_to_s32((int64_t)fRight + newX - fLeft);
|
|
|
|
fBottom = Sk64_pin_to_s32((int64_t)fBottom + newY - fTop);
|
|
|
|
fLeft = newX;
|
|
|
|
fTop = newY;
|
2012-11-06 22:02:51 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Insets SkIRect by (dx,dy).
|
|
|
|
|
|
|
|
If dx is positive, makes SkIRect narrower.
|
|
|
|
If dx is negative, makes SkIRect wider.
|
|
|
|
If dy is positive, makes SkIRect shorter.
|
|
|
|
If dy is negative, makes SkIRect taller.
|
|
|
|
|
|
|
|
@param dx offset added to fLeft and subtracted from fRight
|
|
|
|
@param dy offset added to fTop and subtracted from fBottom
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
void inset(int32_t dx, int32_t dy) {
|
2018-01-30 21:01:33 +00:00
|
|
|
fLeft = Sk32_sat_add(fLeft, dx);
|
|
|
|
fTop = Sk32_sat_add(fTop, dy);
|
|
|
|
fRight = Sk32_sat_sub(fRight, dx);
|
|
|
|
fBottom = Sk32_sat_sub(fBottom, dy);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2010-02-19 21:41:30 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Outsets SkIRect by (dx, dy).
|
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
If dx is positive, makes SkIRect wider.
|
|
|
|
If dx is negative, makes SkIRect narrower.
|
|
|
|
If dy is positive, makes SkIRect taller.
|
|
|
|
If dy is negative, makes SkIRect shorter.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
@param dx subtracted to fLeft and added from fRight
|
|
|
|
@param dy subtracted to fTop and added from fBottom
|
2012-05-15 16:47:23 +00:00
|
|
|
*/
|
|
|
|
void outset(int32_t dx, int32_t dy) { this->inset(-dx, -dy); }
|
|
|
|
|
2018-05-30 13:20:29 +00:00
|
|
|
/** Adjusts SkIRect by adding dL to fLeft, dT to fTop, dR to fRight, and dB to fBottom.
|
2018-05-18 14:19:05 +00:00
|
|
|
|
|
|
|
If dL is positive, narrows SkIRect on the left. If negative, widens it on the left.
|
|
|
|
If dT is positive, shrinks SkIRect on the top. If negative, lengthens it on the top.
|
|
|
|
If dR is positive, narrows SkIRect on the right. If negative, widens it on the right.
|
|
|
|
If dB is positive, shrinks SkIRect on the bottom. If negative, lengthens it on the bottom.
|
|
|
|
|
2018-05-30 13:20:29 +00:00
|
|
|
The resulting SkIRect is not checked for validity. Thus, if the resulting SkIRect left is
|
|
|
|
greater than right, the SkIRect will be considered empty. Call sort() after this call
|
|
|
|
if that is not the desired behavior.
|
|
|
|
|
2018-05-18 14:19:05 +00:00
|
|
|
@param dL offset added to fLeft
|
|
|
|
@param dT offset added to fTop
|
|
|
|
@param dR offset added to fRight
|
|
|
|
@param dB offset added to fBottom
|
|
|
|
*/
|
|
|
|
void adjust(int32_t dL, int32_t dT, int32_t dR, int32_t dB) {
|
|
|
|
fLeft = Sk32_sat_add(fLeft, dL);
|
|
|
|
fTop = Sk32_sat_add(fTop, dT);
|
|
|
|
fRight = Sk32_sat_add(fRight, dR);
|
|
|
|
fBottom = Sk32_sat_add(fBottom, dB);
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns true if: fLeft <= x < fRight && fTop <= y < fBottom.
|
2018-02-27 15:22:04 +00:00
|
|
|
Returns false if SkIRect is empty.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
Considers input to describe constructed SkIRect: (x, y, x + 1, y + 1) and
|
|
|
|
returns true if constructed area is completely enclosed by SkIRect area.
|
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
@param x test SkIPoint x-coordinate
|
|
|
|
@param y test SkIPoint y-coordinate
|
2017-10-30 15:48:35 +00:00
|
|
|
@return true if (x, y) is inside SkIRect
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
bool contains(int32_t x, int32_t y) const {
|
2018-02-09 21:16:12 +00:00
|
|
|
return x >= fLeft && x < fRight && y >= fTop && y < fBottom;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
/** Returns true if SkIRect contains r.
|
2019-08-24 23:39:13 +00:00
|
|
|
Returns false if SkIRect is empty or r is empty.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
2019-08-24 23:39:13 +00:00
|
|
|
SkIRect contains r when SkIRect area completely includes r area.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
2019-08-24 23:39:13 +00:00
|
|
|
@param r SkIRect contained
|
|
|
|
@return true if all sides of SkIRect are outside r
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
/** Returns true if SkIRect contains r.
|
|
|
|
Returns false if SkIRect is empty or r is empty.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
SkIRect contains r when SkIRect area completely includes r area.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
@param r SkRect contained
|
|
|
|
@return true if all sides of SkIRect are outside r
|
2015-02-23 22:44:57 +00:00
|
|
|
*/
|
2019-08-24 23:39:13 +00:00
|
|
|
inline bool contains(const SkRect& r) const;
|
2017-10-30 15:48:35 +00:00
|
|
|
|
2019-08-24 23:39:13 +00:00
|
|
|
/** Returns true if SkIRect contains construction.
|
2017-10-30 15:48:35 +00:00
|
|
|
Asserts if SkIRect is empty or construction is empty, and if SK_DEBUG is defined.
|
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
Return is undefined if SkIRect is empty or construction is empty.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
2019-08-24 23:39:13 +00:00
|
|
|
@param r SkIRect contained
|
|
|
|
@return true if all sides of SkIRect are outside r
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2019-08-24 23:39:13 +00:00
|
|
|
bool containsNoEmptyCheck(const SkIRect& r) const {
|
|
|
|
SkASSERT(fLeft < fRight && fTop < fBottom);
|
|
|
|
SkASSERT(r.fLeft < r.fRight && r.fTop < r.fBottom);
|
|
|
|
return fLeft <= r.fLeft && fTop <= r.fTop && fRight >= r.fRight && fBottom >= r.fBottom;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns true if SkIRect intersects r, and sets SkIRect to intersection.
|
|
|
|
Returns false if SkIRect does not intersect r, and leaves SkIRect unchanged.
|
|
|
|
|
|
|
|
Returns false if either r or SkIRect is empty, leaving SkIRect unchanged.
|
|
|
|
|
|
|
|
@param r limit of result
|
2018-02-27 15:22:04 +00:00
|
|
|
@return true if r and SkIRect have area in common
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2019-08-26 14:31:13 +00:00
|
|
|
bool intersect(const SkIRect& r) {
|
|
|
|
return this->intersect(*this, r);
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns true if a intersects b, and sets SkIRect to intersection.
|
2018-02-27 15:22:04 +00:00
|
|
|
Returns false if a does not intersect b, and leaves SkIRect unchanged.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
Returns false if either a or b is empty, leaving SkIRect unchanged.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
@param a SkIRect to intersect
|
|
|
|
@param b SkIRect to intersect
|
|
|
|
@return true if a and b have area in common
|
|
|
|
*/
|
2019-08-26 14:31:13 +00:00
|
|
|
bool SK_WARN_UNUSED_RESULT intersect(const SkIRect& a, const SkIRect& b);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns true if a intersects b.
|
|
|
|
Returns false if either a or b is empty, or do not intersect.
|
|
|
|
|
|
|
|
@param a SkIRect to intersect
|
|
|
|
@param b SkIRect to intersect
|
|
|
|
@return true if a and b have area in common
|
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
static bool Intersects(const SkIRect& a, const SkIRect& b) {
|
2021-06-16 19:28:00 +00:00
|
|
|
return SkIRect{}.intersect(a, b);
|
2012-05-31 18:28:59 +00:00
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
/** Sets SkIRect to the union of itself and r.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
2019-08-24 23:39:13 +00:00
|
|
|
Has no effect if r is empty. Otherwise, if SkIRect is empty, sets SkIRect to r.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
2019-08-24 23:39:13 +00:00
|
|
|
@param r expansion SkIRect
|
2019-03-15 19:47:01 +00:00
|
|
|
|
|
|
|
example: https://fiddle.skia.org/c/@IRect_join_2
|
2019-08-24 23:39:13 +00:00
|
|
|
*/
|
|
|
|
void join(const SkIRect& r);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Swaps fLeft and fRight if fLeft is greater than fRight; and swaps
|
|
|
|
fTop and fBottom if fTop is greater than fBottom. Result may be empty,
|
|
|
|
and width() and height() will be zero or positive.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2017-05-12 15:21:36 +00:00
|
|
|
void sort() {
|
2018-06-18 19:11:00 +00:00
|
|
|
using std::swap;
|
2017-05-12 15:21:36 +00:00
|
|
|
if (fLeft > fRight) {
|
2018-06-18 19:11:00 +00:00
|
|
|
swap(fLeft, fRight);
|
2017-05-12 15:21:36 +00:00
|
|
|
}
|
|
|
|
if (fTop > fBottom) {
|
2018-06-18 19:11:00 +00:00
|
|
|
swap(fTop, fBottom);
|
2017-05-12 15:21:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
/** Returns SkIRect with fLeft and fRight swapped if fLeft is greater than fRight; and
|
2017-10-30 15:48:35 +00:00
|
|
|
with fTop and fBottom swapped if fTop is greater than fBottom. Result may be empty;
|
|
|
|
and width() and height() will be zero or positive.
|
|
|
|
|
|
|
|
@return sorted SkIRect
|
|
|
|
*/
|
2017-05-12 15:21:36 +00:00
|
|
|
SkIRect makeSorted() const {
|
2020-02-06 16:25:43 +00:00
|
|
|
return MakeLTRB(std::min(fLeft, fRight), std::min(fTop, fBottom),
|
|
|
|
std::max(fLeft, fRight), std::max(fTop, fBottom));
|
2017-05-12 15:21:36 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** \struct SkRect
|
2017-10-30 15:48:35 +00:00
|
|
|
SkRect holds four SkScalar coordinates describing the upper and
|
|
|
|
lower bounds of a rectangle. SkRect may be created from outer bounds or
|
|
|
|
from position, width, and height. SkRect describes an area; if its right
|
|
|
|
is less than or equal to its left, or if its bottom is less than or equal to
|
|
|
|
its top, it is considered empty.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2011-03-15 21:27:08 +00:00
|
|
|
struct SK_API SkRect {
|
2018-05-30 13:20:29 +00:00
|
|
|
SkScalar fLeft; //!< smaller x-axis bounds
|
|
|
|
SkScalar fTop; //!< smaller y-axis bounds
|
|
|
|
SkScalar fRight; //!< larger x-axis bounds
|
|
|
|
SkScalar fBottom; //!< larger y-axis bounds
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
/** Returns constructed SkRect set to (0, 0, 0, 0).
|
|
|
|
Many other rectangles are empty; if left is equal to or greater than right,
|
|
|
|
or if top is equal to or greater than bottom. Setting all members to zero
|
|
|
|
is a convenience, but does not designate a special empty rectangle.
|
|
|
|
|
|
|
|
@return bounds (0, 0, 0, 0)
|
|
|
|
*/
|
2016-05-13 16:23:38 +00:00
|
|
|
static constexpr SkRect SK_WARN_UNUSED_RESULT MakeEmpty() {
|
|
|
|
return SkRect{0, 0, 0, 0};
|
2010-04-12 21:21:59 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns constructed SkRect set to SkScalar values (0, 0, w, h). Does not
|
|
|
|
validate input; w or h may be negative.
|
|
|
|
|
|
|
|
Passing integer values may generate a compiler warning since SkRect cannot
|
|
|
|
represent 32-bit integers exactly. Use SkIRect for an exact integer rectangle.
|
|
|
|
|
|
|
|
@param w SkScalar width of constructed SkRect
|
|
|
|
@param h SkScalar height of constructed SkRect
|
|
|
|
@return bounds (0, 0, w, h)
|
|
|
|
*/
|
2017-10-05 15:27:53 +00:00
|
|
|
static constexpr SkRect SK_WARN_UNUSED_RESULT MakeWH(SkScalar w, SkScalar h) {
|
|
|
|
return SkRect{0, 0, w, h};
|
2010-04-12 21:21:59 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns constructed SkRect set to integer values (0, 0, w, h). Does not validate
|
|
|
|
input; w or h may be negative.
|
|
|
|
|
|
|
|
Use to avoid a compiler warning that input may lose precision when stored.
|
|
|
|
Use SkIRect for an exact integer rectangle.
|
|
|
|
|
|
|
|
@param w integer width of constructed SkRect
|
|
|
|
@param h integer height of constructed SkRect
|
|
|
|
@return bounds (0, 0, w, h)
|
|
|
|
*/
|
2014-12-15 21:42:51 +00:00
|
|
|
static SkRect SK_WARN_UNUSED_RESULT MakeIWH(int w, int h) {
|
2019-08-24 23:39:13 +00:00
|
|
|
return {0, 0, SkIntToScalar(w), SkIntToScalar(h)};
|
2014-12-15 21:42:51 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns constructed SkRect set to (0, 0, size.width(), size.height()). Does not
|
|
|
|
validate input; size.width() or size.height() may be negative.
|
|
|
|
|
|
|
|
@param size SkScalar values for SkRect width and height
|
|
|
|
@return bounds (0, 0, size.width(), size.height())
|
|
|
|
*/
|
2017-10-05 15:27:53 +00:00
|
|
|
static constexpr SkRect SK_WARN_UNUSED_RESULT MakeSize(const SkSize& size) {
|
|
|
|
return SkRect{0, 0, size.fWidth, size.fHeight};
|
2010-02-24 01:49:13 +00:00
|
|
|
}
|
2013-01-17 07:06:06 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns constructed SkRect set to (l, t, r, b). Does not sort input; SkRect may
|
|
|
|
result in fLeft greater than fRight, or fTop greater than fBottom.
|
|
|
|
|
|
|
|
@param l SkScalar stored in fLeft
|
|
|
|
@param t SkScalar stored in fTop
|
|
|
|
@param r SkScalar stored in fRight
|
|
|
|
@param b SkScalar stored in fBottom
|
|
|
|
@return bounds (l, t, r, b)
|
|
|
|
*/
|
2016-07-18 14:31:13 +00:00
|
|
|
static constexpr SkRect SK_WARN_UNUSED_RESULT MakeLTRB(SkScalar l, SkScalar t, SkScalar r,
|
|
|
|
SkScalar b) {
|
|
|
|
return SkRect {l, t, r, b};
|
2010-02-24 01:49:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 18:08:12 +00:00
|
|
|
/** Returns constructed SkRect set to (x, y, x + w, y + h).
|
|
|
|
Does not validate input; w or h may be negative.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
@param x stored in fLeft
|
|
|
|
@param y stored in fTop
|
|
|
|
@param w added to x and stored in fRight
|
|
|
|
@param h added to y and stored in fBottom
|
|
|
|
@return bounds at (x, y) with width w and height h
|
|
|
|
*/
|
2018-09-13 15:32:08 +00:00
|
|
|
static constexpr SkRect SK_WARN_UNUSED_RESULT MakeXYWH(SkScalar x, SkScalar y, SkScalar w,
|
|
|
|
SkScalar h) {
|
2017-10-05 15:27:53 +00:00
|
|
|
return SkRect {x, y, x + w, y + h};
|
2012-11-07 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns constructed SkIRect set to (0, 0, size.width(), size.height()).
|
|
|
|
Does not validate input; size.width() or size.height() may be negative.
|
|
|
|
|
|
|
|
@param size integer values for SkRect width and height
|
|
|
|
@return bounds (0, 0, size.width(), size.height())
|
|
|
|
*/
|
2016-09-12 13:32:07 +00:00
|
|
|
static SkRect Make(const SkISize& size) {
|
|
|
|
return MakeIWH(size.width(), size.height());
|
|
|
|
}
|
2017-07-24 12:58:37 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns constructed SkIRect set to irect, promoting integers to scalar.
|
|
|
|
Does not validate input; fLeft may be greater than fRight, fTop may be greater
|
|
|
|
than fBottom.
|
|
|
|
|
|
|
|
@param irect integer unsorted bounds
|
|
|
|
@return irect members converted to SkScalar
|
|
|
|
*/
|
2013-01-23 15:37:56 +00:00
|
|
|
static SkRect SK_WARN_UNUSED_RESULT Make(const SkIRect& irect) {
|
2019-08-24 23:39:13 +00:00
|
|
|
return {
|
|
|
|
SkIntToScalar(irect.fLeft), SkIntToScalar(irect.fTop),
|
|
|
|
SkIntToScalar(irect.fRight), SkIntToScalar(irect.fBottom)
|
|
|
|
};
|
2013-01-23 15:37:56 +00:00
|
|
|
}
|
2013-01-24 07:06:13 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns true if fLeft is equal to or greater than fRight, or if fTop is equal
|
2018-02-27 15:22:04 +00:00
|
|
|
to or greater than fBottom. Call sort() to reverse rectangles with negative
|
|
|
|
width() or height().
|
|
|
|
|
|
|
|
@return true if width() or height() are zero or negative
|
|
|
|
*/
|
2018-02-13 17:48:57 +00:00
|
|
|
bool isEmpty() const {
|
|
|
|
// We write it as the NOT of a non-empty rect, so we will return true if any values
|
|
|
|
// are NaN.
|
|
|
|
return !(fLeft < fRight && fTop < fBottom);
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns true if fLeft is equal to or less than fRight, or if fTop is equal
|
|
|
|
to or less than fBottom. Call sort() to reverse rectangles with negative
|
|
|
|
width() or height().
|
|
|
|
|
|
|
|
@return true if width() or height() are zero or positive
|
|
|
|
*/
|
2017-05-12 15:21:36 +00:00
|
|
|
bool isSorted() const { return fLeft <= fRight && fTop <= fBottom; }
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns true if all values in the rectangle are finite: SK_ScalarMin or larger,
|
|
|
|
and SK_ScalarMax or smaller.
|
|
|
|
|
|
|
|
@return true if no member is infinite or NaN
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns left edge of SkRect, if sorted. Call isSorted() to see if SkRect is valid.
|
|
|
|
Call sort() to reverse fLeft and fRight if needed.
|
|
|
|
|
|
|
|
@return fLeft
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr SkScalar x() const { return fLeft; }
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
/** Returns top edge of SkRect, if sorted. Call isEmpty() to see if SkRect may be invalid,
|
|
|
|
and sort() to reverse fTop and fBottom if needed.
|
|
|
|
|
|
|
|
@return fTop
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr SkScalar y() const { return fTop; }
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
/** Returns left edge of SkRect, if sorted. Call isSorted() to see if SkRect is valid.
|
|
|
|
Call sort() to reverse fLeft and fRight if needed.
|
|
|
|
|
|
|
|
@return fLeft
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr SkScalar left() const { return fLeft; }
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
/** Returns top edge of SkRect, if sorted. Call isEmpty() to see if SkRect may be invalid,
|
|
|
|
and sort() to reverse fTop and fBottom if needed.
|
|
|
|
|
|
|
|
@return fTop
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr SkScalar top() const { return fTop; }
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
/** Returns right edge of SkRect, if sorted. Call isSorted() to see if SkRect is valid.
|
|
|
|
Call sort() to reverse fLeft and fRight if needed.
|
|
|
|
|
|
|
|
@return fRight
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr SkScalar right() const { return fRight; }
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
/** Returns bottom edge of SkRect, if sorted. Call isEmpty() to see if SkRect may be invalid,
|
|
|
|
and sort() to reverse fTop and fBottom if needed.
|
|
|
|
|
|
|
|
@return fBottom
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr SkScalar bottom() const { return fBottom; }
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
/** Returns span on the x-axis. This does not check if SkRect is sorted, or if
|
|
|
|
result fits in 32-bit float; result may be negative or infinity.
|
|
|
|
|
|
|
|
@return fRight minus fLeft
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr SkScalar width() const { return fRight - fLeft; }
|
2017-10-30 15:48:35 +00:00
|
|
|
|
2018-07-13 12:21:59 +00:00
|
|
|
/** Returns span on the y-axis. This does not check if SkRect is sorted, or if
|
2017-10-30 15:48:35 +00:00
|
|
|
result fits in 32-bit float; result may be negative or infinity.
|
|
|
|
|
|
|
|
@return fBottom minus fTop
|
|
|
|
*/
|
2021-12-09 19:59:57 +00:00
|
|
|
constexpr SkScalar height() const { return fBottom - fTop; }
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
/** Returns average of left edge and right edge. Result does not change if SkRect
|
|
|
|
is sorted. Result may overflow to infinity if SkRect is far from the origin.
|
|
|
|
|
2018-10-31 14:54:50 +00:00
|
|
|
@return midpoint on x-axis
|
2017-10-30 15:48:35 +00:00
|
|
|
*/
|
2022-06-03 18:00:12 +00:00
|
|
|
constexpr SkScalar centerX() const {
|
2018-05-24 14:39:57 +00:00
|
|
|
// don't use SkScalarHalf(fLeft + fBottom) as that might overflow before the 0.5
|
|
|
|
return SkScalarHalf(fLeft) + SkScalarHalf(fRight);
|
|
|
|
}
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
/** Returns average of top edge and bottom edge. Result does not change if SkRect
|
2018-07-13 12:21:59 +00:00
|
|
|
is sorted.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
2018-10-31 14:54:50 +00:00
|
|
|
@return midpoint on y-axis
|
2017-10-30 15:48:35 +00:00
|
|
|
*/
|
2022-06-03 18:00:12 +00:00
|
|
|
constexpr SkScalar centerY() const {
|
2018-05-24 14:39:57 +00:00
|
|
|
// don't use SkScalarHalf(fTop + fBottom) as that might overflow before the 0.5
|
|
|
|
return SkScalarHalf(fTop) + SkScalarHalf(fBottom);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2022-06-03 18:00:12 +00:00
|
|
|
/** Returns the point this->centerX(), this->centerY().
|
|
|
|
@return rectangle center
|
|
|
|
*/
|
|
|
|
constexpr SkPoint center() const { return {this->centerX(), this->centerY()}; }
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns true if all members in a: fLeft, fTop, fRight, and fBottom; are
|
|
|
|
equal to the corresponding members in b.
|
|
|
|
|
|
|
|
a and b are not equal if either contain NaN. a and b are equal if members
|
2018-10-31 14:54:50 +00:00
|
|
|
contain zeroes with different signs.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
@param a SkRect to compare
|
|
|
|
@param b SkRect to compare
|
|
|
|
@return true if members are equal
|
|
|
|
*/
|
2011-09-01 15:34:14 +00:00
|
|
|
friend bool operator==(const SkRect& a, const SkRect& b) {
|
2017-07-24 12:58:37 +00:00
|
|
|
return SkScalarsEqual((const SkScalar*)&a, (const SkScalar*)&b, 4);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2010-02-19 21:41:30 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns true if any in a: fLeft, fTop, fRight, and fBottom; does not
|
|
|
|
equal the corresponding members in b.
|
|
|
|
|
|
|
|
a and b are not equal if either contain NaN. a and b are equal if members
|
2018-10-31 14:54:50 +00:00
|
|
|
contain zeroes with different signs.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
@param a SkRect to compare
|
|
|
|
@param b SkRect to compare
|
|
|
|
@return true if members are not equal
|
|
|
|
*/
|
2011-09-01 15:34:14 +00:00
|
|
|
friend bool operator!=(const SkRect& a, const SkRect& b) {
|
2017-07-24 12:58:37 +00:00
|
|
|
return !SkScalarsEqual((const SkScalar*)&a, (const SkScalar*)&b, 4);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns four points in quad that enclose SkRect ordered as: top-left, top-right,
|
|
|
|
bottom-right, bottom-left.
|
2018-11-15 13:25:12 +00:00
|
|
|
|
|
|
|
TODO: Consider adding parameter to control whether quad is clockwise or counterclockwise.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
@param quad storage for corners of SkRect
|
2019-03-15 19:47:01 +00:00
|
|
|
|
|
|
|
example: https://fiddle.skia.org/c/@Rect_toQuad
|
2017-10-30 15:48:35 +00:00
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
void toQuad(SkPoint quad[4]) const;
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Sets SkRect to (0, 0, 0, 0).
|
|
|
|
|
|
|
|
Many other rectangles are empty; if left is equal to or greater than right,
|
|
|
|
or if top is equal to or greater than bottom. Setting all members to zero
|
|
|
|
is a convenience, but does not designate a special empty rectangle.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2016-05-13 16:23:38 +00:00
|
|
|
void setEmpty() { *this = MakeEmpty(); }
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Sets SkRect to src, promoting src members from integer to scalar.
|
|
|
|
Very large values in src may lose precision.
|
|
|
|
|
|
|
|
@param src integer SkRect
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Sets SkRect to (left, top, right, bottom).
|
|
|
|
left and right are not sorted; left is not necessarily less than right.
|
|
|
|
top and bottom are not sorted; top is not necessarily less than bottom.
|
|
|
|
|
|
|
|
@param left stored in fLeft
|
|
|
|
@param top stored in fTop
|
|
|
|
@param right stored in fRight
|
|
|
|
@param bottom stored in fBottom
|
|
|
|
*/
|
2011-05-09 17:00:02 +00:00
|
|
|
void setLTRB(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) {
|
2019-08-24 23:39:13 +00:00
|
|
|
fLeft = left;
|
|
|
|
fTop = top;
|
|
|
|
fRight = right;
|
|
|
|
fBottom = bottom;
|
2011-05-09 17:00:02 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Sets to bounds of SkPoint array with count entries. If count is zero or smaller,
|
2019-08-24 23:39:13 +00:00
|
|
|
or if SkPoint array contains an infinity or NaN, sets to (0, 0, 0, 0).
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
Result is either empty or sorted: fLeft is less than or equal to fRight, and
|
|
|
|
fTop is less than or equal to fBottom.
|
|
|
|
|
|
|
|
@param pts SkPoint array
|
|
|
|
@param count entries in array
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2019-08-24 23:39:13 +00:00
|
|
|
void setBounds(const SkPoint pts[], int count) {
|
|
|
|
(void)this->setBoundsCheck(pts, count);
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Sets to bounds of SkPoint array with count entries. Returns false if count is
|
|
|
|
zero or smaller, or if SkPoint array contains an infinity or NaN; in these cases
|
|
|
|
sets SkRect to (0, 0, 0, 0).
|
|
|
|
|
|
|
|
Result is either empty or sorted: fLeft is less than or equal to fRight, and
|
|
|
|
fTop is less than or equal to fBottom.
|
|
|
|
|
|
|
|
@param pts SkPoint array
|
|
|
|
@param count entries in array
|
|
|
|
@return true if all SkPoint values are finite
|
2019-03-15 19:47:01 +00:00
|
|
|
|
|
|
|
example: https://fiddle.skia.org/c/@Rect_setBoundsCheck
|
2017-10-30 15:48:35 +00:00
|
|
|
*/
|
2012-07-26 15:20:36 +00:00
|
|
|
bool setBoundsCheck(const SkPoint pts[], int count);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2018-05-30 13:20:29 +00:00
|
|
|
/** Sets to bounds of SkPoint pts array with count entries. If any SkPoint in pts
|
|
|
|
contains infinity or NaN, all SkRect dimensions are set to NaN.
|
|
|
|
|
|
|
|
@param pts SkPoint array
|
|
|
|
@param count entries in array
|
2019-03-15 19:47:01 +00:00
|
|
|
|
|
|
|
example: https://fiddle.skia.org/c/@Rect_setBoundsNoCheck
|
2018-05-30 13:20:29 +00:00
|
|
|
*/
|
2018-05-17 01:28:55 +00:00
|
|
|
void setBoundsNoCheck(const SkPoint pts[], int count);
|
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
/** Sets bounds to the smallest SkRect enclosing SkPoint p0 and p1. The result is
|
2017-10-30 15:48:35 +00:00
|
|
|
sorted and may be empty. Does not check to see if values are finite.
|
|
|
|
|
|
|
|
@param p0 corner to include
|
|
|
|
@param p1 corner to include
|
|
|
|
*/
|
2012-05-16 13:35:36 +00:00
|
|
|
void set(const SkPoint& p0, const SkPoint& p1) {
|
2020-02-05 18:34:09 +00:00
|
|
|
fLeft = std::min(p0.fX, p1.fX);
|
|
|
|
fRight = std::max(p0.fX, p1.fX);
|
|
|
|
fTop = std::min(p0.fY, p1.fY);
|
|
|
|
fBottom = std::max(p0.fY, p1.fY);
|
2012-05-16 13:35:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 18:08:12 +00:00
|
|
|
/** Sets SkRect to (x, y, x + width, y + height).
|
|
|
|
Does not validate input; width or height may be negative.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
@param x stored in fLeft
|
|
|
|
@param y stored in fTop
|
|
|
|
@param width added to x and stored in fRight
|
|
|
|
@param height added to y and stored in fBottom
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Sets SkRect to (0, 0, width, height). Does not validate input;
|
|
|
|
width or height may be negative.
|
|
|
|
|
|
|
|
@param width stored in fRight
|
|
|
|
@param height stored in fBottom
|
|
|
|
*/
|
2012-10-11 19:39:09 +00:00
|
|
|
void setWH(SkScalar width, SkScalar height) {
|
|
|
|
fLeft = 0;
|
|
|
|
fTop = 0;
|
|
|
|
fRight = width;
|
|
|
|
fBottom = height;
|
|
|
|
}
|
2019-08-24 23:39:13 +00:00
|
|
|
void setIWH(int32_t width, int32_t height) {
|
|
|
|
this->setWH(SkIntToScalar(width), SkIntToScalar(height));
|
|
|
|
}
|
2012-10-11 19:39:09 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns SkRect offset by (dx, dy).
|
|
|
|
|
|
|
|
If dx is negative, SkRect returned is moved to the left.
|
|
|
|
If dx is positive, SkRect returned is moved to the right.
|
|
|
|
If dy is negative, SkRect returned is moved upward.
|
|
|
|
If dy is positive, SkRect returned is moved downward.
|
|
|
|
|
|
|
|
@param dx added to fLeft and fRight
|
|
|
|
@param dy added to fTop and fBottom
|
2018-06-14 17:21:08 +00:00
|
|
|
@return SkRect offset on axes, with original width and height
|
2017-10-30 15:48:35 +00:00
|
|
|
*/
|
2019-11-22 21:56:36 +00:00
|
|
|
constexpr SkRect makeOffset(SkScalar dx, SkScalar dy) const {
|
2014-06-02 21:38:28 +00:00
|
|
|
return MakeLTRB(fLeft + dx, fTop + dy, fRight + dx, fBottom + dy);
|
|
|
|
}
|
2017-07-24 12:58:37 +00:00
|
|
|
|
2019-11-22 21:56:36 +00:00
|
|
|
/** Returns SkRect offset by v.
|
|
|
|
|
|
|
|
@param v added to rect
|
|
|
|
@return SkRect offset on axes, with original width and height
|
|
|
|
*/
|
|
|
|
constexpr SkRect makeOffset(SkVector v) const { return this->makeOffset(v.x(), v.y()); }
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns SkRect, inset by (dx, dy).
|
|
|
|
|
|
|
|
If dx is negative, SkRect returned is wider.
|
|
|
|
If dx is positive, SkRect returned is narrower.
|
|
|
|
If dy is negative, SkRect returned is taller.
|
|
|
|
If dy is positive, SkRect returned is shorter.
|
|
|
|
|
|
|
|
@param dx added to fLeft and subtracted from fRight
|
|
|
|
@param dy added to fTop and subtracted from fBottom
|
|
|
|
@return SkRect inset symmetrically left and right, top and bottom
|
|
|
|
*/
|
2014-06-02 21:38:28 +00:00
|
|
|
SkRect makeInset(SkScalar dx, SkScalar dy) const {
|
|
|
|
return MakeLTRB(fLeft + dx, fTop + dy, fRight - dx, fBottom - dy);
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns SkRect, outset by (dx, dy).
|
|
|
|
|
|
|
|
If dx is negative, SkRect returned is narrower.
|
|
|
|
If dx is positive, SkRect returned is wider.
|
|
|
|
If dy is negative, SkRect returned is shorter.
|
|
|
|
If dy is positive, SkRect returned is taller.
|
|
|
|
|
|
|
|
@param dx subtracted to fLeft and added from fRight
|
|
|
|
@param dy subtracted to fTop and added from fBottom
|
|
|
|
@return SkRect outset symmetrically left and right, top and bottom
|
|
|
|
*/
|
2015-03-13 13:08:28 +00:00
|
|
|
SkRect makeOutset(SkScalar dx, SkScalar dy) const {
|
|
|
|
return MakeLTRB(fLeft - dx, fTop - dy, fRight + dx, fBottom + dy);
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Offsets SkRect by adding dx to fLeft, fRight; and by adding dy to fTop, fBottom.
|
|
|
|
|
|
|
|
If dx is negative, moves SkRect to the left.
|
|
|
|
If dx is positive, moves SkRect to the right.
|
|
|
|
If dy is negative, moves SkRect upward.
|
|
|
|
If dy is positive, moves SkRect downward.
|
|
|
|
|
|
|
|
@param dx offset added to fLeft and fRight
|
|
|
|
@param dy offset added to fTop and fBottom
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
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
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Offsets SkRect by adding delta.fX to fLeft, fRight; and by adding delta.fY to
|
|
|
|
fTop, fBottom.
|
|
|
|
|
|
|
|
If delta.fX is negative, moves SkRect to the left.
|
|
|
|
If delta.fX is positive, moves SkRect to the right.
|
|
|
|
If delta.fY is negative, moves SkRect upward.
|
|
|
|
If delta.fY is positive, moves SkRect downward.
|
|
|
|
|
|
|
|
@param delta added to SkRect
|
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
void offset(const SkPoint& delta) {
|
|
|
|
this->offset(delta.fX, delta.fY);
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Offsets SkRect so that fLeft equals newX, and fTop equals newY. width and height
|
|
|
|
are unchanged.
|
|
|
|
|
|
|
|
@param newX stored in fLeft, preserving width()
|
|
|
|
@param newY stored in fTop, preserving height()
|
|
|
|
*/
|
2012-11-06 22:02:51 +00:00
|
|
|
void offsetTo(SkScalar newX, SkScalar newY) {
|
|
|
|
fRight += newX - fLeft;
|
|
|
|
fBottom += newY - fTop;
|
|
|
|
fLeft = newX;
|
|
|
|
fTop = newY;
|
|
|
|
}
|
2012-11-08 02:03:56 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Insets SkRect by (dx, dy).
|
|
|
|
|
|
|
|
If dx is positive, makes SkRect narrower.
|
|
|
|
If dx is negative, makes SkRect wider.
|
|
|
|
If dy is positive, makes SkRect shorter.
|
|
|
|
If dy is negative, makes SkRect taller.
|
|
|
|
|
|
|
|
@param dx added to fLeft and subtracted from fRight
|
|
|
|
@param dy added to fTop and subtracted from fBottom
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Outsets SkRect by (dx, dy).
|
|
|
|
|
|
|
|
If dx is positive, makes SkRect wider.
|
|
|
|
If dx is negative, makes SkRect narrower.
|
|
|
|
If dy is positive, makes SkRect taller.
|
|
|
|
If dy is negative, makes SkRect shorter.
|
|
|
|
|
|
|
|
@param dx subtracted to fLeft and added from fRight
|
|
|
|
@param dy subtracted to fTop and added from fBottom
|
2011-08-23 14:39:01 +00:00
|
|
|
*/
|
|
|
|
void outset(SkScalar dx, SkScalar dy) { this->inset(-dx, -dy); }
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns true if SkRect intersects r, and sets SkRect to intersection.
|
|
|
|
Returns false if SkRect does not intersect r, and leaves SkRect unchanged.
|
|
|
|
|
|
|
|
Returns false if either r or SkRect is empty, leaving SkRect unchanged.
|
|
|
|
|
|
|
|
@param r limit of result
|
|
|
|
@return true if r and SkRect have area in common
|
2019-03-15 19:47:01 +00:00
|
|
|
|
|
|
|
example: https://fiddle.skia.org/c/@Rect_intersect
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2017-03-07 19:55:37 +00:00
|
|
|
bool intersect(const SkRect& r);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns true if a intersects b, and sets SkRect to intersection.
|
|
|
|
Returns false if a does not intersect b, and leaves SkRect unchanged.
|
|
|
|
|
|
|
|
Returns false if either a or b is empty, leaving SkRect unchanged.
|
|
|
|
|
|
|
|
@param a SkRect to intersect
|
|
|
|
@param b SkRect to intersect
|
|
|
|
@return true if a and b have area in common
|
|
|
|
*/
|
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) {
|
2020-02-05 18:34:09 +00:00
|
|
|
SkScalar L = std::max(al, bl);
|
|
|
|
SkScalar R = std::min(ar, br);
|
|
|
|
SkScalar T = std::max(at, bt);
|
|
|
|
SkScalar B = std::min(ab, bb);
|
2014-10-09 21:29:01 +00:00
|
|
|
return L < R && T < B;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
/** Returns true if SkRect intersects r.
|
2019-08-22 20:19:50 +00:00
|
|
|
Returns false if either r or SkRect is empty, or do not intersect.
|
2017-10-30 15:48:35 +00:00
|
|
|
|
2019-08-22 20:19:50 +00:00
|
|
|
@param r SkRect to intersect
|
|
|
|
@return true if r and SkRect have area in common
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns true if a intersects b.
|
|
|
|
Returns false if either a or b is empty, or do not intersect.
|
|
|
|
|
|
|
|
@param a SkRect to intersect
|
|
|
|
@param b SkRect to intersect
|
|
|
|
@return true if a and b have area in common
|
|
|
|
*/
|
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
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Sets SkRect to the union of itself and r.
|
|
|
|
|
|
|
|
Has no effect if r is empty. Otherwise, if SkRect is empty, sets
|
|
|
|
SkRect to r.
|
|
|
|
|
|
|
|
@param r expansion SkRect
|
2019-03-15 19:47:01 +00:00
|
|
|
|
|
|
|
example: https://fiddle.skia.org/c/@Rect_join_2
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2019-08-22 20:19:50 +00:00
|
|
|
void join(const SkRect& r);
|
2014-10-01 16:24:12 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Sets SkRect to the union of itself and r.
|
|
|
|
|
|
|
|
Asserts if r is empty and SK_DEBUG is defined.
|
|
|
|
If SkRect is empty, sets SkRect to r.
|
|
|
|
|
|
|
|
May produce incorrect results if r is empty.
|
|
|
|
|
|
|
|
@param r expansion SkRect
|
|
|
|
*/
|
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
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Sets SkRect to the union of itself and the construction.
|
|
|
|
|
|
|
|
May produce incorrect results if SkRect or r is empty.
|
|
|
|
|
|
|
|
@param r expansion SkRect
|
|
|
|
*/
|
2015-05-01 15:51:48 +00:00
|
|
|
void joinPossiblyEmptyRect(const SkRect& r) {
|
2020-02-05 18:34:09 +00:00
|
|
|
fLeft = std::min(fLeft, r.left());
|
|
|
|
fTop = std::min(fTop, r.top());
|
|
|
|
fRight = std::max(fRight, r.right());
|
|
|
|
fBottom = std::max(fBottom, r.bottom());
|
2015-05-01 15:51:48 +00:00
|
|
|
}
|
|
|
|
|
2018-02-20 16:44:43 +00:00
|
|
|
/** Returns true if: fLeft <= x < fRight && fTop <= y < fBottom.
|
|
|
|
Returns false if SkRect is empty.
|
|
|
|
|
|
|
|
@param x test SkPoint x-coordinate
|
|
|
|
@param y test SkPoint y-coordinate
|
|
|
|
@return true if (x, y) is inside SkRect
|
|
|
|
*/
|
|
|
|
bool contains(SkScalar x, SkScalar y) const {
|
|
|
|
return x >= fLeft && x < fRight && y >= fTop && y < fBottom;
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns true if SkRect contains r.
|
|
|
|
Returns false if SkRect is empty or r is empty.
|
|
|
|
|
|
|
|
SkRect contains r when SkRect area completely includes r area.
|
|
|
|
|
|
|
|
@param r SkRect contained
|
|
|
|
@return true if all sides of SkRect are outside r
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns true if SkRect contains r.
|
|
|
|
Returns false if SkRect is empty or r is empty.
|
|
|
|
|
|
|
|
SkRect contains r when SkRect area completely includes r area.
|
|
|
|
|
|
|
|
@param r SkIRect contained
|
|
|
|
@return true if all sides of SkRect are outside r
|
|
|
|
*/
|
2015-02-25 21:19:48 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Sets SkIRect by adding 0.5 and discarding the fractional portion of SkRect
|
|
|
|
members, using (SkScalarRoundToInt(fLeft), SkScalarRoundToInt(fTop),
|
|
|
|
SkScalarRoundToInt(fRight), SkScalarRoundToInt(fBottom)).
|
|
|
|
|
|
|
|
@param dst storage for SkIRect
|
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
void round(SkIRect* dst) const {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(dst);
|
2019-08-24 23:39:13 +00:00
|
|
|
dst->setLTRB(SkScalarRoundToInt(fLeft), SkScalarRoundToInt(fTop),
|
|
|
|
SkScalarRoundToInt(fRight), SkScalarRoundToInt(fBottom));
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 18:08:12 +00:00
|
|
|
/** Sets SkIRect by discarding the fractional portion of fLeft and fTop; and rounding
|
|
|
|
up fRight and fBottom, using
|
|
|
|
(SkScalarFloorToInt(fLeft), SkScalarFloorToInt(fTop),
|
|
|
|
SkScalarCeilToInt(fRight), SkScalarCeilToInt(fBottom)).
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
@param dst storage for SkIRect
|
|
|
|
*/
|
2010-02-19 21:41:30 +00:00
|
|
|
void roundOut(SkIRect* dst) const {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(dst);
|
2019-08-24 23:39:13 +00:00
|
|
|
dst->setLTRB(SkScalarFloorToInt(fLeft), SkScalarFloorToInt(fTop),
|
|
|
|
SkScalarCeilToInt(fRight), SkScalarCeilToInt(fBottom));
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 18:08:12 +00:00
|
|
|
/** Sets SkRect by discarding the fractional portion of fLeft and fTop; and rounding
|
|
|
|
up fRight and fBottom, using
|
|
|
|
(SkScalarFloorToInt(fLeft), SkScalarFloorToInt(fTop),
|
|
|
|
SkScalarCeilToInt(fRight), SkScalarCeilToInt(fBottom)).
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
@param dst storage for SkRect
|
|
|
|
*/
|
2014-11-19 02:06:45 +00:00
|
|
|
void roundOut(SkRect* dst) const {
|
2019-08-24 23:39:13 +00:00
|
|
|
dst->setLTRB(SkScalarFloorToScalar(fLeft), SkScalarFloorToScalar(fTop),
|
|
|
|
SkScalarCeilToScalar(fRight), SkScalarCeilToScalar(fBottom));
|
2011-08-01 20:53:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 18:08:12 +00:00
|
|
|
/** Sets SkRect by rounding up fLeft and fTop; and discarding the fractional portion
|
|
|
|
of fRight and fBottom, using
|
2017-10-30 15:48:35 +00:00
|
|
|
(SkScalarCeilToInt(fLeft), SkScalarCeilToInt(fTop),
|
|
|
|
SkScalarFloorToInt(fRight), SkScalarFloorToInt(fBottom)).
|
|
|
|
|
|
|
|
@param dst storage for SkIRect
|
|
|
|
*/
|
2012-11-06 22:02:51 +00:00
|
|
|
void roundIn(SkIRect* dst) const {
|
|
|
|
SkASSERT(dst);
|
2019-08-24 23:39:13 +00:00
|
|
|
dst->setLTRB(SkScalarCeilToInt(fLeft), SkScalarCeilToInt(fTop),
|
|
|
|
SkScalarFloorToInt(fRight), SkScalarFloorToInt(fBottom));
|
2012-11-06 22:02:51 +00:00
|
|
|
}
|
2012-11-08 02:03:56 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns SkIRect by adding 0.5 and discarding the fractional portion of SkRect
|
|
|
|
members, using (SkScalarRoundToInt(fLeft), SkScalarRoundToInt(fTop),
|
|
|
|
SkScalarRoundToInt(fRight), SkScalarRoundToInt(fBottom)).
|
|
|
|
|
|
|
|
@return rounded SkIRect
|
|
|
|
*/
|
2013-11-07 16:06:53 +00:00
|
|
|
SkIRect round() const {
|
|
|
|
SkIRect ir;
|
|
|
|
this->round(&ir);
|
|
|
|
return ir;
|
|
|
|
}
|
2017-07-24 12:58:37 +00:00
|
|
|
|
2018-09-13 18:08:12 +00:00
|
|
|
/** Sets SkIRect by discarding the fractional portion of fLeft and fTop; and rounding
|
|
|
|
up fRight and fBottom, using
|
|
|
|
(SkScalarFloorToInt(fLeft), SkScalarFloorToInt(fTop),
|
|
|
|
SkScalarCeilToInt(fRight), SkScalarCeilToInt(fBottom)).
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
@return rounded SkIRect
|
|
|
|
*/
|
2014-11-19 13:03:18 +00:00
|
|
|
SkIRect roundOut() const {
|
|
|
|
SkIRect ir;
|
|
|
|
this->roundOut(&ir);
|
|
|
|
return ir;
|
|
|
|
}
|
2021-08-24 00:13:23 +00:00
|
|
|
/** Sets SkIRect by rounding up fLeft and fTop; and discarding the fractional portion
|
|
|
|
of fRight and fBottom, using
|
|
|
|
(SkScalarCeilToInt(fLeft), SkScalarCeilToInt(fTop),
|
|
|
|
SkScalarFloorToInt(fRight), SkScalarFloorToInt(fBottom)).
|
|
|
|
|
|
|
|
@return rounded SkIRect
|
|
|
|
*/
|
|
|
|
SkIRect roundIn() const {
|
|
|
|
SkIRect ir;
|
|
|
|
this->roundIn(&ir);
|
|
|
|
return ir;
|
|
|
|
}
|
2017-07-24 12:58:37 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Swaps fLeft and fRight if fLeft is greater than fRight; and swaps
|
|
|
|
fTop and fBottom if fTop is greater than fBottom. Result may be empty;
|
|
|
|
and width() and height() will be zero or positive.
|
|
|
|
*/
|
2014-10-10 12:50:15 +00:00
|
|
|
void sort() {
|
2018-06-18 19:11:00 +00:00
|
|
|
using std::swap;
|
2014-11-03 22:34:30 +00:00
|
|
|
if (fLeft > fRight) {
|
2018-06-18 19:11:00 +00:00
|
|
|
swap(fLeft, fRight);
|
2014-11-03 22:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fTop > fBottom) {
|
2018-06-18 19:11:00 +00:00
|
|
|
swap(fTop, fBottom);
|
2014-11-03 22:34:30 +00:00
|
|
|
}
|
2014-10-10 12:50:15 +00:00
|
|
|
}
|
2013-03-09 07:01:15 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns SkRect with fLeft and fRight swapped if fLeft is greater than fRight; and
|
|
|
|
with fTop and fBottom swapped if fTop is greater than fBottom. Result may be empty;
|
|
|
|
and width() and height() will be zero or positive.
|
|
|
|
|
|
|
|
@return sorted SkRect
|
|
|
|
*/
|
2017-05-12 15:21:36 +00:00
|
|
|
SkRect makeSorted() const {
|
2020-02-05 18:34:09 +00:00
|
|
|
return MakeLTRB(std::min(fLeft, fRight), std::min(fTop, fBottom),
|
|
|
|
std::max(fLeft, fRight), std::max(fTop, fBottom));
|
2017-05-12 15:21:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Returns pointer to first scalar in SkRect, to treat it as an array with four
|
|
|
|
entries.
|
|
|
|
|
|
|
|
@return pointer to fLeft
|
|
|
|
*/
|
2013-03-08 16:07:54 +00:00
|
|
|
const SkScalar* asScalars() const { return &fLeft; }
|
2014-06-09 14:59:06 +00:00
|
|
|
|
2017-10-30 15:48:35 +00:00
|
|
|
/** Writes text representation of SkRect to standard output. Set asHex to true to
|
|
|
|
generate exact binary representations of floating point numbers.
|
|
|
|
|
|
|
|
@param asHex true if SkScalar values are written as hexadecimal
|
2019-03-15 19:47:01 +00:00
|
|
|
|
|
|
|
example: https://fiddle.skia.org/c/@Rect_dump
|
2017-10-30 15:48:35 +00:00
|
|
|
*/
|
2014-12-15 15:59:53 +00:00
|
|
|
void dump(bool asHex) const;
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
/** Writes text representation of SkRect to standard output. The representation may be
|
|
|
|
directly compiled as C++ code. Floating point values are written
|
|
|
|
with limited precision; it may not be possible to reconstruct original SkRect
|
|
|
|
from output.
|
|
|
|
*/
|
2014-12-15 15:59:53 +00:00
|
|
|
void dump() const { this->dump(false); }
|
2017-10-30 15:48:35 +00:00
|
|
|
|
|
|
|
/** Writes text representation of SkRect to standard output. The representation may be
|
|
|
|
directly compiled as C++ code. Floating point values are written
|
|
|
|
in hexadecimal to preserve their exact bit pattern. The output reconstructs the
|
|
|
|
original SkRect.
|
|
|
|
|
|
|
|
Use instead of dump() when submitting
|
|
|
|
*/
|
2014-12-15 15:59:53 +00:00
|
|
|
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
|