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
| ||
| ||
|
Constructors
Rect2i() | |
Methods
grow_individual(left:int, top:int, right:int, bottom:int)const | |
Operators
operator !=(right:Rect2i) | |
operator ==(right:Rect2i) |
Property Descriptions
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.
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
Constructs aRect2i with itsposition andsize set toVector2i.ZERO.
Constructs aRect2i as a copy of the givenRect2i.
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
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)
varrect=newRect2I(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.
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)
varrect=newRect2I(0,0,5,2);rect=rect.Expand(newVector2I(10,0));// rect is Rect2I(0, 0, 10, 2)rect=rect.Expand(newVector2I(-5,5));// rect is Rect2I(-5, 0, 15, 5)
Returns the rectangle's area. This is equivalent tosize.x*size.y. See alsohas_area().
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.
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)
vara=newRect2I(4,4,8,8).Grow(4);// a is Rect2I(0, 0, 16, 16)varb=newRect2I(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().
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)
vara=newRect2I(0,0,5,10);varb=newRect2I(2,0,8,4);varc=rect1.Intersection(rect2);// 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.
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.