The Rect2 built-in Variant type represents an axis-aligned rectangle in a 2D space. It is defined by its position and size, which are Vector2. It is frequently used for fast overlap tests (see intersects). Although Rect2 itself is axis-aligned, it can be combined with Transform2D to represent a rotated or skewed rectangle.
For integer coordinates, use Rect2i. The 3D equivalent to Rect2 is AABB.
Constructs a Rect2 with its position and size set to Vector2.ZERO.
Constructs a Rect2 as a copy of the given Rect2.
Constructs a Rect2 from a Rect2i.
Constructs a Rect2 by position
and size
.
Constructs a Rect2 by setting its position to (x
, y
), and its size to (width
, height
).
Returns true
if the position or size of both rectangles are not equal.
Note: Due to floating-point precision errors, consider using is_equal_approx instead, which is more reliable.
Inversely transforms (multiplies) the Rect2 by the given Transform2D transformation matrix, under the assumption that the transformation basis is orthonormal (i.e. rotation/reflection is fine, scaling/skew is not).
rect * transform
is equivalent to transform.inverse() * rect
. See Transform2D.inverse.
For transforming by inverse of an affine transformation (e.g. with scaling) transform.affine_inverse() * rect
can be used instead. See Transform2D.affine_inverse.
Returns true
if both position and size of the rectangles are exactly equal, respectively.
Note: Due to floating-point precision errors, consider using is_equal_approx instead, which is more reliable.
The ending point. This is usually the bottom-right corner of the rectangle, and is equivalent to position + size
. Setting this point affects the size.
The origin point. This is usually the top-left corner of the rectangle.
The rectangle's width and height, starting from position. Setting this value also affects the end point.
Note: It's recommended setting the width and height to non-negative values, as most methods in Godot assume that the position is the top-left corner, and the end is the bottom-right corner. To get an equivalent rectangle with non-negative size, use abs.
Returns a Rect2 equivalent to this rectangle, with its width and height modified to be non-negative values, and with its position being the top-left corner of the rectangle.
Note: It's recommended to use this method when size is negative, as most other methods in Godot assume that the position is the top-left corner, and the end is the bottom-right corner.
Returns true
if this rectangle completely encloses the b
rectangle.
Returns a copy of this rectangle expanded to align the edges with the given to
point, if necessary.
Returns the rectangle's area. This is equivalent to size.x * size.y
. See also has_area.
Returns the center point of the rectangle. This is the same as position + (size / 2.0)
.
Returns a copy of this rectangle extended on all sides by the given amount
. A negative amount
shrinks the rectangle instead. See also grow_individual and grow_side.
Returns a copy of this rectangle with its left
, top
, right
, and bottom
sides extended by the given amounts. Negative values shrink the sides, instead. See also grow and grow_side.
Returns a copy of this rectangle with its side
extended by the given amount
(see Side constants). A negative amount
shrinks the rectangle, instead. See also grow and grow_individual.
Returns true
if this rectangle has positive width and height. See also get_area.
Returns true
if the rectangle contains the given point
. By convention, points on the right and bottom edges are not included.
Note: This method is not reliable for Rect2 with a negative size. Use abs first to get a valid rectangle.
Returns the intersection between this rectangle and b
. If the rectangles do not intersect, returns an empty Rect2.
Note: If you only need to know whether two rectangles are overlapping, use intersects, instead.
Returns true
if this rectangle overlaps with the b
rectangle. The edges of both rectangles are excluded, unless include_borders
is true
.
Returns true
if this rectangle and rect
are approximately equal, by calling Vector2.is_equal_approx on the position and the size.
Returns true
if this rectangle's values are finite, by calling Vector2.is_finite on the position and the size.
Returns a Rect2 that encloses both this rectangle and b
around the edges. See also encloses.