A 2D axis-aligned bounding box using integer coordinates.
The Rect2i built-in Variant type represents an axis-aligned rectangle in a 2D space, using integer coordinates. It is defined by its position and size, which are Vector2i. Because it does not rotate, it is frequently used for fast overlap tests (see intersects()).
For floating-point coordinates, see Rect2.
Note: Negative values for size are not supported. With negative size, most Rect2i methods do not work correctly. Use abs() to get an equivalent Rect2i with a non-negative size.
Note: In a boolean context, a Rect2i evaluates to false if both position and size are zero (equal to Vector2i.ZERO). Otherwise, it always evaluates to true.
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 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 Rect2i 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.
var rect = Rect2i(25, 25, -100, -50)
var absolute = rect.abs() # absolute is Rect2i(-75, -25, 100, 50)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 a copy of this rectangle expanded to align the edges with the given to point, if necessary.
var rect = Rect2i(0, 0, 5, 2)
rect = rect.expand(Vector2i(10, 0)) # rect is Rect2i(0, 0, 10, 2)
rect = rect.expand(Vector2i(-5, 5)) # rect is Rect2i(-5, 0, 15, 5)Returns the rectangle's area. This is equivalent to size.x * size.y. See also has_area().
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().
var a = Rect2i(4, 4, 8, 8).grow(4) # a is Rect2i(0, 0, 16, 16)
var b = Rect2i(0, 0, 8, 4).grow(2) # b is Rect2i(-2, -2, 12, 8)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 the intersection between this rectangle and b. If the rectangles do not intersect, returns an empty Rect2i.
var a = Rect2i(0, 0, 5, 10)
var b = Rect2i(2, 0, 8, 4)
var c = a.intersection(b) # c is Rect2i(2, 0, 3, 4)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.
Returns a Rect2i that encloses both this rectangle and b around the edges. See also encloses().