Rect

class Rect

Rect holds four float coordinates describing the upper and lower bounds of a rectangle.

Rect may be created from outer bounds or from position, width, and height. Rect 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.

Example:

rect = skia.Rect(0, 0, 180, 120)
rect = skia.Rect((0, 0, 180, 120))  # Convert from tuple
print(rect.width(), rect.height())
left, top, right, bottom = tuple(rect)  # Convert to tuple

Methods

Intersects

Returns true if a intersects b.

Make

Overloaded function.

MakeEmpty

Returns constructed Rect set to (0, 0, 0, 0).

MakeIWH

Returns constructed Rect set to integer values (0, 0, w, h).

MakeLTRB

Returns constructed Rect set to (l, t, r, b).

MakeSize

Returns constructed Rect set to (0, 0, size.width(), size.height()).

MakeWH

Returns constructed Rect set to Scalar values (0, 0, w, h).

MakeXYWH

Returns constructed Rect set to (x, y, x + w, y + h).

__init__

Overloaded function.

asScalars

Returns pointer to first scalar in Rect, to treat it as an array with four entries.

bottom

Returns bottom edge of SkRect, if sorted.

centerX

Returns average of left edge and right edge.

centerY

Returns average of top edge and bottom edge.

contains

Overloaded function.

dump

Writes text representation of Rect to standard output.

dumpHex

Writes text representation of Rect to standard output.

height

Returns span on the y-axis.

inset

Insets Rect by (dx, dy).

intersect

Overloaded function.

intersects

Returns true if Rect intersects r.

isEmpty

Returns true if fLeft is equal to or greater than fRight, or if fTop is equal to or greater than fBottom.

isFinite

Returns true if all values in the rectangle are finite: SK_ScalarMin or larger, and SK_ScalarMax or smaller.

isSorted

Returns true if fLeft is equal to or less than fRight, or if fTop is equal to or less than fBottom.

join

Sets Rect to the union of itself and r.

joinNonEmptyArg

Sets Rect to the union of itself and r.

joinPossiblyEmptyRect

Sets Rect to the union of itself and the construction.

left

Returns left edge of SkRect, if sorted.

makeInset

Returns Rect, inset by (dx, dy).

makeOffset

Overloaded function.

makeOutset

Returns Rect, outset by (dx, dy).

makeSorted

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.

offset

Overloaded function.

offsetTo

Offsets Rect so that fLeft equals newX, and fTop equals newY.

outset

Outsets Rect by (dx, dy).

right

Returns right edge of SkRect, if sorted.

round

Returns IRect by adding 0.5 and discarding the fractional portion of Rect members, using ( SkScalarRoundToInt(fLeft), SkScalarRoundToInt(fTop), SkScalarRoundToInt(fRight), SkScalarRoundToInt(fBottom)).

roundIn

Sets Rect by rounding up fLeft and fTop; and discarding the fractional portion of fRight and fBottom, using ( SkScalarCeilToInt(fLeft), SkScalarCeilToInt(fTop), SkScalarFloorToInt(fRight), SkScalarFloorToInt(fBottom)).

roundOut

Sets IRect by discarding the fractional portion of fLeft and fTop; and rounding up fRight and fBottom, using ( SkScalarFloorToInt(fLeft), SkScalarFloorToInt(fTop), SkScalarCeilToInt(fRight), SkScalarCeilToInt(fBottom)).

set

Overloaded function.

setBounds

Sets to bounds of Point array with count entries.

setBoundsCheck

Sets to bounds of Point array with count entries.

setBoundsNoCheck

Sets to bounds of Point pts array with count entries.

setEmpty

Sets Rect to (0, 0, 0, 0).

setIWH

setLTRB

Sets Rect to (left, top, right, bottom).

setWH

Sets Rect to (0, 0, width, height).

setXYWH

Sets Rect to (x, y, x + width, y + height).

sort

Swaps fLeft and fRight if fLeft is greater than fRight; and swaps fTop and fBottom if fTop is greater than fBottom.

toQuad

Returns four points in quad that enclose Rect ordered as: top-left, top-right, bottom-right, bottom-left.

top

Returns top edge of SkRect, if sorted.

width

Returns span on the x-axis.

x

Returns left edge of Rect, if sorted.

y

Returns top edge of SkRect, if sorted.

Attributes

fBottom

larger y-axis bounds

fLeft

smaller x-axis bounds

fRight

larger x-axis bounds

fTop

smaller y-axis bounds

Methods

static Rect.Intersects(a: skia.Rect, b: skia.Rect) bool

Returns true if a intersects b.

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

Parameters:
  • aRect to intersect

  • bRect to intersect

Returns:

true if a and b have area in common

static Rect.Make(*args, **kwargs)

Overloaded function.

  1. Make(size: skia.ISize) -> skia.Rect

    Returns constructed Rect set to (0, 0, size.width(), size.height()).

    Does not validate input; size.width() or size.height() may be negative.

    size:

    integer values for Rect width and height

    return:

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

  2. Make(irect: skia.IRect) -> skia.Rect

    Returns constructed Rect set to irect, promoting integers to scalar.

    Does not validate input; fLeft may be greater than fRight, fTop may be greater than fBottom.

    irect:

    integer unsorted bounds

    return:

    irect members converted to Scalar

static Rect.MakeEmpty() skia.Rect

Returns constructed Rect 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.

Returns:

bounds (0, 0, 0, 0)

static Rect.MakeIWH(w: int, h: int) skia.Rect

Returns constructed Rect 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 IRect for an exact integer rectangle.

Parameters:
  • w – integer width of constructed Rect

  • h – integer height of constructed Rect

Returns:

bounds (0, 0, w, h)

static Rect.MakeLTRB(l: float, t: float, r: float, b: float) skia.Rect

Returns constructed Rect set to (l, t, r, b).

Does not sort input; Rect may result in fLeft greater than fRight, or fTop greater than fBottom.

Parameters:
  • l – Scalar stored in fLeft

  • t – Scalar stored in fTop

  • r – Scalar stored in fRight

  • b – Scalar stored in fBottom

Returns:

bounds (l, t, r, b)

static Rect.MakeSize(size: skia.Size) skia.Rect

Returns constructed Rect set to (0, 0, size.width(), size.height()).

Does not validate input; size.width() or size.height() may be negative.

Parameters:

size – Scalar values for Rect width and height

Returns:

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

static Rect.MakeWH(w: float, h: float) skia.Rect

Returns constructed Rect set to Scalar values (0, 0, w, h).

Does not validate input; w or h may be negative.

Passing integer values may generate a compiler warning since Rect cannot represent 32-bit integers exactly. Use IRect for an exact integer rectangle.

Parameters:
  • w – Scalar width of constructed Rect

  • h – Scalar height of constructed Rect

Returns:

bounds (0, 0, w, h)

static Rect.MakeXYWH(x: float, y: float, w: float, h: float) skia.Rect

Returns constructed Rect set to (x, y, x + w, y + h).

Does not validate input; w or h may be negative.

Parameters:
  • x – stored in fLeft

  • y – stored in fTop

  • w – added to x and stored in fRight

  • h – added to y and stored in fBottom

Returns:

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

Rect.__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: skia.Rect) -> None

    Returns constructed Rect 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)

  2. __init__(self: skia.Rect, w: float, h: float) -> None

    Returns constructed Rect set to Scalar values (0, 0, w, h).

    Does not validate input; w or h may be negative.

    Passing integer values may generate a compiler warning since Rect cannot represent 32-bit integers exactly. Use IRect for an exact integer rectangle.

    w:

    Scalar width of constructed Rect

    h:

    Scalar height of constructed Rect

    return:

    bounds (0, 0, w, h)

  3. __init__(self: skia.Rect, l: float, t: float, r: float, b: float) -> None

    Returns constructed Rect set to (l, t, r, b).

    Does not sort input; Rect may result in fLeft greater than fRight, or fTop greater than fBottom.

    l:

    Scalar stored in fLeft

    t:

    Scalar stored in fTop

    r:

    Scalar stored in fRight

    b:

    Scalar stored in fBottom

    return:

    bounds (l, t, r, b)

  4. __init__(self: skia.Rect, size: skia.ISize) -> None

    Returns constructed Rect set to (0, 0, size.width(), size.height()).

    Does not validate input; size.width() or size.height() may be negative.

    size:

    integer values for Rect width and height

    return:

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

  5. __init__(self: skia.Rect, irect: skia.IRect) -> None

    Returns constructed Rect set to irect, promoting integers to scalar.

    Does not validate input; fLeft may be greater than fRight, fTop may be greater than fBottom.

    irect:

    integer unsorted bounds

    return:

    irect members converted to Scalar

  6. __init__(self: skia.Rect, t: tuple) -> None

Rect.asScalars(self: skia.Rect) memoryview

Returns pointer to first scalar in Rect, to treat it as an array with four entries.

Returns:

pointer to fLeft

Rect.bottom(self: skia.Rect) float

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.

Returns:

fBottom

Rect.centerX(self: skia.Rect) float

Returns average of left edge and right edge.

Result does not change if SkRect is sorted. Result may overflow to infinity if Rect is far from the origin.

Returns:

midpoint on x-axis

Rect.centerY(self: skia.Rect) float

Returns average of top edge and bottom edge.

Result does not change if Rect is sorted.

Returns:

midpoint on y-axis

Rect.contains(*args, **kwargs)

Overloaded function.

  1. contains(self: skia.Rect, x: float, y: float) -> bool

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

    Returns false if Rect is empty.

    x:

    test Point x-coordinate

    y:

    test Point y-coordinate

    return:

    true if (x, y) is inside Rect

  2. contains(self: skia.Rect, r: skia.Rect) -> bool

    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.

    r:

    Rect contained

    return:

    true if all sides of Rect are outside r

  3. contains(self: skia.Rect, r: skia.IRect) -> bool

    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.

    r:

    IRect contained

    return:

    true if all sides of Rect are outside r

Rect.dump(self: skia.Rect, asHex: bool = False) None

Writes text representation of Rect to standard output.

Set asHex to true to generate exact binary representations of floating point numbers.

Parameters:

asHex – true if Scalar values are written as hexadecimal

Rect.dumpHex(self: skia.Rect) None

Writes text representation of Rect 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 Rect.

Use instead of dump() when submitting

Rect.height(self: skia.Rect) float

Returns span on the y-axis.

Rect.inset(self: skia.Rect, dx: float, dy: float) None

Insets Rect by (dx, dy).

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

Parameters:
  • dx – added to fLeft and subtracted from fRight

  • dy – added to fTop and subtracted from fBottom

Rect.intersect(*args, **kwargs)

Overloaded function.

  1. intersect(self: skia.Rect, r: skia.Rect) -> bool

    Returns true if Rect intersects r, and sets Rect to intersection.

    Returns false if Rect does not intersect r, and leaves Rect unchanged.

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

    r:

    limit of result

    return:

    true if r and Rect have area in common

  2. intersect(self: skia.Rect, a: skia.Rect, b: skia.Rect) -> bool

    Returns true if a intersects b, and sets Rect to intersection.

    Returns false if a does not intersect b, and leaves Rect unchanged.

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

    a:

    Rect to intersect

    b:

    Rect to intersect

    return:

    true if a and b have area in common

Rect.intersects(self: skia.Rect, r: skia.Rect) bool

Returns true if Rect intersects r.

Returns false if either r or Rect is empty, or do not intersect.

Parameters:

rRect to intersect

Returns:

true if r and Rect have area in common

Rect.isEmpty(self: skia.Rect) bool

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().

Returns:

true if width() or height() are zero or negative

Rect.isFinite(self: skia.Rect) bool

Returns true if all values in the rectangle are finite: SK_ScalarMin or larger, and SK_ScalarMax or smaller.

Returns:

true if no member is infinite or NaN

Rect.isSorted(self: skia.Rect) bool

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().

Returns:

true if width() or height() are zero or positive

Rect.join(self: skia.Rect, r: skia.Rect) None

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:

r – expansion Rect

Rect.joinNonEmptyArg(self: skia.Rect, r: skia.Rect) None

Sets Rect to the union of itself and r.

Asserts if r is empty and SK_DEBUG is defined. If Rect is empty, sets Rect to r.

May produce incorrect results if r is empty.

Parameters:

r – expansion Rect

Rect.joinPossiblyEmptyRect(self: skia.Rect, r: skia.Rect) None

Sets Rect to the union of itself and the construction.

May produce incorrect results if Rect or r is empty.

Parameters:

r – expansion Rect

Rect.left(self: skia.Rect) float

Returns left edge of SkRect, if sorted.

Call isSorted() to see if SkRect is valid. Call sort() to reverse fLeft and fRight if needed.

Returns:

fLeft

Rect.makeInset(self: skia.Rect, dx: float, dy: float) skia.Rect

Returns Rect, inset by (dx, dy).

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

Parameters:
  • dx – added to fLeft and subtracted from fRight

  • dy – added to fTop and subtracted from fBottom

Returns:

Rect inset symmetrically left and right, top and bottom

Rect.makeOffset(*args, **kwargs)

Overloaded function.

  1. makeOffset(self: skia.Rect, dx: float, dy: float) -> skia.Rect

    Returns Rect offset by (dx, dy).

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

    dx:

    added to fLeft and fRight

    dy:

    added to fTop and fBottom

    return:

    Rect offset on axes, with original width and height

  2. makeOffset(self: skia.Rect, v: skia.Point) -> skia.Rect

    Returns Rect offset by v.

    v:

    added to rect

    return:

    Rect offset on axes, with original width and height

Rect.makeOutset(self: skia.Rect, dx: float, dy: float) skia.Rect

Returns Rect, outset by (dx, dy).

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

Parameters:
  • dx – subtracted to fLeft and added from fRight

  • dy – subtracted to fTop and added from fBottom

Returns:

Rect outset symmetrically left and right, top and bottom

Rect.makeSorted(self: skia.Rect) skia.Rect

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.

Returns:

sorted Rect

Rect.offset(*args, **kwargs)

Overloaded function.

  1. offset(self: skia.Rect, dx: float, dy: float) -> None

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

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

    dx:

    offset added to fLeft and fRight

    dy:

    offset added to fTop and fBottom

  2. offset(self: skia.Rect, delta: skia.Point) -> None

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

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

    param delta:

    added to Rect

Rect.offsetTo(self: skia.Rect, newX: float, newY: float) None

Offsets Rect so that fLeft equals newX, and fTop equals newY.

width and height are unchanged.

Parameters:
  • newX – stored in fLeft, preserving width()

  • newY – stored in fTop, preserving height()

Rect.outset(self: skia.Rect, dx: float, dy: float) None

Outsets Rect by (dx, 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:
  • dx – subtracted to fLeft and added from fRight

  • dy – subtracted to fTop and added from fBottom

Rect.right(self: skia.Rect) float

Returns right edge of SkRect, if sorted.

Call isSorted() to see if SkRect is valid. Call sort() to reverse fLeft and fRight if needed.

Returns:

fRight

Rect.round(self: skia.Rect) skia.IRect

Returns IRect by adding 0.5 and discarding the fractional portion of Rect members, using ( SkScalarRoundToInt(fLeft), SkScalarRoundToInt(fTop), SkScalarRoundToInt(fRight), SkScalarRoundToInt(fBottom)).

Returns:

rounded IRect

Rect.roundIn(self: skia.Rect) skia.IRect

Sets Rect by rounding up fLeft and fTop; and discarding the fractional portion of fRight and fBottom, using ( SkScalarCeilToInt(fLeft), SkScalarCeilToInt(fTop), SkScalarFloorToInt(fRight), SkScalarFloorToInt(fBottom)).

Rect.roundOut(self: skia.Rect) skia.IRect

Sets IRect by discarding the fractional portion of fLeft and fTop; and rounding up fRight and fBottom, using ( SkScalarFloorToInt(fLeft), SkScalarFloorToInt(fTop), SkScalarCeilToInt(fRight), SkScalarCeilToInt(fBottom)).

Returns:

rounded IRect

Rect.set(*args, **kwargs)

Overloaded function.

  1. set(self: skia.Rect, src: skia.IRect) -> None

    Sets Rect to src, promoting src members from integer to scalar.

    Very large values in src may lose precision.

    src:

    integer Rect

  2. set(self: skia.Rect, p0: skia.Point, p1: skia.Point) -> None

    Sets bounds to the smallest Rect enclosing Point p0 and p1.

    The result is sorted and may be empty. Does not check to see if values are finite.

    p0:

    corner to include

    p1:

    corner to include

Rect.setBounds(self: skia.Rect, points: list[skia.Point]) None

Sets to bounds of Point array with count entries.

If count is zero or smaller, or if Point array contains an infinity or NaN, sets 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.

Parameters:

pointsPoint array

Rect.setBoundsCheck(self: skia.Rect, points: list[skia.Point]) bool

Sets to bounds of Point array with count entries.

Returns false if count is zero or smaller, or if Point array contains an infinity or NaN; in these cases sets Rect 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.

Parameters:

pointsPoint array

Returns:

true if all Point values are finite

Rect.setBoundsNoCheck(self: skia.Rect, points: list[skia.Point]) None

Sets to bounds of Point pts array with count entries.

If any Point in pts contains infinity or NaN, all Rect dimensions are set to NaN.

Parameters:

pointsPoint array

Rect.setEmpty(self: skia.Rect) None

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

Rect.setIWH(self: skia.Rect, width: int, height: int) None
Rect.setLTRB(self: skia.Rect, left: float, top: float, right: float, bottom: float) None

Sets Rect 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:
  • left – stored in fLeft

  • top – stored in fTop

  • right – stored in fRight

  • bottom – stored in fBottom

Rect.setWH(self: skia.Rect, width: float, height: float) None

Sets Rect to (0, 0, width, height).

Does not validate input; width or height may be negative.

Parameters:
  • width – stored in fRight

  • height – stored in fBottom

Rect.setXYWH(self: skia.Rect, x: float, y: float, width: float, height: float) None

Sets Rect to (x, y, x + width, y + height).

Does not validate input; width or height may be negative.

Parameters:
  • x – stored in fLeft

  • y – stored in fTop

  • width – added to x and stored in fRight

  • height – added to y and stored in fBottom

Rect.sort(self: skia.Rect) None

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.

Rect.toQuad(self: skia.Rect) list[skia.Point]

Returns four points in quad that enclose Rect ordered as: top-left, top-right, bottom-right, bottom-left.

Returns:

corners of Rect

Rect.top(self: skia.Rect) float

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.

Returns:

fTop

Rect.width(self: skia.Rect) float

Returns span on the x-axis.

This does not check if Rect is sorted, or if result fits in 32-bit float; result may be negative or infinity.

Returns:

fRight minus fLeft

Rect.x(self: skia.Rect) float

Returns left edge of Rect, if sorted.

Call isSorted() to see if Rect is valid. Call sort() to reverse fLeft and fRight if needed.

Returns:

fLeft

Rect.y(self: skia.Rect) float

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.

Returns:

fTop

Attributes

Rect.fBottom

larger y-axis bounds

Rect.fLeft

smaller x-axis bounds

Rect.fRight

larger x-axis bounds

Rect.fTop

smaller y-axis bounds