Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

Rect2i

使用整数坐标的 2D 轴对齐边界框。

描述

Rect2i 是内置的 Variant 类型,代表 2D 空间中与轴对齐的矩形,使用整数坐标。由 positionsize 定义,它们都是 Vector2i。因为这个矩形不会旋转,所以可以进行快速的重合检查(见 intersects())。

浮点数坐标的版本见 Rect2

注意:不支持负数的 size。如果大小为负数,Rect2i 的大多数方法都无法正常工作。请使用 abs() 获取等价且大小非负的 Rect2i

注意:在布尔值上下文中,positionsize 均为零(等于 Vector2i.ZERO)的 Rect2i 会得到 false。否则得到 true

Note

通过 C# 使用该 API 时会有显著不同,详见 C# API differences to GDScript

教程

属性

Vector2i

end

Vector2i(0, 0)

Vector2i

position

Vector2i(0, 0)

Vector2i

size

Vector2i(0, 0)

构造函数

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)

方法

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

运算符

bool

operator !=(right: Rect2i)

bool

operator ==(right: Rect2i)


属性说明

Vector2i end = Vector2i(0, 0) 🔗

终点。通常为矩形的右下角,等价于 position + size。设置该点会影响 size


Vector2i position = Vector2i(0, 0) 🔗

原点。通常为矩形的左上角。


Vector2i size = Vector2i(0, 0) 🔗

矩形的宽和高,相对于 position。设置该值会影响终点 end

注意:建议将宽和高设置为非负数,因为 Godot 中的大多数方法假设 position 为左上角、end 为右下角。要获取等价且大小非负的矩形,请使用 abs()


构造函数说明

Rect2i Rect2i() 🔗

构造 Rect2i,将 positionsize 设置为 Vector2i.ZERO


Rect2i Rect2i(from: Rect2i)

构造给定 Rect2i 的副本。


Rect2i Rect2i(from: Rect2)

根据 Rect2 构造 Rect2i。会截断浮点数坐标。


Rect2i Rect2i(position: Vector2i, size: Vector2i)

使用指定的 positionsize 构造 Rect2i


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

构造 Rect2i,将 position 设置为 (x, y),将 size 设置为 (width, height)。


方法说明

Rect2i abs() const 🔗

返回一个与该矩形等效的 Rect2i,其宽度和高度被修改为非负值,其 position 为该矩形的左上角。

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

注意:size 为负时,建议使用该方法,因为 Godot 中的大多数其他方法都假设 position 是左上角,end 是右下角。


bool encloses(b: Rect2i) const 🔗

如果该 Rect2i 完全包含另一个,则返回 true


Rect2i expand(to: Vector2i) const 🔗

返回该矩形的副本,如有必要,该矩形被扩展为将边缘与给定的 to 点对齐。

var rect = Rect2i(0, 0, 5, 2)

rect = rect.expand(Vector2i(10, 0)) # rect 为 Rect2i(0, 0, 10, 2)
rect = rect.expand(Vector2i(-5, 5)) # rect 为 Rect2i(-5, 0, 15, 5)

int get_area() const 🔗

返回该矩形的面积。这相当于 size.x * size.y。另见 has_area()


Vector2i get_center() const 🔗

返回该矩形的中心点。这与 position + (size / 2) 相同。

注意:如果 size 为奇数,则结果将向 position 舍入。


Rect2i grow(amount: int) const 🔗

返回该矩形的副本,该矩形在所有边上扩展给定的 amount。负的 amount 会缩小该矩形。另见 grow_individual()grow_side()

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

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

返回该矩形的副本,其 lefttoprightbottom 边扩展了给定的量。相反,负值会缩小边。另见 grow() and grow_side()


Rect2i grow_side(side: int, amount: int) const 🔗

返回该矩形的副本,其 side 按给定的 amount 扩展(请参阅 Side 常量)。相反,负的 amount 会缩小该矩形。另见 grow()grow_individual()


bool has_area() const 🔗

如果该矩形具有正的宽度和高度,则返回 true。另见 get_area()


bool has_point(point: Vector2i) const 🔗

如果该矩形包含给定的 point,则返回 true。依照惯例,包括右侧和底部边缘上的点。

注意:对于大小为负Rect2i,该方法并不可靠。请首先使用 abs() 获取一个有效的矩形。


Rect2i intersection(b: Rect2i) const 🔗

返回该矩形与 b 之间的交集。如果矩形不相交,则返回空的 Rect2i

var a = Rect2i(0, 0, 5, 10)
var b = Rect2i(2, 0, 8, 4)

var c = a.intersection(b) # c 为 Rect2i(2, 0, 3, 4)

注意:如果你只需要知道两个矩形是否重叠,请改用 intersects()


bool intersects(b: Rect2i) const 🔗

如果该矩形与 b 矩形重叠,则返回 true。两个矩形的边缘均被排除。


Rect2i merge(b: Rect2i) const 🔗

返回一个包含该矩形和边缘周围的 bRect2i。另见 encloses()


运算符说明

bool operator !=(right: Rect2i) 🔗

如果两个矩形的 positionsize 不相等,则返回 true


bool operator ==(right: Rect2i) 🔗

如果该矩形的 positionsize 分别相等,则返回 true