Up to date
This page is up to date for Godot 4.1
. If you still find outdated information, please open an issue.
Rect2i
A 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
| ||
| ||
|
Constructors
Rect2i ( ) | |
Methods
abs ( ) const | |
get_area ( ) const | |
get_center ( ) const | |
grow_individual ( int left, int top, int right, int bottom ) const | |
has_area ( ) const | |
intersection ( Rect2i b ) const | |
intersects ( Rect2i b ) const | |
Operators
operator != ( Rect2i right ) | |
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.
Constructs a Rect2i as a copy of the given Rect2i.
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.
GDScriptC#
# 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))
// position (-3, 2), size (1, 1)
var rect = new Rect2I(new Vector2I(-3, 2), new Vector2I(1, 1));
// position (-3, -1), size (3, 4), so we fit both rect and Vector2I(0, -1)
var rect2 = rect.Expand(new 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.