Work in progress

Godot documentation is being updated to reflect the latest changes in version 4.0. Some documentation pages may still state outdated information. This banner will tell you if you're reading one of such pages.

The contents of this page are up to date. If you can still find outdated information, please open an issue.

Rect2i

2D axis-aligned bounding box using integer coordinates.

Description

Rect2i consists of a position, a size, and several utility functions. It is typically used for fast overlap tests.

It uses integer coordinates. If you need floating-point coordinates, use Rect2 instead.

Negative values for size are not supported and will not work for most methods. Use abs to get a Rect2i with a positive size.

Tutorials

Properties

Vector2i

end

Vector2i(0, 0)

Vector2i

position

Vector2i(0, 0)

Vector2i

size

Vector2i(0, 0)

Constructors

Rect2i

Rect2i ( )

Rect2i

Rect2i ( Rect2i from )

Rect2i

Rect2i ( Rect2 from )

Rect2i

Rect2i ( Vector2i position, Vector2i size )

Rect2i

Rect2i ( int x, int y, int width, int height )

Methods

Rect2i

abs ( ) const

bool

encloses ( Rect2i b ) const

Rect2i

expand ( Vector2i to ) const

int

get_area ( ) const

Vector2i

get_center ( ) const

Rect2i

grow ( int amount ) const

Rect2i

grow_individual ( int left, int top, int right, int bottom ) const

Rect2i

grow_side ( int side, int amount ) const

bool

has_area ( ) const

bool

has_point ( Vector2i point ) const

Rect2i

intersection ( Rect2i b ) const

bool

intersects ( Rect2i b ) const

Rect2i

merge ( Rect2i b ) const

Operators

bool

operator != ( Rect2i right )

bool

operator == ( Rect2i right )


Property Descriptions

Vector2i end = Vector2i(0, 0)

Ending corner. This is calculated as position + size. Setting this value will change the size.


Vector2i position = Vector2i(0, 0)

Beginning corner. Typically has values lower than end.


Vector2i size = Vector2i(0, 0)

Size from position to end. Typically, all components are positive.

If the size is negative, you can use abs to fix it.


Constructor Descriptions

Rect2i Rect2i ( )

Constructs a default-initialized Rect2i with default (zero) values of position and size.


Rect2i Rect2i ( Rect2i from )

Constructs a Rect2i as a copy of the given Rect2i.


Rect2i Rect2i ( Rect2 from )

Constructs a new Rect2i from Rect2. The floating point coordinates will be truncated.


Rect2i Rect2i ( Vector2i position, Vector2i size )

Constructs a Rect2i by position and size.


Rect2i Rect2i ( int x, int y, int width, int height )

Constructs a Rect2i by x, y, width, and height.


Method Descriptions

Rect2i abs ( ) const

Returns a Rect2i with equivalent position and area, modified so that the top-left corner is the origin and width and height are positive.


bool encloses ( Rect2i b ) const

Returns true if this Rect2i completely encloses another one.


Rect2i expand ( Vector2i to ) const

Returns a copy of this Rect2i expanded so that the borders align with the given point.

# position (-3, 2), size (1, 1)
var rect = Rect2i(Vector2i(-3, 2), Vector2i(1, 1))
# position (-3, -1), size (3, 4), so we fit both rect and Vector2i(0, -1)
var rect2 = rect.expand(Vector2i(0, -1))

int get_area ( ) const

Returns the area of the Rect2i. See also has_area.


Vector2i get_center ( ) const

Returns the center of the Rect2i, which is equal to position + (size / 2).

If size is an odd number, the returned center value will be rounded towards position.


Rect2i grow ( int amount ) const

Returns a copy of the Rect2i grown by the specified amount on all sides.


Rect2i grow_individual ( int left, int top, int right, int bottom ) const

Returns a copy of the Rect2i grown by the specified amount on each side individually.


Rect2i grow_side ( int side, int amount ) const

Returns a copy of the Rect2i grown by the specified amount on the specified Side.


bool has_area ( ) const

Returns true if the Rect2i has area, and false if the Rect2i is linear, empty, or has a negative size. See also get_area.


bool has_point ( Vector2i point ) const

Returns true if the Rect2i contains a point. By convention, the right and bottom edges of the Rect2i are considered exclusive, so points on these edges are not included.

Note: This method is not reliable for Rect2i with a negative size. Use abs to get a positive sized equivalent rectangle to check for contained points.


Rect2i intersection ( Rect2i b ) const

Returns the intersection of this Rect2i and b.

If the rectangles do not intersect, an empty Rect2i is returned.


bool intersects ( Rect2i b ) const

Returns true if the Rect2i overlaps with b (i.e. they have at least one point in common).


Rect2i merge ( Rect2i b ) const

Returns a larger Rect2i that contains this Rect2i and b.


Operator Descriptions

bool operator != ( Rect2i right )

Returns true if the rectangles are not equal.


bool operator == ( Rect2i right )

Returns true if the rectangles are equal.