Rect2i
使用整數座標的 2D 軸對齊邊界框。
說明
Rect2i 內建 Variant 型別表示 2D 空間中的軸對齊矩形,使用整數座標。它由其 position 和 size 定義,即Vector2i,由於它不會旋轉,所以常用於快速重疊測試(參見intersects())。
浮點座標,請參見Rect2。
注意: 不支援 size 的負值。對於負大小,大多數 Rect2i 方法都無法正常運作。使用 abs() 獲得具有非負大小的等效 Rect2i。
注意: 在布林本文中,如果position 和size 均為零(等於Vector2i.ZERO),則Rect2i 的計算結果為false )。否則,它的計算結果始終為 true。
備註
使用 C# 操作此 API 時有顯著差異,詳見 C# API 與 GDScript 的不同。
教學
屬性
|
||
|
||
|
建構子
Rect2i() |
|
方法
abs() const |
|
get_area() const |
|
get_center() const |
|
grow_individual(left: int, top: int, right: int, bottom: int) const |
|
has_area() const |
|
intersection(b: Rect2i) const |
|
intersects(b: Rect2i) const |
|
運算子
operator !=(right: Rect2i) |
|
operator ==(right: Rect2i) |
屬性說明
Vector2i end = Vector2i(0, 0) 🔗
終點角。通過 position + size 計算而來。設定該值會修改大小。
Vector2i position = Vector2i(0, 0) 🔗
相機的位置是固定的,所以左上角總是在原點。
Vector2i size = Vector2i(0, 0) 🔗
矩形的寬度和高度,從 position 開始。設定此值也會影響 end 點。
注意:建議將寬度和高度設為非負值,因為Godot中的大多數方法都假設position是左上角,end是底部-右上角。若要獲得非負大小的等效矩形,請使用abs()。
建構子說明
預設建構 Rect2i,position 和 size 均為預設值(零)。
建構給定 Rect2i 的副本。
從 Rect2 建構新的 Rect2i。浮點數座標將被截斷。
Rect2i Rect2i(position: Vector2i, size: Vector2i)
按位置和大小建構 Rect2i。
方法說明
傳回與此矩形等效的Rect2i,其寬度和高度修改為非負值,其position為矩形的左上角。
var rect = Rect2i(25, 25, -100, -50)
var absolute = rect.abs() # absolute is Rect2i(-75, -25, 100, 50)
var rect = new Rect2I(25, 25, -100, -50);
var absolute = rect.Abs(); // absolute is Rect2I(-75, -25, 100, 50)
注意:當size為負數時,建議使用此方法,因為Godot中的大多數其他方法都假設position是左上角,end是右下角。
bool encloses(b: Rect2i) const 🔗
如果該 Rect2i 完全包含另一個,則返回 true。
Rect2i expand(to: Vector2i) 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, 15, 5)
var rect = new Rect2I(0, 0, 5, 2);
rect = rect.Expand(new Vector2I(10, 0)); // rect is Rect2I(0, 0, 10, 2)
rect = rect.Expand(new Vector2I(-5, 5)); // rect is Rect2I(-5, 0, 15, 5)
返回該 Rect2 的面積。另請參閱 has_area()。
返回該 Rect2i 的中心,等於 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 is Rect2i(0, 0, 16, 16)
var b = Rect2i(0, 0, 8, 4).grow(2) # b is Rect2i(-2, -2, 12, 8)
var a = new Rect2I(4, 4, 8, 8).Grow(4); // a is Rect2I(0, 0, 16, 16)
var b = new Rect2I(0, 0, 8, 4).Grow(2); // b is Rect2I(-2, -2, 12, 8)
Rect2i grow_individual(left: int, top: int, right: int, bottom: int) const 🔗
傳回此矩形的副本,其 left、top、right 和 bottom 邊擴充了給定的量。負值會縮小另請參閱grow() 和grow_side()。
Rect2i grow_side(side: int, amount: int) const 🔗
傳回此矩形的副本,其 side 按給定的 amount 擴充(請參閱 Side 常數)。負數 amount 會縮小矩形,相反。另請參見 grow() 和 grow_individual()。
Returns true if this rectangle has positive width and height. See also get_area().
bool has_point(point: Vector2i) const 🔗
返回 true 時,該 Rect2 包含此點。依照慣例,Rect2 的右邊緣和下邊緣是被排除在外的,因此不包含位於這兩條邊上的點。
注意:對於大小為負的 Rect2,該方法並不可靠。請使用 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 is Rect2i(2, 0, 3, 4)
var a = new Rect2I(0, 0, 5, 10);
var b = new Rect2I(2, 0, 8, 4);
var c = rect1.Intersection(rect2); // c is Rect2I(2, 0, 3, 4)
注意:如果您只需要知道兩個矩形是否重疊,請改用intersects()。
bool intersects(b: Rect2i) const 🔗
如果該 Rect2i 與 b 重疊(即至少包含一個共同的點),則返回 true。
Rect2i merge(b: Rect2i) const 🔗
傳回包圍此矩形和邊緣周圍的b 的 Rect2i 。另請參閱 encloses()。
運算子說明
bool operator !=(right: Rect2i) 🔗
如果矩形不相等,則返回 true。
bool operator ==(right: Rect2i) 🔗
如果矩形相等,則返回 true。