Rect2
使用浮點數座標的 2D 軸對齊邊界框。
說明
Rect2內建Variant型別表示二維空間中的軸對齊矩形。它由其position和size定義,分別是Vector2。常用於快速重疊測試(參見intersects())。雖然Rect2本身是軸對齊的,但它可以與Transform2D組合來表示旋轉或傾斜的矩形。
對於整數座標,請使用 Rect2i。與 Rect2 等效的 3D 是 AABB。
注意: 不支援 size 的負值。對於負大小,大多數 Rect2 方法都無法正常運作。使用 abs() 取得具有非負大小的等效 Rect2。
注意: 在布林本文中,如果position 和size 均為零(等於Vector2.ZERO),則Rect2 的計算結果為false )。否則,它的計算結果始終為 true。
備註
使用 C# 操作此 API 時有顯著差異,詳見 C# API 與 GDScript 的不同。
教學
屬性
|
||
|
||
|
建構子
Rect2() |
|
方法
abs() const |
|
get_area() const |
|
get_center() const |
|
get_support(direction: Vector2) const |
|
grow_individual(left: float, top: float, right: float, bottom: float) const |
|
has_area() const |
|
intersection(b: Rect2) const |
|
intersects(b: Rect2, include_borders: bool = false) const |
|
is_equal_approx(rect: Rect2) const |
|
is_finite() const |
|
運算子
operator !=(right: Rect2) |
|
operator *(right: Transform2D) |
|
operator ==(right: Rect2) |
屬性說明
終點角。通過 position + size 計算而來。設定該值會修改大小。
Vector2 position = Vector2(0, 0) 🔗
相機的位置是固定的,所以左上角總是在原點。
Vector2 size = Vector2(0, 0) 🔗
矩形的寬度和高度,從 position 開始。設定此值也會影響 end 點。
注意:建議將寬度和高度設為非負值,因為Godot中的大多數方法都假設position是左上角,end是底部-右上角。若要獲得非負大小的等效矩形,請使用abs()。
建構子說明
預設建構 Rect2,position 和 size 均為預設值(零)。
建構給定 Rect2 的副本。
從 Rect2i 建構 Rect2。
Rect2 Rect2(position: Vector2, size: Vector2)
按位置和大小建構 Rect2。
Rect2 Rect2(x: float, y: float, width: float, height: float)
方法說明
傳回與此矩形等效的Rect2,其寬度和高度修改為非負值,其position為矩形的左上角。
var rect = Rect2(25, 25, -100, -50)
var absolute = rect.abs() # absolute is Rect2(-75, -25, 100, 50)
var rect = new Rect2(25, 25, -100, -50);
var absolute = rect.Abs(); // absolute is Rect2(-75, -25, 100, 50)
注意:當size為負數時,建議使用此方法,因為Godot中的大多數其他方法都假設position是左上角,end是右下角。
bool encloses(b: Rect2) const 🔗
如果該 Rect2i 完全包含另一個,則返回 true。
Rect2 expand(to: Vector2) const 🔗
Returns a copy of this rectangle expanded to align the edges with the given to point, if necessary.
var rect = Rect2(0, 0, 5, 2)
rect = rect.expand(Vector2(10, 0)) # rect is Rect2(0, 0, 10, 2)
rect = rect.expand(Vector2(-5, 5)) # rect is Rect2(-5, 0, 15, 5)
var rect = new Rect2(0, 0, 5, 2);
rect = rect.Expand(new Vector2(10, 0)); // rect is Rect2(0, 0, 10, 2)
rect = rect.Expand(new Vector2(-5, 5)); // rect is Rect2(-5, 0, 15, 5)
返回該 Rect2 的面積。另請參閱 has_area()。
返回該向量的逆向量。與 Vector3(1.0 / v.x, 1.0 / v.y, 1.0 / v.z) 相同。
Vector2 get_support(direction: Vector2) const 🔗
Returns the vertex's position of this rect that's the farthest in the given direction. This point is commonly known as the support point in collision detection algorithms.
Rect2 grow(amount: float) const 🔗
傳回給定的amount 在所有邊上擴充的此矩形的副本。負的amount 會縮小矩形。另請參閱grow_individual() 和grow_side()。
var a = Rect2(4, 4, 8, 8).grow(4) # a is Rect2(0, 0, 16, 16)
var b = Rect2(0, 0, 8, 4).grow(2) # b is Rect2(-2, -2, 12, 8)
var a = new Rect2(4, 4, 8, 8).Grow(4); // a is Rect2(0, 0, 16, 16)
var b = new Rect2(0, 0, 8, 4).Grow(2); // b is Rect2(-2, -2, 12, 8)
Rect2 grow_individual(left: float, top: float, right: float, bottom: float) const 🔗
傳回此矩形的副本,其 left、top、right 和 bottom 邊擴充了給定的量。負值會縮小另請參閱grow() 和grow_side()。
Rect2 grow_side(side: int, amount: float) 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: Vector2) const 🔗
返回 true 時,該 Rect2 包含此點。依照慣例,Rect2 的右邊緣和下邊緣是被排除在外的,因此不包含位於這兩條邊上的點。
注意:對於大小為負的 Rect2,該方法並不可靠。請使用 abs() 獲取等價的正數大小矩形再檢查是否包含某個點。
Rect2 intersection(b: Rect2) const 🔗
傳回此矩形與 b 之間的交集。如果矩形不相交,則傳回空的 Rect2。
var rect1 = Rect2(0, 0, 5, 10)
var rect2 = Rect2(2, 0, 8, 4)
var a = rect1.intersection(rect2) # a is Rect2(2, 0, 3, 4)
var rect1 = new Rect2(0, 0, 5, 10);
var rect2 = new Rect2(2, 0, 8, 4);
var a = rect1.Intersection(rect2); // a is Rect2(2, 0, 3, 4)
注意:如果您只需要知道兩個矩形是否重疊,請改用intersects()。
bool intersects(b: Rect2, include_borders: bool = false) const 🔗
如果該 Rect2i 與 b 重疊(即至少包含一個共同的點),則返回 true。
bool is_equal_approx(rect: Rect2) const 🔗
Returns true if this rectangle and rect are approximately equal, by calling Vector2.is_equal_approx() on the position and the size.
如果該平面是有限的,則返回 true,判斷方法是在每個分量上呼叫 @GlobalScope.is_finite()。
傳回包圍此矩形和邊緣周圍的 b 的 Rect2。另請參閱 encloses().
運算子說明
bool operator !=(right: Rect2) 🔗
如果矩形不相等,則返回 true。
注意:由於浮點數精度誤差,請考慮改用 is_equal_approx(),會更可靠。
Rect2 operator *(right: Transform2D) 🔗
Inversely transforms (multiplies) the Rect2 by the given Transform2D transformation matrix, under the assumption that the transformation basis is orthonormal (i.e. rotation/reflection is fine, scaling/skew is not).
rect * transform is equivalent to transform.inverse() * rect. See Transform2D.inverse().
For transforming by inverse of an affine transformation (e.g. with scaling) transform.affine_inverse() * rect can be used instead. See Transform2D.affine_inverse().
bool operator ==(right: Rect2) 🔗
如果矩形完全相等,則返回 true。
注意:由於浮點數精度誤差,請考慮改用 is_equal_approx(),會更可靠。