Up to date

This page is up to date for Godot `4.2`. If you still find outdated information, please open an issue.

# Rect2i¶

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

## Description¶

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`.

참고

There are notable differences when using this API with C#. See C#과 GDScript의 API 차이점 for more information.

## 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)`

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.

Vector2i position = `Vector2i(0, 0)`

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

Vector2i size = `Vector2i(0, 0)`

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.

## Constructor Descriptions¶

Rect2i Rect2i ( )

Constructs a Rect2i with its position and size set to Vector2i.ZERO.

Rect2i Rect2i ( Rect2i from )

Constructs a Rect2i as a copy of the given Rect2i.

Rect2i Rect2i ( Rect2 from )

Constructs a Rect2i from a Rect2. The floating-point coordinates are 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 setting its position to (`x`, `y`), and its size to (`width`, `height`).

## Method Descriptions¶

Rect2i abs ( ) const

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.

bool encloses ( Rect2i b ) const

Returns `true` if this Rect2i completely encloses another one.

Rect2i expand ( Vector2i to ) const

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, 10, 5)
```

int get_area ( ) const

Returns the rectangle's area. This is equivalent to `size.x * size.y`. See also has_area.

Vector2i get_center ( ) const

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

Note: If the size is odd, the result will be rounded towards position.

Rect2i grow ( int amount ) const

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.