# SkIRect Reference

# IRect

# Struct SkIRect

SkIRect holds four 32 bit integer coordinates describing the upper and 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.

# Overview

## Subtopics

topics | description |
---|

## Operators

description | function |
---|---|

bool operator!=(const SkIRect& a, const SkIRect& b) | Returns true if members are unequal. |

bool operator==(const SkIRect& a, const SkIRect& b) | Returns true if members are equal. |

## Member Functions

description | function |
---|---|

EmptyIRect | Returns immutable bounds of (0, 0, 0, 0). |

Intersects | Returns true if areas overlap. |

IntersectsNoEmptyCheck | Returns true if areas overlap. Skips empty check. |

MakeEmpty | Returns bounds of (0, 0, 0, 0). |

MakeLTRB | Constructs from int left, top, right, bottom. |

MakeLargest | Constructs from (SK MinS32, SK MinS32, SK MaxS32, SK MaxS32). |

MakeSize | Constructs from ISize returning (0, 0, width, height). |

MakeWH | Constructs from int input returning (0, 0, width, height). |

MakeXYWH | Constructs from int input returning (x, y, width, height). |

bottom | Returns larger bounds in y, if sorted. |

centerX | Returns midpoint in x. |

centerY | Returns midpoint in y. |

contains | Returns true if points are equal or inside. |

containsNoEmptyCheck | Returns true if points are equal or inside. Skips empty check. |

height | Returns span in y. |

inset | Moves the sides symmetrically about the center. |

intersect | Sets to shared area; returns true if not empty. |

intersectNoEmptyCheck | Sets to shared area; returns true if not empty. Skips empty check. |

is16Bit | Returns true if members fit in 16-bit word. |

isEmpty | Returns true if width or height are zero or negative. |

isLargest | Returns true if equal to (SK MinS32, SK MinS32, SK MaxS32, SK MaxS32). |

join | Sets to union of bounds. |

left | Returns smaller bounds in x, if sorted. |

makeInset | Constructs from sides moved symmetrically about the center. |

makeOffset | Constructs from translated sides. |

makeOutset | Constructs from sides moved symmetrically about the center. |

makeSorted | Constructs, ordering sides from smaller to larger. |

offset | Translates sides without changing width and height. |

offsetTo | Translates to (x, y) without changing width and height. |

outset | Moves the sides symmetrically about the center. |

quickReject | Returns true if rectangles do not intersect. |

right | Returns larger bounds in x, if sorted. |

set | Sets to (left, top, right, bottom). |

setEmpty | Sets to (0, 0, 0, 0). |

setLTRB | Sets to SkScalar input (left, top, right, bottom). |

setLargest | Sets to (SK MinS32, SK MinS32, SK MaxS32, SK MaxS32). |

setLargestInverted | Sets to (SK MaxS32, SK MaxS32, SK MinS32, SK MinS32). |

setXYWH | Sets to (x, y, width, height). |

size | Returns ISize (width, height). |

sort | Orders sides from smaller to larger. |

top | Returns smaller bounds in y, if sorted. |

width | Returns span in x. |

x | Returns bounds left. |

y | Returns bounds top. |

May contain any value. The smaller of the horizontal values when sorted. When equal to or greater than fRight, IRect is empty.

May contain any value. The smaller of the horizontal values when sorted. When equal to or greater than fBottom, IRect is empty.

May contain any value. The larger of the vertical values when sorted. When equal to or less than fLeft, IRect is empty.

May contain any value. The larger of the vertical values when sorted. When equal to or less than fTop, IRect is empty.

## MakeEmpty

static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeEmpty()

Returns constructed IRect 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 Value

bounds (0, 0, 0, 0)

### Example

### See Also

EmptyIRect isEmpty setEmpty setLargestInverted SkRect::MakeEmpty

## MakeLargest

static SkIRect SK_WARN_UNUSED_RESULT MakeLargest()

Returns constructed IRect setting left and top to most negative value, and setting right and bottom to most positive value.

### Return Value

bounds (SK MinS32, SK MinS32, SK MaxS32, SK MaxS32)

### Example

### See Also

isLargest setLargest SkRect::MakeLargest

## MakeWH

static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeWH(int32_t w, int32_t h)

Returns constructed IRect set to (0, 0, w, h). Does not validate input; w or h may be negative.

### Parameters

| width of constructed Rect |

| height of constructed Rect |

### Return Value

### Example

### See Also

MakeSize MakeXYWH SkRect::MakeWH SkRect::MakeIWH

## MakeSize

static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeSize(const SkISize& size)

Returns constructed IRect set to (0, 0, size.width, size.height). Does not validate input; size.width or size.height may be negative.

### Parameters

| values for Rect width and height |

### Return Value

bounds (0, 0, size.width, size.height)

### Example

### See Also

MakeWH MakeXYWH SkRect::Make SkRect::MakeIWH

## MakeLTRB

static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeLTRB(int32_t l, int32_t t, int32_t r, int32_t b)

Returns constructed IRect set to (l, t, r, b). Does not sort input; Rect may result in fLeft greater than fRight, or fTop greater than fBottom.

### Parameters

| integer stored in fLeft |

| integer stored in fTop |

| integer stored in fRight |

| integer stored in fBottom |

### Return Value

### Example

### See Also

## MakeXYWH

static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)

Returns constructed IRect set to: (x, y, x + w, y + h). Does not validate input; w or h may be negative.

### Parameters

| stored in fLeft |

| stored in fTop |

| added to x and stored in fRight |

| added to y and stored in fBottom |

### Return Value

bounds at (x, y) with width w and height h

### Example

### See Also

## left

int left() const

Returns left edge of IRect, if sorted. Call sort to reverse fLeft and fRight if needed.

### Return Value

### Example

### See Also

## top

int top() const

Returns top edge of IRect, if sorted. Call isEmpty to see if IRect may be invalid, and sort to reverse fTop and fBottom if needed.

### Return Value

### Example

### See Also

## right

int right() const

Returns right edge of IRect, if sorted. Call sort to reverse fLeft and fRight if needed.

### Return Value

### Example

### See Also

## bottom

int bottom() const

Returns bottom edge of IRect, if sorted. Call isEmpty to see if IRect may be invalid, and sort to reverse fTop and fBottom if needed.

### Return Value

### Example

### See Also

## x

int x() const

Returns left edge of IRect, if sorted. Call isEmpty to see if IRect may be invalid, and sort to reverse fLeft and fRight if needed.

### Return Value

### Example

### See Also

## y

int y() const

Returns top edge of IRect, if sorted. Call isEmpty to see if IRect may be invalid, and sort to reverse fTop and fBottom if needed.

### Return Value

### Example

### See Also

## width

int width() const

Returns span on the x-axis. This does not check if IRect is sorted, or if result fits in 32-bit signed integer; result may be negative.

### Return Value

### Example

### See Also

## height

int height() const

Returns span on the y-axis. This does not check if IRect is sorted, or if result fits in 32-bit signed integer; result may be negative.

### Return Value

### Example

### See Also

## size

SkISize size() const

Returns spans on the x-axis and y-axis. This does not check if IRect is sorted, or if result fits in 32-bit signed integer; result may be negative.

### Return Value

### Example

### See Also

## centerX

int centerX() const

Returns average of left edge and right edge. Result does not change if Rect is sorted. Result may be incorrect if Rect is far from the origin.

Result is rounded down.

### Return Value

midpoint in x

### Example

#### Example Output

```
left: 20 right: 41 centerX: 30 div2: 30
left: -20 right: -41 centerX: -31 div2: -30
left: -10 right: 11 centerX: 0 div2: 0
```

### See Also

## centerY

int centerY() const

Returns average of top edge and bottom edge. Result does not change if Rect is sorted. Result may be incorrect if Rect is far from the origin.

Result is rounded down.

### Return Value

midpoint in y

### Example

### See Also

## isEmpty

bool isEmpty() const

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 width or height.

### Return Value

true if width or height are zero or negative

### Example

### See Also

EmptyIRect MakeEmpty sort SkRect::isEmpty

## isLargest

bool isLargest() const

Returns true if IRect encloses largest possible area.

### Return Value

true if equal to (SK MinS32, SK MinS32, SK MaxS32, SK MaxS32)

### Example

#### Example Output

```
large is largest: true
large width -2
large is empty: false
```

### See Also

## operator==

bool operator==(const SkIRect& a, const SkIRect& b)

Returns true if all members in a: fLeft, fTop, fRight, and fBottom; are identical to corresponding members in b.

### Parameters

| IRect to compare |

| IRect to compare |

### Return Value

true if members are equal

### Example

### See Also

operator!=(const SkIRect& a, const SkIRect& b)

## operator!=

bool operator!=(const SkIRect& a, const SkIRect& b)

Returns true if any member in a: fLeft, fTop, fRight, and fBottom; is not identical to the corresponding member in b.

### Parameters

| IRect to compare |

| IRect to compare |

### Return Value

true if members are not equal

### Example

### See Also

operator==(const SkIRect& a, const SkIRect& b)

## is16Bit

bool is16Bit() const

Returns true if all members: fLeft, fTop, fRight, and fBottom; values are equal to or larger than -32768 and equal to or smaller than 32767.

### Return Value

true if members fit in 16-bit word

### Example

### See Also

## setEmpty

void setEmpty()

Sets IRect 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.

### Example

### See Also

## set

void set(int32_t left, int32_t top, int32_t right, int32_t bottom)

Sets IRect 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.

### Parameters

| assigned to fLeft |

| assigned to fTop |

| assigned to fRight |

| assigned to fBottom |

### Example

### See Also

## setLTRB

void setLTRB(int32_t left, int32_t top, int32_t right, int32_t bottom)

Sets IRect 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.

### Parameters

| stored in fLeft |

| stored in fTop |

| stored in fRight |

| stored in fBottom |

### Example

### See Also

## setXYWH

void setXYWH(int32_t x, int32_t y, int32_t width, int32_t height)

Sets IRect to: (x, y, x + width, y + height). Does not validate input; width or height may be negative.

### Parameters

| stored in fLeft |

| stored in fTop |

| added to x and stored in fRight |

| added to y and stored in fBottom |

### Example

### See Also

MakeXYWH setLTRB set SkRect::setXYWH

## setLargest

void setLargest()

Sets rectangle left and top to most negative value, and sets right and bottom to most positive value.

### Example

### See Also

MakeLargest isLargest setLargestInverted SK MinS32 SK MaxS32

## setLargestInverted

void setLargestInverted()

Sets rectangle left and top to most positive value, and sets right and bottom to most negative value. This is used internally to flag that a condition is met, but otherwise has no special purpose.

### See Also

## makeOffset

SkIRect makeOffset(int32_t dx, int32_t dy) const

Returns IRect offset by (dx, dy).

If dx is negative, IRect returned is moved to the left. If dx is positive, IRect returned is moved to the right. If dy is negative, IRect returned is moved upward. If dy is positive, IRect returned is moved downward.

### Parameters

| offset added to fLeft and fRight |

| offset added to fTop and fBottom |

### Return Value

Rect offset in x or y, with original width and height

### Example

### See Also

offset makeInset makeOutset SkRect::makeOffset

## makeInset

SkIRect makeInset(int32_t dx, int32_t dy) const

Returns IRect, inset by (dx, dy).

If dx is negative, IRect returned is wider. If dx is positive, IRect returned is narrower. If dy is negative, IRect returned is taller. If dy is positive, IRect returned is shorter.

### Parameters

| offset added to fLeft and subtracted from fRight |

| offset added to fTop and subtracted from fBottom |

### Return Value

Rect inset symmetrically left and right, top and bottom

### Example

### See Also

inset makeOffset makeOutset SkRect::makeInset

## makeOutset

SkIRect makeOutset(int32_t dx, int32_t dy) const

Returns IRect, outset by (dx, dy).

If dx is negative, IRect returned is narrower. If dx is positive, IRect returned is wider. If dy is negative, IRect returned is shorter. If dy is positive, IRect returned is taller.

### Parameters

| offset subtracted to fLeft and added from fRight |

| offset subtracted to fTop and added from fBottom |

### Return Value

Rect outset symmetrically left and right, top and bottom

### Example

### See Also

outset makeOffset makeInset SkRect::makeOutset

## offset

void offset(int32_t dx, int32_t dy)

Offsets IRect by adding dx to fLeft, fRight; and by adding dy to fTop, fBottom.

If dx is negative, moves IRect returned to the left. If dx is positive, moves IRect returned to the right. If dy is negative, moves IRect returned upward. If dy is positive, moves IRect returned downward.

### Parameters

| offset added to fLeft and fRight |

| offset added to fTop and fBottom |

### Example

### See Also

offsetTo makeOffset SkRect::offset

void offset(const SkIPoint& delta)

Offsets IRect by adding delta.fX to fLeft, fRight; and by adding delta.fY to fTop, fBottom.

If delta.fX is negative, moves IRect returned to the left. If delta.fX is positive, moves IRect returned to the right. If delta.fY is negative, moves IRect returned upward. If delta.fY is positive, moves IRect returned downward.

### Parameters

| offset added to IRect |

### Example

### See Also

offsetTo makeOffset SkRect::offset

## offsetTo

void offsetTo(int32_t newX, int32_t newY)

Offsets IRect so that fLeft equals newX, and fTop equals newY. width and height are unchanged.

### Parameters

| stored in fLeft, preserving width |

| stored in fTop, preserving height |

### Example

### See Also

offset makeOffset setXYWH SkRect::offsetTo

## inset

void inset(int32_t dx, int32_t dy)

If dx is positive, makes IRect narrower. If dx is negative, makes IRect wider. If dy is positive, makes IRect shorter. If dy is negative, makes IRect taller.

### Parameters

| offset added to fLeft and subtracted from fRight |

| offset added to fTop and subtracted from fBottom |

### Example

### See Also

outset makeInset SkRect::inset

## outset

void outset(int32_t dx, int32_t dy)

If dx is positive, makes Rect wider. If dx is negative, makes Rect narrower. If dy is positive, makes Rect taller. If dy is negative, makes Rect shorter.

### Parameters

| subtracted to fLeft and added from fRight |

| subtracted to fTop and added from fBottom |

### Example

### See Also

inset makeOutset SkRect::outset

## quickReject

bool quickReject(int l, int t, int r, int b) const

Constructs IRect (l, t, r, b) and returns true if constructed IRect does not intersect IRect. Does not check to see if construction or IRect is empty.

Is implemented with short circuit logic so that true can be returned after a single compare.

### Parameters

| x minimum of constructed Rect |

| y minimum of constructed Rect |

| x maximum of constructed Rect |

| y maximum of constructed Rect |

### Return Value

true if construction and IRect have no area in common

### Example

#### Example Output

```
rect (7, 11, 13, 17) test(13, 11, 15, 17) quickReject true; intersects false
rect (7, 11, 13, 17) test(7, 7, 13, 11) quickReject true; intersects false
rect (7, 11, 13, 17) test(12, 16, 14, 18) quickReject false; intersects true
```

### See Also

## contains

bool contains(int32_t x, int32_t y) const

Returns true if: fLeft <= x < fRight && fTop <= y < fBottom.

Returns false if Rect is empty.

Considers input to describe constructed IRect: (x, y, x + 1, y + 1)and returns true if constructed area is completely enclosed by IRect area.

### Parameters

| test Point x-coordinate |

| test Point y-coordinate |

### Return Value

true if (x, y) is inside IRect

### Example

### See Also

containsNoEmptyCheck SkRect::contains

bool contains(int32_t left, int32_t top, int32_t right, int32_t bottom) const

Constructs Rect to intersect from (left, top, right, bottom). Does not sort construction.

Returns true if Rect contains construction. Returns false if Rect is empty or construction is empty.

### Parameters

| x minimum of constructed Rect |

| y minimum of constructed Rect |

| x maximum of constructed Rect |

| y maximum of constructed Rect |

### Return Value

true if all sides of IRect are outside construction

### Example

### See Also

containsNoEmptyCheck SkRect::contains

bool contains(const SkIRect& r) const

Returns true if Rect contains r. Returns false if Rect is empty or r is empty.

Rect contains r when Rect area completely includes r area.

### Parameters

| IRect contained |

### Return Value

true if all sides of IRect are outside r

### Example

### See Also

containsNoEmptyCheck SkRect::contains

bool contains(const SkRect& r) const

Returns true if Rect contains r. Returns false if Rect is empty or r is empty.

Rect contains r when Rect area completely includes r area.

### Parameters

| Rect contained |

### Return Value

true if all sides of IRect are outside r

### Example

### See Also

containsNoEmptyCheck SkRect::contains

## containsNoEmptyCheck

bool containsNoEmptyCheck(int32_t left, int32_t top, int32_t right, int32_t bottom) const

Constructs IRect from (left, top, right, bottom). Does not sort construction.

Returns true if Rect contains construction. Asserts if IRect is empty or construction is empty, and if SK DEBUG is defined.

Return is undefined if Rect is empty or construction is empty.

### Parameters

| x minimum of constructed Rect |

| y minimum of constructed Rect |

| x maximum of constructed Rect |

| y maximum of constructed Rect |

### Return Value

true if all sides of IRect are outside construction

### Example

### See Also

bool containsNoEmptyCheck(const SkIRect& r) const

Returns true if Rect contains construction. Asserts if IRect is empty or construction is empty, and if SK DEBUG is defined.

Return is undefined if Rect is empty or construction is empty.

### Parameters

| Rect contained |

### Return Value

true if all sides of IRect are outside r

### Example

### See Also

# Intersection

IRects intersect when they enclose a common area. To intersect, each of the pair must describe area; fLeft is less than fRight, and fTop is less than fBottom; empty() returns false. The intersection of IRect pair can be described by: (max(a.fLeft, b.fLeft), max(a.fTop, b.fTop), min(a.fRight, b.fRight), min(a.fBottom, b.fBottom))The intersection is only meaningful if the resulting IRect is not empty and describes an area: fLeft is less than fRight, and fTop is less than fBottom.

## intersect

bool intersect(const SkIRect& r)

Returns true if IRect intersects r, and sets IRect to intersection. Returns false if IRect does not intersect r, and leaves IRect unchanged.

Returns false if either r or IRect is empty, leaving IRect unchanged.

### Parameters

| limit of result |

### Return Value

true if r and Rect have area in common

### Example

#### Example Output

```
intersection: 30, 60, 50, 80
```

### See Also

Intersects intersectNoEmptyCheck join SkRect::intersect

bool SK_WARN_UNUSED_RESULT intersect(const SkIRect& a, const SkIRect& b)

Returns true if a intersects b, and sets IRect to intersection. Returns false if a does not intersect b, and leaves IRect unchanged.

Returns false if either a or b is empty, leaving IRect unchanged.

### Parameters

| IRect to intersect |

| IRect to intersect |

### Return Value

true if a and b have area in common

### Example

### See Also

Intersects intersectNoEmptyCheck join SkRect::intersect

## intersectNoEmptyCheck

bool SK_WARN_UNUSED_RESULT intersectNoEmptyCheck(const SkIRect& a, const SkIRect& b)

Returns true if a intersects b, and sets IRect to intersection. Returns false if a does not intersect b, and leaves IRect unchanged.

Asserts if either a or b is empty, and if SK DEBUG is defined.

### Parameters

| IRect to intersect |

| IRect to intersect |

### Return Value

true if a and b have area in common

### Example

### See Also

Intersects intersect join SkRect::intersect

bool intersect(int32_t left, int32_t top, int32_t right, int32_t bottom)

Constructs IRect to intersect from (left, top, right, bottom). Does not sort construction.

Returns true if IRect intersects construction, and sets IRect to intersection. Returns false if IRect does not intersect construction, and leaves IRect unchanged.

Returns false if either construction or IRect is empty, leaving IRect unchanged.

### Parameters

| x minimum of constructed IRect |

| y minimum of constructed IRect |

| x maximum of constructed IRect |

| y maximum of constructed IRect |

### Return Value

true if construction and IRect have area in common

### Example

#### Example Output

```
intersection: 30, 60, 50, 80
```

### See Also

intersectNoEmptyCheck Intersects join SkRect::intersect

## Intersects

static bool Intersects(const SkIRect& a, const SkIRect& b)

Returns true if a intersects b. Returns false if either a or b is empty, or do not intersect.

### Parameters

| IRect to intersect |

| IRect to intersect |

### Return Value

true if a and b have area in common

### Example

### See Also

IntersectsNoEmptyCheck intersect SkRect::intersect

## IntersectsNoEmptyCheck

static bool IntersectsNoEmptyCheck(const SkIRect& a, const SkIRect& b)

Returns true if a intersects b. Asserts if either a or b is empty, and if SK DEBUG is defined.

### Parameters

| IRect to intersect |

| IRect to intersect |

### Return Value

true if a and b have area in common

### Example

### See Also

Intersects intersect SkRect::intersect

## join

void join(int32_t left, int32_t top, int32_t right, int32_t bottom)

Constructs Rect to intersect from (left, top, right, bottom). Does not sort construction.

Sets Rect to the union of itself and the construction.

Has no effect if construction is empty. Otherwise, if Rect is empty, sets Rect to construction.

### Parameters

| x minimum of constructed Rect |

| y minimum of constructed Rect |

| x maximum of constructed Rect |

| y maximum of constructed Rect |

### Example

### See Also

void join(const SkIRect& r)

Sets Rect to the union of itself and r.

Has no effect if r is empty. Otherwise, if Rect is empty, sets Rect to r.

### Parameters

| expansion Rect |

### Example

### See Also

## sort

void sort()

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.

### Example

### See Also

## makeSorted

SkIRect makeSorted() const

Returns Rect 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 Value

sorted IRect

### Example

### See Also

## EmptyIRect

static const SkIRect& SK_WARN_UNUSED_RESULT EmptyIRect()

Returns a reference to immutable empty IRect, set to (0, 0, 0, 0).

### Return Value

global IRect set to all zeroes