Class Geometry2D
- Namespace
- Godot
- Assembly
- GodotSharp.dll
Provides a set of helper functions to create geometric shapes, compute intersections between shapes, and process various other geometric operations in 2D.
public static class Geometry2D
- Inheritance
-
Geometry2D
- Inherited Members
Properties
Singleton
public static Geometry2DInstance Singleton { get; }
Property Value
Methods
ClipPolygons(Vector2[], Vector2[])
Clips polygonA against polygonB and returns an array of clipped polygons. This performs Difference between polygons. Returns an empty array if polygonB completely overlaps polygonA.
If polygonB is enclosed by polygonA, returns an outer polygon (boundary) and inner polygon (hole) which could be distinguished by calling IsPolygonClockwise(Vector2[]).
public static Array<Vector2[]> ClipPolygons(Vector2[] polygonA, Vector2[] polygonB)
Parameters
Returns
ClipPolylineWithPolygon(Vector2[], Vector2[])
Clips polyline against polygon and returns an array of clipped polylines. This performs Difference between the polyline and the polygon. This operation can be thought of as cutting a line with a closed shape.
public static Array<Vector2[]> ClipPolylineWithPolygon(Vector2[] polyline, Vector2[] polygon)
Parameters
Returns
ConvexHull(Vector2[])
Given an array of Vector2s, returns the convex hull as a list of points in counterclockwise order. The last point is the same as the first one.
public static Vector2[] ConvexHull(Vector2[] points)
Parameters
pointsVector2[]
Returns
- Vector2[]
DecomposePolygonInConvex(Vector2[])
Decomposes the polygon into multiple convex hulls and returns an array of Vector2[].
public static Array<Vector2[]> DecomposePolygonInConvex(Vector2[] polygon)
Parameters
polygonVector2[]
Returns
ExcludePolygons(Vector2[], Vector2[])
Mutually excludes common area defined by intersection of polygonA and polygonB (see IntersectPolygons(Vector2[], Vector2[])) and returns an array of excluded polygons. This performs Xor between polygons. In other words, returns all but common area between polygons.
The operation may result in an outer polygon (boundary) and inner polygon (hole) produced which could be distinguished by calling IsPolygonClockwise(Vector2[]).
public static Array<Vector2[]> ExcludePolygons(Vector2[] polygonA, Vector2[] polygonB)
Parameters
Returns
GetClosestPointToSegment(Vector2, Vector2, Vector2)
Returns the 2D point on the 2D segment (s1, s2) that is closest to point. The returned point will always be inside the specified segment.
public static Vector2 GetClosestPointToSegment(Vector2 point, Vector2 s1, Vector2 s2)
Parameters
Returns
GetClosestPointToSegmentUncapped(Vector2, Vector2, Vector2)
Returns the 2D point on the 2D line defined by (s1, s2) that is closest to point. The returned point can be inside the segment (s1, s2) or outside of it, i.e. somewhere on the line extending from the segment.
public static Vector2 GetClosestPointToSegmentUncapped(Vector2 point, Vector2 s1, Vector2 s2)
Parameters
Returns
GetClosestPointsBetweenSegments(Vector2, Vector2, Vector2, Vector2)
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 a Vector2[] that contains this point on (p1, q1) as well the accompanying point on (p2, q2).
public static Vector2[] GetClosestPointsBetweenSegments(Vector2 p1, Vector2 q1, Vector2 p2, Vector2 q2)
Parameters
Returns
- Vector2[]
IntersectPolygons(Vector2[], Vector2[])
Intersects polygonA with polygonB and returns an array of intersected polygons. This performs Intersection between polygons. In other words, returns common area shared by polygons. Returns an empty array if no intersection occurs.
The operation may result in an outer polygon (boundary) and inner polygon (hole) produced which could be distinguished by calling IsPolygonClockwise(Vector2[]).
public static Array<Vector2[]> IntersectPolygons(Vector2[] polygonA, Vector2[] polygonB)
Parameters
Returns
IntersectPolylineWithPolygon(Vector2[], Vector2[])
Intersects polyline with polygon and returns an array of intersected polylines. This performs Intersection between the polyline and the polygon. This operation can be thought of as chopping a line with a closed shape.
public static Array<Vector2[]> IntersectPolylineWithPolygon(Vector2[] polyline, Vector2[] polygon)
Parameters
Returns
IsPointInCircle(Vector2, Vector2, float)
Returns true if point is inside the circle or if it's located exactly on the circle's boundary, otherwise returns false.
public static bool IsPointInCircle(Vector2 point, Vector2 circlePosition, float circleRadius)
Parameters
Returns
IsPointInPolygon(Vector2, Vector2[])
Returns true if point is inside polygon or if it's located exactly on polygon's boundary, otherwise returns false.
public static bool IsPointInPolygon(Vector2 point, Vector2[] polygon)
Parameters
Returns
IsPolygonClockwise(Vector2[])
Returns true if polygon's vertices are ordered in clockwise order, otherwise returns false.
public static bool IsPolygonClockwise(Vector2[] polygon)
Parameters
polygonVector2[]
Returns
LineIntersectsLine(Vector2, Vector2, Vector2, Vector2)
Checks if the two lines (fromA, dirA) and (fromB, dirB) intersect. If yes, return the point of intersection as Vector2. If no intersection takes place, returns null.
Note: The lines are specified using direction vectors, not end points.
public static Variant LineIntersectsLine(Vector2 fromA, Vector2 dirA, Vector2 fromB, Vector2 dirB)
Parameters
Returns
MakeAtlas(Vector2[])
Given an array of Vector2s representing tiles, builds an atlas. The returned dictionary has two keys: points is a Vector2[] that specifies the positions of each tile, size contains the overall size of the whole atlas as Vector2I.
public static Dictionary MakeAtlas(Vector2[] sizes)
Parameters
sizesVector2[]
Returns
MergePolygons(Vector2[], Vector2[])
Merges (combines) polygonA and polygonB and returns an array of merged polygons. This performs Union between polygons.
The operation may result in an outer polygon (boundary) and multiple inner polygons (holes) produced which could be distinguished by calling IsPolygonClockwise(Vector2[]).
public static Array<Vector2[]> MergePolygons(Vector2[] polygonA, Vector2[] polygonB)
Parameters
Returns
OffsetPolygon(Vector2[], float, PolyJoinType)
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 joinType, see Geometry2D.PolyJoinType.
The operation may result in an outer polygon (boundary) and inner polygon (hole) produced which could be distinguished by calling IsPolygonClockwise(Vector2[]).
Note: To translate the polygon's vertices specifically, multiply them to a Transform2D:
var polygon = new Vector2[] { 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)]
public static Array<Vector2[]> OffsetPolygon(Vector2[] polygon, float delta, Geometry2D.PolyJoinType joinType = PolyJoinType.Square)
Parameters
polygonVector2[]deltafloatjoinTypeGeometry2D.PolyJoinType
Returns
OffsetPolyline(Vector2[], float, PolyJoinType, PolyEndType)
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 joinType, see Geometry2D.PolyJoinType.
Each polygon's endpoints will be rounded as determined by endType, see Geometry2D.PolyEndType.
The operation may result in an outer polygon (boundary) and inner polygon (hole) produced which could be distinguished by calling IsPolygonClockwise(Vector2[]).
public static Array<Vector2[]> OffsetPolyline(Vector2[] polyline, float delta, Geometry2D.PolyJoinType joinType = PolyJoinType.Square, Geometry2D.PolyEndType endType = PolyEndType.Square)
Parameters
polylineVector2[]deltafloatjoinTypeGeometry2D.PolyJoinTypeendTypeGeometry2D.PolyEndType
Returns
PointIsInsideTriangle(Vector2, Vector2, Vector2, Vector2)
Returns if point is inside the triangle specified by a, b and c.
public static bool PointIsInsideTriangle(Vector2 point, Vector2 a, Vector2 b, Vector2 c)
Parameters
Returns
SegmentIntersectsCircle(Vector2, Vector2, Vector2, float)
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 position circlePosition and has radius circleRadius. 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).
public static float SegmentIntersectsCircle(Vector2 segmentFrom, Vector2 segmentTo, Vector2 circlePosition, float circleRadius)
Parameters
Returns
SegmentIntersectsSegment(Vector2, Vector2, Vector2, Vector2)
Checks if the two segments (fromA, toA) and (fromB, toB) intersect. If yes, return the point of intersection as Vector2. If no intersection takes place, returns null.
public static Variant SegmentIntersectsSegment(Vector2 fromA, Vector2 toA, Vector2 fromB, Vector2 toB)
Parameters
Returns
TriangulateDelaunay(Vector2[])
Triangulates the area specified by discrete set of points such that no point is inside the circumcircle of any resulting triangle. Returns a int[] where each triangle consists of three consecutive point indices into points (i.e. the returned array will have n * 3 elements, with n being the number of found triangles). If the triangulation did not succeed, an empty int[] is returned.
public static int[] TriangulateDelaunay(Vector2[] points)
Parameters
pointsVector2[]
Returns
- int[]
TriangulatePolygon(Vector2[])
Triangulates the polygon specified by the points in polygon. Returns a int[] where each triangle consists of three consecutive point indices into polygon (i.e. the returned array will have n * 3 elements, with n 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 empty int[] is returned.
public static int[] TriangulatePolygon(Vector2[] polygon)
Parameters
polygonVector2[]
Returns
- int[]