Rect2i

A 2D axis-aligned bounding box using integer coordinates.

Description

TheRect2i built-inVariant type represents an axis-aligned rectangle in a 2D space, using integer coordinates. It is defined by itsposition andsize, which areVector2i. Because it does not rotate, it is frequently used for fast overlap tests (seeintersects()).

For floating-point coordinates, seeRect2.

Note: Negative values forsize are not supported. With negative size, mostRect2i methods do not work correctly. Useabs() to get an equivalentRect2i with a non-negative size.

Note: In a boolean context, aRect2i evaluates tofalse if bothposition andsize are zero (equal toVector2i.ZERO). Otherwise, it always evaluates totrue.

Note

There are notable differences when using this API with C#. SeeC# API differences to GDScript for more information.

Tutorials

Properties

Vector2i

end

Vector2i(0,0)

Vector2i

position

Vector2i(0,0)

Vector2i

size

Vector2i(0,0)

Constructors

Rect2i

Rect2i()

Rect2i

Rect2i(from:Rect2i)

Rect2i

Rect2i(from:Rect2)

Rect2i

Rect2i(position:Vector2i, size:Vector2i)

Rect2i

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

Methods

Rect2i

abs()const

bool

encloses(b:Rect2i)const

Rect2i

expand(to:Vector2i)const

int

get_area()const

Vector2i

get_center()const

Rect2i

grow(amount:int)const

Rect2i

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

Rect2i

grow_side(side:int, amount:int)const

bool

has_area()const

bool

has_point(point:Vector2i)const

Rect2i

intersection(b:Rect2i)const

bool

intersects(b:Rect2i)const

Rect2i

merge(b:Rect2i)const

Operators

bool

operator !=(right:Rect2i)

bool

operator ==(right:Rect2i)


Property Descriptions

Vector2iend =Vector2i(0,0)🔗

The ending point. This is usually the bottom-right corner of the rectangle, and is equivalent toposition+size. Setting this point affects thesize.


Vector2iposition =Vector2i(0,0)🔗

The origin point. This is usually the top-left corner of the rectangle.


Vector2isize =Vector2i(0,0)🔗

The rectangle's width and height, starting fromposition. Setting this value also affects theend point.

Note: It's recommended setting the width and height to non-negative values, as most methods in Godot assume that theposition is the top-left corner, and theend is the bottom-right corner. To get an equivalent rectangle with non-negative size, useabs().


Constructor Descriptions

Rect2iRect2i()🔗

Constructs aRect2i with itsposition andsize set toVector2i.ZERO.


Rect2iRect2i(from:Rect2i)

Constructs aRect2i as a copy of the givenRect2i.


Rect2iRect2i(from:Rect2)

Constructs aRect2i from aRect2. The floating-point coordinates are truncated.


Rect2iRect2i(position:Vector2i, size:Vector2i)

Constructs aRect2i byposition andsize.


Rect2iRect2i(x:int, y:int, width:int, height:int)

Constructs aRect2i by setting itsposition to (x,y), and itssize to (width,height).


Method Descriptions

Rect2iabs()const🔗

Returns aRect2i equivalent to this rectangle, with its width and height modified to be non-negative values, and with itsposition being the top-left corner of the rectangle.

varrect=Rect2i(25,25,-100,-50)varabsolute=rect.abs()# absolute is Rect2i(-75, -25, 100, 50)

Note: It's recommended to use this method whensize is negative, as most other methods in Godot assume that theposition is the top-left corner, and theend is the bottom-right corner.


boolencloses(b:Rect2i)const🔗

Returnstrue if thisRect2i completely encloses another one.


Rect2iexpand(to:Vector2i)const🔗

Returns a copy of this rectangle expanded to align the edges with the givento point, if necessary.

varrect=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)

intget_area()const🔗

Returns the rectangle's area. This is equivalent tosize.x*size.y. See alsohas_area().


Vector2iget_center()const🔗

Returns the center point of the rectangle. This is the same asposition+(size/2).

Note: If thesize is odd, the result will be rounded towardsposition.


Rect2igrow(amount:int)const🔗

Returns a copy of this rectangle extended on all sides by the givenamount. A negativeamount shrinks the rectangle instead. See alsogrow_individual() andgrow_side().

vara=Rect2i(4,4,8,8).grow(4)# a is Rect2i(0, 0, 16, 16)varb=Rect2i(0,0,8,4).grow(2)# b is Rect2i(-2, -2, 12, 8)

Rect2igrow_individual(left:int, top:int, right:int, bottom:int)const🔗

Returns a copy of this rectangle with itsleft,top,right, andbottom sides extended by the given amounts. Negative values shrink the sides, instead. See alsogrow() andgrow_side().


Rect2igrow_side(side:int, amount:int)const🔗

Returns a copy of this rectangle with itsside extended by the givenamount (seeSide constants). A negativeamount shrinks the rectangle, instead. See alsogrow() andgrow_individual().


boolhas_area()const🔗

Returnstrue if this rectangle has positive width and height. See alsoget_area().


boolhas_point(point:Vector2i)const🔗

Returnstrue if the rectangle contains the givenpoint. By convention, points on the right and bottom edges arenot included.

Note: This method is not reliable forRect2i with anegativesize. Useabs() first to get a valid rectangle.


Rect2iintersection(b:Rect2i)const🔗

Returns the intersection between this rectangle andb. If the rectangles do not intersect, returns an emptyRect2i.

vara=Rect2i(0,0,5,10)varb=Rect2i(2,0,8,4)varc=a.intersection(b)# c is Rect2i(2, 0, 3, 4)

Note: If you only need to know whether two rectangles are overlapping, useintersects(), instead.


boolintersects(b:Rect2i)const🔗

Returnstrue if this rectangle overlaps with theb rectangle. The edges of both rectangles are excluded.


Rect2imerge(b:Rect2i)const🔗

Returns aRect2i that encloses both this rectangle andb around the edges. See alsoencloses().


Operator Descriptions

booloperator !=(right:Rect2i)🔗

Returnstrue if theposition orsize of both rectangles are not equal.


booloperator ==(right:Rect2i)🔗

Returnstrue if bothposition andsize of the rectangles are equal, respectively.


User-contributed notes

Please read theUser-contributed notes policy before submitting a comment.