Rect2i

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

描述

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

浮点数坐标的版本见 Rect2

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

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

备注

通过 C# 使用这个 API 时有显著的不同。详见 C# API 与 GDScript 的差异

教程

属性

Vector2i

end

Vector2i(0, 0)

Vector2i

position

Vector2i(0, 0)

Vector2i

size

Vector2i(0, 0)

构造函数

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 )

方法

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

操作符

bool

operator != ( Rect2i right )

bool

operator == ( Rect2i right )


属性说明

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 ( Rect2i from )

构造给定 Rect2i 的副本。


Rect2i Rect2i ( Rect2 from )

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


Rect2i Rect2i ( Vector2i position, Vector2i size )

使用指定的 positionsize 构造 Rect2i


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

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


方法说明

Rect2i abs ( ) const

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

GDScriptC#

  1. var rect = Rect2i(25, 25, -100, -50)
  2. var absolute = rect.abs() # 绝对值为 Rect2i(-75, -25, 100, 50)
  1. var rect = new Rect2I(25, 25, -100, -50);
  2. var absolute = rect.Abs(); // 绝对值为 Rect2I(-75, -25, 100, 50)

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


bool encloses ( Rect2i b ) const

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


Rect2i expand ( Vector2i to ) const

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

GDScriptC#

  1. var rect = Rect2i(0, 0, 5, 2)
  2. rect = rect.expand(Vector2i(10, 0)) # rect 为 Rect2i(0, 0, 10, 2)
  3. rect = rect.expand(Vector2i(-5, 5)) # rect 为 Rect2i(-5, 0, 10, 5)
  1. var rect = new Rect2I(0, 0, 5, 2);
  2. rect = rect.Expand(new Vector2I(10, 0)); // rect 为 Rect2I(0, 0, 10, 2)
  3. rect = rect.Expand(new Vector2I(-5, 5)); // rect 为 Rect2I(-5, 0, 10, 5)

int get_area ( ) const

返回该矩形的面积。这相当于 size.x * size.y。另请参阅 has_area


Vector2i get_center ( ) const

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

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


Rect2i grow ( int amount ) const

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

GDScriptC#

  1. var a = Rect2i(4, 4, 8, 8).grow(4) # a 为 Rect2i(0, 0, 16, 16)
  2. var b = Rect2i(0, 0, 8, 4).grow(2) # b 为 Rect2i(-2, -2, 12, 8)
  1. var a = new Rect2I(4, 4, 8, 8).Grow(4); // a 为 Rect2I(0, 0, 16, 16)
  2. var b = new Rect2I(0, 0, 8, 4).Grow(2); // b 为 Rect2I(-2, -2, 12, 8)

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

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


Rect2i grow_side ( int side, int amount ) const

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


bool has_area ( ) const

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


bool has_point ( Vector2i point ) const

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

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


Rect2i intersection ( Rect2i b ) const

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

GDScriptC#

  1. var a = Rect2i(0, 0, 5, 10)
  2. var b = Rect2i(2, 0, 8, 4)
  3. var c = a.intersection(b) # c 为 Rect2i(2, 0, 3, 4)
  1. var a = new Rect2I(0, 0, 5, 10);
  2. var b = new Rect2I(2, 0, 8, 4);
  3. var c = rect1.Intersection(rect2); // c 为 Rect2I(2, 0, 3, 4)

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


bool intersects ( Rect2i b ) const

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


Rect2i merge ( Rect2i b ) const

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


操作符说明

bool operator != ( Rect2i right )

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


bool operator == ( Rect2i right )

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

Previous Next


© 版权所有 2014-present Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0). Revision b1c660f7.

Built with Sphinx using a theme provided by Read the Docs.