Geometry2D
Provides methods for some common 2D geometric operations.
Geometry2D.swift:11class Geometry2D
Provides a set of helper functions to create geometric shapes, compute intersections between shapes, and process various other geometric operations in 2D.
Superclasses
class Object
Base class for all other classes in the engine.
Citizens in SwiftGodot
Conformances
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Types
Type members
static func clipPolygons(polygonA: PackedVector2Array, polygonB: PackedVector2Array
) -> VariantCollection<PackedVector2Array> Clips
polygonA
againstpolygonB
and returns an array of clipped polygons. This performs .operationDifference between polygons. Returns an empty array ifpolygonB
completely overlapspolygonA
.static func clipPolylineWithPolygon(polyline: PackedVector2Array, polygon: PackedVector2Array
) -> VariantCollection<PackedVector2Array> Clips
polyline
againstpolygon
and returns an array of clipped polylines. This performs .operationDifference between the polyline and the polygon. This operation can be thought of as cutting a line with a closed shape.static func convexHull(points: PackedVector2Array
) -> PackedVector2Array Given an array of
Vector2
s, returns the convex hull as a list of points in counterclockwise order. The last point is the same as the first one.static func decomposePolygonInConvex(polygon: PackedVector2Array
) -> VariantCollection<PackedVector2Array> Decomposes the
polygon
into multiple convex hulls and returns an array ofPackedVector2Array
.static func excludePolygons(polygonA: PackedVector2Array, polygonB: PackedVector2Array
) -> VariantCollection<PackedVector2Array> Mutually excludes common area defined by intersection of
polygonA
andpolygonB
(seeintersectPolygons(polygonA:polygonB:)
) and returns an array of excluded polygons. This performs .operationXor between polygons. In other words, returns all but common area between polygons.static func getClosestPointToSegment(point: Vector2, s1: Vector2, s2: Vector2
) -> Vector2 Returns the 2D point on the 2D segment (
s1
,s2
) that is closest topoint
. The returned point will always be inside the specified segment.static func getClosestPointToSegmentUncapped(point: Vector2, s1: Vector2, s2: Vector2
) -> Vector2 Returns the 2D point on the 2D line defined by (
s1
,s2
) that is closest topoint
. The returned point can be inside the segment (s1
,s2
) or outside of it, i.e. somewhere on the line extending from the segment.static func getClosestPointsBetweenSegments(p1: Vector2, q1: Vector2, p2: Vector2, q2: Vector2
) -> PackedVector2Array Given the two 2D segments (
p1
,q1
) and (p2
,q2
), finds those two points on the two segments that are closest to each other. Returns aPackedVector2Array
that contains this point on (p1
,q1
) as well the accompanying point on (p2
,q2
).static func intersectPolygons(polygonA: PackedVector2Array, polygonB: PackedVector2Array
) -> VariantCollection<PackedVector2Array> Intersects
polygonA
withpolygonB
and returns an array of intersected polygons. This performs .operationIntersection between polygons. In other words, returns common area shared by polygons. Returns an empty array if no intersection occurs.static func intersectPolylineWithPolygon(polyline: PackedVector2Array, polygon: PackedVector2Array
) -> VariantCollection<PackedVector2Array> Intersects
polyline
withpolygon
and returns an array of intersected polylines. This performs .operationIntersection between the polyline and the polygon. This operation can be thought of as chopping a line with a closed shape.static func isPointInCircle(point: Vector2, circlePosition: Vector2, circleRadius: Double
) -> Bool Returns
true
ifpoint
is inside the circle or if it’s located exactly on the circle’s boundary, otherwise returnsfalse
.static func isPointInPolygon(point: Vector2, polygon: PackedVector2Array
) -> Bool Returns
true
ifpoint
is insidepolygon
or if it’s located exactly on polygon’s boundary, otherwise returnsfalse
.static func isPolygonClockwise(polygon: PackedVector2Array
) -> Bool Returns
true
ifpolygon
’s vertices are ordered in clockwise order, otherwise returnsfalse
.static func lineIntersectsLine(fromA: Vector2, dirA: Vector2, fromB: Vector2, dirB: Vector2
) -> Variant Checks if the two lines (
fromA
,dirA
) and (fromB
,dirB
) intersect. If yes, return the point of intersection asVector2
. If no intersection takes place, returnsnull
.static func makeAtlas(sizes: PackedVector2Array
) -> GDictionary Given an array of
Vector2
s representing tiles, builds an atlas. The returned dictionary has two keys:points
is aPackedVector2Array
that specifies the positions of each tile,size
contains the overall size of the whole atlas asVector2i
.static func mergePolygons(polygonA: PackedVector2Array, polygonB: PackedVector2Array
) -> VariantCollection<PackedVector2Array> Merges (combines)
polygonA
andpolygonB
and returns an array of merged polygons. This performs .operationUnion between polygons.static func offsetPolygon(PackedVector2Array, delta: Double, joinType: Geometry2D.PolyJoinType
) -> VariantCollection<PackedVector2Array> Inflates or deflates
polygon
bydelta
units (pixels). Ifdelta
is positive, makes the polygon grow outward. Ifdelta
is negative, shrinks the polygon inward. Returns an array of polygons because inflating/deflating may result in multiple discrete polygons. Returns an empty array ifdelta
is negative and the absolute value of it approximately exceeds the minimum bounding rectangle dimensions of the polygon.static func offsetPolyline(PackedVector2Array, delta: Double, joinType: Geometry2D.PolyJoinType, endType: Geometry2D.PolyEndType
) -> VariantCollection<PackedVector2Array> Inflates or deflates
polyline
bydelta
units (pixels), producing polygons. Ifdelta
is positive, makes the polyline grow outward. Returns an array of polygons because inflating/deflating may result in multiple discrete polygons. Ifdelta
is negative, returns an empty array.static func pointIsInsideTriangle(point: Vector2, a: Vector2, b: Vector2, c: Vector2
) -> Bool Returns if
point
is inside the triangle specified bya
,b
andc
.static func segmentIntersectsCircle(segmentFrom: Vector2, segmentTo: Vector2, circlePosition: Vector2, circleRadius: Double
) -> Double Given the 2D segment (
segmentFrom
,segmentTo
), returns the position on the segment (as a number between 0 and 1) at which the segment hits the circle that is located at positioncirclePosition
and has radiuscircleRadius
. If the segment does not intersect the circle, -1 is returned (this is also the case if the line extending the segment would intersect the circle, but the segment does not).static func segmentIntersectsSegment(fromA: Vector2, toA: Vector2, fromB: Vector2, toB: Vector2
) -> Variant Checks if the two segments (
fromA
,toA
) and (fromB
,toB
) intersect. If yes, return the point of intersection asVector2
. If no intersection takes place, returnsnull
.static func triangulateDelaunay(points: PackedVector2Array
) -> PackedInt32Array Triangulates the area specified by discrete set of
points
such that no point is inside the circumcircle of any resulting triangle. Returns aPackedInt32Array
where each triangle consists of three consecutive point indices intopoints
(i.e. the returned array will haven * 3
elements, withn
being the number of found triangles). If the triangulation did not succeed, an emptyPackedInt32Array
is returned.static func triangulatePolygon(PackedVector2Array
) -> PackedInt32Array Triangulates the polygon specified by the points in
polygon
. Returns aPackedInt32Array
where each triangle consists of three consecutive point indices intopolygon
(i.e. the returned array will haven * 3
elements, withn
being the number of found triangles). Output triangles will always be counter clockwise, and the contour will be flipped if it’s clockwise. If the triangulation did not succeed, an emptyPackedInt32Array
is returned.class var godotClassName: StringName