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.
Checking the stable version of the documentation...
Geometry2D
繼承: Object
提供常見 2D 幾何運算的方法。
說明
為建立幾何形狀,計算形狀之間的交集,以及處理 2D 中的各種其他幾何操作提供了一組輔助函式。
方法
列舉
enum PolyBooleanOperation: 🔗
PolyBooleanOperation OPERATION_UNION = 0
建立區域,其中主體或剪輯多邊形(或兩者)被填充。
PolyBooleanOperation OPERATION_DIFFERENCE = 1
建立主體多邊形被填充的區域,但剪輯多邊形被填充的區域除外。
PolyBooleanOperation OPERATION_INTERSECTION = 2
建立主體和剪輯多邊形都被填充的區域。
PolyBooleanOperation OPERATION_XOR = 3
建立主體或剪輯多邊形被填充的區域,但不是兩者都被填充的區域。
enum PolyJoinType: 🔗
PolyJoinType JOIN_SQUARE = 0
在 1 * delta 的所有凸邊連接處均勻地套用平方。
PolyJoinType JOIN_ROUND = 1
雖然扁平化路徑不可能完美地追蹤一個弧線,但它們可以通過一系列弧弦來近似。
PolyJoinType JOIN_MITER = 2
對斜接有一個必要的限制,因為以非常尖銳的角度連接的邊緣偏移會產生過長和過窄的 "尖刺"。對於任何給定的邊緣連接,當斜接偏移會超過最大距離時,就採用 "方形 "連接。
enum PolyEndType: 🔗
PolyEndType END_POLYGON = 0
端點使用 PolyJoinType 值連接,路徑被填充為多邊形。
PolyEndType END_JOINED = 1
端點使用 PolyJoinType 值連接,路徑被填充為多邊形線。
PolyEndType END_BUTT = 2
端點是方形的,沒有延伸。
PolyEndType END_SQUARE = 3
端點被平方化並擴充了 delta 單位。
PolyEndType END_ROUND = 4
端點被四捨五入,並以 delta 為單位進行擴充。
方法說明
Array[Vector2i] bresenham_line(from: Vector2i, to: Vector2i) 🔗
Returns the Bresenham line between the from and to points. A Bresenham line is a series of pixels that draws a line and is always 1-pixel thick on every row and column of the drawing (never more, never less).
Example code to draw a line between two Marker2D nodes using a series of CanvasItem.draw_rect() calls:
func _draw():
for pixel in Geometry2D.bresenham_line($MarkerA.position, $MarkerB.position):
draw_rect(Rect2(pixel, Vector2.ONE), Color.WHITE)
Array[PackedVector2Array] clip_polygons(polygon_a: PackedVector2Array, polygon_b: PackedVector2Array) 🔗
根據 polygon_b 裁剪 polygon_a,並返回一組裁剪後的多邊形。這會在多邊形之間執行 OPERATION_DIFFERENCE。如果 polygon_b 與 polygon_a 完全重疊,則返回一個空陣列。
如果 polygon_b 被 polygon_a 包圍,則返回一個外多邊形(邊界)和一個內多邊形(孔),可以通過呼叫 is_polygon_clockwise() 來區分。
Array[PackedVector2Array] clip_polyline_with_polygon(polyline: PackedVector2Array, polygon: PackedVector2Array) 🔗
根據 polygon 裁剪 polyline,並返回一組裁剪後的折線。這會在折線和多邊形之間執行 OPERATION_DIFFERENCE。這個操作可以被認為是用一個封閉的形狀切割一條線。
PackedVector2Array convex_hull(points: PackedVector2Array) 🔗
給出一個 Vector2 的陣列,以逆時針的順序返回凸面的點的列表。最後一個點與第一個點相同。
Array[PackedVector2Array] decompose_polygon_in_convex(polygon: PackedVector2Array) 🔗
將 polygon 分解為多個凸面,並返回一個 PackedVector2Array 的陣列。
Array[PackedVector2Array] exclude_polygons(polygon_a: PackedVector2Array, polygon_b: PackedVector2Array) 🔗
相互排除由 polygon_a 和 polygon_b 的交集(參見 intersect_polygons())定義的公共區域,並返回一組排除的多邊形。這會在多邊形之間執行 OPERATION_XOR。換句話說,返回各多邊形之間除公共區域之外的所有區域。
該操作可能會產生一個外多邊形(邊界)和一個內多邊形(孔),這可以通過呼叫 is_polygon_clockwise() 來區分。
Vector2 get_closest_point_to_segment(point: Vector2, s1: Vector2, s2: Vector2) 🔗
返回 2D 線段 (s1, s2) 上與 point 最接近的 2D 點。返回的點始終在指定的線段上。
Vector2 get_closest_point_to_segment_uncapped(point: Vector2, s1: Vector2, s2: Vector2) 🔗
返回 2D 直線 (s1, s2) 上與 point 最接近的 2D 點。返回的點可能在 (s1, s2) 線段上,也有可能不在,即直線上該線段之外的部分。
PackedVector2Array get_closest_points_between_segments(p1: Vector2, q1: Vector2, p2: Vector2, q2: Vector2) 🔗
給定兩個 2D 線段 (p1, q1) 和 (p2, q2),找出這兩個線段見距離最近的兩個點。返回 PackedVector2Array,包含 (p1, q1) 和 (p2, q2) 上的點。
Array[PackedVector2Array] intersect_polygons(polygon_a: PackedVector2Array, polygon_b: PackedVector2Array) 🔗
將 polygon_a 與 polygon_b 相交,並返回一組相交的多邊形。這會在多邊形之間執行 OPERATION_INTERSECTION。換句話說,返回由各多邊形共用的公共區域。如果沒有交集,則返回一個空陣列。
該操作可能會產生一個外多邊形(邊界)和一個內多邊形(孔),這可以通過呼叫 is_polygon_clockwise() 來區分。
Array[PackedVector2Array] intersect_polyline_with_polygon(polyline: PackedVector2Array, polygon: PackedVector2Array) 🔗
將 polyline 與 polygon 相交,並返回一組相交的折線。這會在折線和多邊形之間執行 OPERATION_INTERSECTION。這個操作可以被認為是用一個封閉的形狀切割一條線。
bool is_point_in_circle(point: Vector2, circle_position: Vector2, circle_radius: float) 🔗
如果 point 在圓內或恰好位於圓的邊界上,則返回 true;否則返回 false。
bool is_point_in_polygon(point: Vector2, polygon: PackedVector2Array) 🔗
如果 point 在 polygon 內或者它恰好位於多邊形的邊界上,則返回 true;否則返回 false。
bool is_polygon_clockwise(polygon: PackedVector2Array) 🔗
Returns true if polygon's vertices are ordered in clockwise order, otherwise returns false.
Note: Assumes a Cartesian coordinate system where +x is right and +y is up. If using screen coordinates (+y is down), the result will need to be flipped (i.e. a true result will indicate counter-clockwise).
Variant line_intersects_line(from_a: Vector2, dir_a: Vector2, from_b: Vector2, dir_b: Vector2) 🔗
Returns the point of intersection between the two lines (from_a, dir_a) and (from_b, dir_b). Returns a Vector2, or null if the lines are parallel.
from and dir are not endpoints of a line segment or ray but the slope (dir) and a known point (from) on that line. To get the intersection between two line segments, use segment_intersects_segment().
var from_a = Vector2.ZERO
var dir_a = Vector2.RIGHT
var from_b = Vector2.DOWN
# Returns Vector2(1, 0)
Geometry2D.line_intersects_line(from_a, dir_a, from_b, Vector2(1, -1))
# Returns Vector2(-1, 0)
Geometry2D.line_intersects_line(from_a, dir_a, from_b, Vector2(-1, -1))
# Returns null
Geometry2D.line_intersects_line(from_a, dir_a, from_b, Vector2.RIGHT)
var fromA = Vector2.Zero;
var dirA = Vector2.Right;
var fromB = Vector2.Down;
// Returns new Vector2(1, 0)
Geometry2D.LineIntersectsLine(fromA, dirA, fromB, new Vector2(1, -1));
// Returns new Vector2(-1, 0)
Geometry2D.LineIntersectsLine(fromA, dirA, fromB, new Vector2(-1, -1));
// Returns null
Geometry2D.LineIntersectsLine(fromA, dirA, fromB, Vector2.Right);
Dictionary make_atlas(sizes: PackedVector2Array) 🔗
給定一個表示圖塊的 Vector2 陣列,建構一個合集。返回的字典有兩個鍵:points 是一個 PackedVector2Array,指定每個圖塊的位置;size 包含整個合集的整體大小,是一個 Vector2i。
Array[PackedVector2Array] merge_polygons(polygon_a: PackedVector2Array, polygon_b: PackedVector2Array) 🔗
合併(結合)polygon_a 和 polygon_b,並返回一組合並的多邊形。這在多邊形之間執行 OPERATION_UNION。
該操作可能會產生一個外部多邊形(邊界)和多個內部多邊形(孔),可以通過呼叫 is_polygon_clockwise() 來區分它們。
Array[PackedVector2Array] offset_polygon(polygon: PackedVector2Array, delta: float, join_type: PolyJoinType = 0) 🔗
Inflates or deflates polygon by delta units (pixels). If delta is positive, makes the polygon grow outward. If delta is negative, shrinks the polygon inward. Returns an array of polygons because inflating/deflating may result in multiple discrete polygons. Returns an empty array if delta is negative and the absolute value of it approximately exceeds the minimum bounding rectangle dimensions of the polygon.
Each polygon's vertices will be rounded as determined by join_type.
The operation may result in an outer polygon (boundary) and inner polygon (hole) produced which could be distinguished by calling is_polygon_clockwise().
Note: To translate the polygon's vertices specifically, multiply them to a Transform2D:
var polygon = PackedVector2Array([Vector2(0, 0), Vector2(100, 0), Vector2(100, 100), Vector2(0, 100)])
var offset = Vector2(50, 50)
polygon = Transform2D(0, offset) * polygon
print(polygon) # Prints [(50.0, 50.0), (150.0, 50.0), (150.0, 150.0), (50.0, 150.0)]
Vector2[] polygon = [new Vector2(0, 0), new Vector2(100, 0), new Vector2(100, 100), new Vector2(0, 100)];
var offset = new Vector2(50, 50);
polygon = new Transform2D(0, offset) * polygon;
GD.Print((Variant)polygon); // Prints [(50, 50), (150, 50), (150, 150), (50, 150)]
Array[PackedVector2Array] offset_polyline(polyline: PackedVector2Array, delta: float, join_type: PolyJoinType = 0, end_type: PolyEndType = 3) 🔗
Inflates or deflates polyline by delta units (pixels), producing polygons. If delta is positive, makes the polyline grow outward. Returns an array of polygons because inflating/deflating may result in multiple discrete polygons. If delta is negative, returns an empty array.
Each polygon's vertices will be rounded as determined by join_type.
Each polygon's endpoints will be rounded as determined by end_type.
The operation may result in an outer polygon (boundary) and inner polygon (hole) produced which could be distinguished by calling is_polygon_clockwise().
bool point_is_inside_triangle(point: Vector2, a: Vector2, b: Vector2, c: Vector2) const 🔗
返回 point 是否位於由 a、b、c 指定的三角形中。
float segment_intersects_circle(segment_from: Vector2, segment_to: Vector2, circle_position: Vector2, circle_radius: float) 🔗
給定 2D 線段 (segment_from, segment_to),返回該線段與圓相交的位置(0 和 1 之間的數),這個圓位於 circle_position,半徑為 circle_radius。如果該線段與圓不相交,則返回 -1(同樣也適用於這條線段的延長線所在的直線與圓相交,而線段本身不相交的情況)。
Variant segment_intersects_segment(from_a: Vector2, to_a: Vector2, from_b: Vector2, to_b: Vector2) 🔗
Checks if two line segments intersect, with line a between from_a and to_a and line b between from_b and to_b. If the line segments intersect, the point of intersection is returned as a Vector2. If no intersection takes place, null is returned.
PackedInt32Array triangulate_delaunay(points: PackedVector2Array) 🔗
對由離散的 points 集指定的區域進行三角化,使得任何點都不在任何生成的三角形的外接圓內。返回一個 PackedInt32Array,其中每個三角形由 points 中的三個連續點的索引組成(即返回的陣列將具有 n * 3 個元素,其中 n 是找到的三角形的數量)。如果三角化沒有成功,則返回一個空的 PackedInt32Array。
PackedInt32Array triangulate_polygon(polygon: PackedVector2Array) 🔗
對由 polygon 中的點指定的多邊形進行三角化。 返回一個 PackedInt32Array,其中每個三角形由 polygon 中的三個連續點的索引組成(即返回的陣列將具有 n * 3 個元素,其中 n 是找到的三角形的數量)。輸出三角形將始終為逆時針旋轉,如果為順時針旋轉,則輪廓將翻轉。如果三角化沒有成功,則返回一個空的 PackedInt32Array。