Rect class

Immutable 2D rectangle with integer position and size. Note that a Rect can be constructed with negative size values; this will be handled properly.

For many operations, the rectangle is treated as a group of columns and rows. For example, consider the Rect defined by top=1, right=4, bottom=5, left=1. This rectangle consists of 3 columns (right - left) and 4 rows (bottom - top). For grid-based game logic, this is almost always the behavior we want.

Constructors

Rect(Vec2 position, Vec2 size)
const
Rect.atOrigin(int width, int height)
Create a Rect positioned at (0, 0) with the given width and height
Rect.positionAndSize(int x, int y, int width, int height)
Create a Rect at the given position coordinates with the given width and height
Rect.sides(int top, int right, int bottom, int left)
Create a Rect using the given bounds

Properties

absArea int
Area of the rectangle (always positive)
no setter
area int
Area of the rectangle (may be negative)
no setter
aspectRatio double
Aspect ratio of the rectangle (always >= 1.0)
no setter
bottom int
Vertical coordinate of the bottom side of this rectangle
no setter
bottomLeft Vec2
Bottom-left corner of the rectangle
no setter
bottomRight Vec2
Bottom-right corner of the rectangle
no setter
center Vec2
Integer center point of the rectangle
no setter
hashCode int
The hash code for this object.
no setteroverride
height int
Height of the rectangle
no setter
left int
Horizontal coordinate of the left side of this rectangle
no setter
position Vec2
final
Horizontal coordinate of the right side of this rectangle
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
size Vec2
final
top int
Vertical coordinate of the top side of this rectangle
no setter
topLeft Vec2
Top-left corner of the rectangle
no setter
topRight Vec2
Top-right corner of the rectangle
no setter
width int
Width of the rectangle
no setter
x int
Horizontal coordinate of the top-left corner of this rectangle
no setter
y int
Vertical coordinate of the top-left corner of this rectangle
no setter

Methods

centerRect(int width, int height) Rect
Create a new Rect with the given width and height that is centered on this rectangle.
clamp(Vec2 point) Vec2
Clamps the given point to the nearest point that is within the bounds of this Rect. Returns the clamped point as a new Vec2.
contains(Object? other) bool
Returns true if other is a VectorBase or a Rect and is contained within this rectangle. For this operation the rectangle is treated as a group of columns and rows.
containsRect(Rect other) bool
Returns true if other is contained within this rectangle, including being equivalent to this rectangle. That is, other's top, right, bottom, and left must be equal to or inside this rectangle.
containsVec(VectorBase vector) bool
Returns true if vector is anywhere within the columns and rows enclosed by this rectangle.
distanceTo(Rect other) int
Returns the distance between this Rect and other. This is minimum length that a corridor would have to be to go from one Rect to the other. If the two Rects are adjacent, returns zero. If they overlap, returns -1.
expand(int amount) Rect
Create a new Rect by expanding all four sides of this rectangle outwards by the given amount. Calling with a negative amount will return a shrunken Rect (see shrink).
getBorder() List<Vec2>
getPoints() List<Vec2>
Get a list of all points enclosed by this Rect.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
shrink(int amount) Rect
Create a new Rect by shrinking all four sides of this rectangle inwards by the given amount. Calling with a negative amount will return an expanded Rect.
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
Returns true if other is another Rect that creates the same rectangle in 2D coordinate space as this rectangle (i.e., their top, right, bottom, and left values are equivalent).
override

Constants

nill → const Rect
The null rectangle: position (0, 0) size (0, 0)