Up to date

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

# Rect2¶

## 描述¶

Rect2 内置 Variant 类型表示 2D 空间中的轴对齐矩形。它由其 positionsize 定义，皆为 Vector2 类型。它经常被用于快速重叠测试（参见 intersects）。虽然 Rect2 本身是轴对齐的，但它可以与 Transform2D 组合来表示旋转或倾斜的矩形。

## 属性¶

 Vector2 end `Vector2(0, 0)` Vector2 position `Vector2(0, 0)` Vector2 size `Vector2(0, 0)`

## 构造函数¶

 Rect2 Rect2 ( ) Rect2 Rect2 ( Rect2 from ) Rect2 Rect2 ( Rect2i from ) Rect2 Rect2 ( Vector2 position, Vector2 size ) Rect2 Rect2 ( float x, float y, float width, float height )

## 方法¶

 Rect2 abs ( ) const bool encloses ( Rect2 b ) const Rect2 expand ( Vector2 to ) const float get_area ( ) const Vector2 get_center ( ) const Rect2 grow ( float amount ) const Rect2 grow_individual ( float left, float top, float right, float bottom ) const Rect2 grow_side ( int side, float amount ) const bool has_area ( ) const bool has_point ( Vector2 point ) const Rect2 intersection ( Rect2 b ) const bool intersects ( Rect2 b, bool include_borders=false ) const bool is_equal_approx ( Rect2 rect ) const bool is_finite ( ) const Rect2 merge ( Rect2 b ) const

## 操作符¶

 bool operator != ( Rect2 right ) Rect2 operator * ( Transform2D right ) bool operator == ( Rect2 right )

## 属性说明¶

Vector2 end = `Vector2(0, 0)`

Vector2 position = `Vector2(0, 0)`

Vector2 size = `Vector2(0, 0)`

## 构造函数说明¶

Rect2 Rect2 ( )

Rect2 Rect2 ( Rect2 from )

Rect2 Rect2 ( Rect2i from )

Rect2i 构造 Rect2

Rect2 Rect2 ( Vector2 position, Vector2 size )

Rect2 Rect2 ( float x, float y, float width, float height )

## 方法说明¶

Rect2 abs ( ) const

```var rect = Rect2(25, 25, -100, -50)
var absolute = rect.abs() # 绝对值为 Rect2(-75, -25, 100, 50)
```

bool encloses ( Rect2 b ) const

Rect2 expand ( Vector2 to ) const

```var rect = Rect2(0, 0, 5, 2)

rect = rect.expand(Vector2(10, 0)) # rect 为 Rect2(0, 0, 10, 2)
rect = rect.expand(Vector2(-5, 5)) # rect 为 Rect2(-5, 0, 10, 5)
```

float get_area ( ) const

Vector2 get_center ( ) const

Rect2 grow ( float amount ) const

```var a = Rect2(4, 4, 8, 8).grow(4) # a 为 Rect2(0, 0, 16, 16)
var b = Rect2(0, 0, 8, 4).grow(2) # b 为 Rect2(-2, -2, 12, 8)
```

Rect2 grow_individual ( float left, float top, float right, float bottom ) const

Rect2 grow_side ( int side, float amount ) const

bool has_area ( ) const

bool has_point ( Vector2 point ) const

Rect2 intersection ( Rect2 b ) const

```var rect1 = Rect2(0, 0, 5, 10)
var rect2 = Rect2(2, 0, 8, 4)

var a = rect1.intersection(rect2) # a 为 Rect2(2, 0, 3, 4)
```

bool intersects ( Rect2 b, bool include_borders=false ) const

bool is_equal_approx ( Rect2 rect ) const

bool is_finite ( ) const

Rect2 merge ( Rect2 b ) const

## 操作符说明¶

bool operator != ( Rect2 right )

Rect2 operator * ( Transform2D right )

`rect * transform` 相当于 `transform.inverse() * rect`。请参阅 Transform2D.inverse

bool operator == ( Rect2 right )