GoostGeometry2D

Inherits: Object

Geometry singleton extension.

Description

Performs various geometry-related operations. Provides simple polygon clipping, offsetting and decomposition functionality, and other geometry utility operations mostly revolving around polygons and polylines.

The methods for polygon operations in this class operate on single polygons only. For more advanced operations on multiple polygons, refer to PolyBoolean2D, PolyOffset2D, PolyDecomp2D classes instead.

Methods

Rect2 bounding_rect ( PoolVector2Array points ) const
PoolVector2Array capsule ( float radius, float height, float max_error=0.25 ) const
PoolVector2Array circle ( float radius, float max_error=0.25 ) const
Array clip_polygons ( PoolVector2Array polygon_a, PoolVector2Array polygon_b ) const
Array clip_polyline_with_polygon ( PoolVector2Array polyline, PoolVector2Array polygon ) const
Array decompose_polygon ( PoolVector2Array polygon ) const
Array deflate_polygon ( PoolVector2Array polygon, float delta ) const
Array deflate_polyline ( PoolVector2Array polyline, float delta ) const
PoolVector2Array ellipse ( float width, float height, float max_error=0.25 ) const
Array exclude_polygons ( PoolVector2Array polygon_a, PoolVector2Array polygon_b ) const
Array inflate_polygon ( PoolVector2Array polygon, float delta ) const
Array intersect_polygons ( PoolVector2Array polygon_a, PoolVector2Array polygon_b ) const
Array intersect_polyline_with_polygon ( PoolVector2Array polyline, PoolVector2Array polygon ) const
Array merge_polygons ( PoolVector2Array polygon_a, PoolVector2Array polygon_b ) const
PoolVector2Array pixel_circle ( int radius, Vector2 origin=Vector2( 0, 0 ) ) const
PoolVector2Array pixel_line ( Vector2 start, Vector2 end ) const
int point_in_polygon ( Vector2 point, PoolVector2Array polygon ) const
float polygon_area ( PoolVector2Array polygon ) const
Vector2 polygon_centroid ( PoolVector2Array polygon ) const
float polygon_perimeter ( PoolVector2Array polygon ) const
PoolVector2Array polygon_to_pixels ( PoolVector2Array points ) const
float polyline_length ( PoolVector2Array polyline ) const
PoolVector2Array polyline_to_pixels ( PoolVector2Array points ) const
PoolVector2Array rectangle ( Vector2 extents ) const
PoolVector2Array regular_polygon ( int sides, float size ) const
PoolVector2Array simplify_polyline ( PoolVector2Array polyline, float epsilon ) const
PoolVector2Array smooth_polygon ( PoolVector2Array polygon, float density, float alpha=0.5 ) const
PoolVector2Array smooth_polygon_approx ( PoolVector2Array polygon, int iterations=1, float cut_distance=0.25 ) const
PoolVector2Array smooth_polyline ( PoolVector2Array polyline, float density, float alpha=0.5 ) const
PoolVector2Array smooth_polyline_approx ( PoolVector2Array polyline, int iterations=1, float cut_distance=0.25 ) const
Array triangulate_polygon ( PoolVector2Array polygon ) const

Method Descriptions

Computes the axis-aligned bounding rectangle of given points.


Generates a closed path approximating the shape of a capsule (stadium) with the specified radius and height.

The max_error parameter represents the maximum gap in pixels allowed between capsule’s half circle segment and the boundary of the mathematical circle, with low values increasing the number of vertices, hence accuracy.


Generates a closed path approximating the shape of a circle with the specified radius.

The max_error parameter represents the maximum gap in pixels allowed between a circle segment and the boundary of the mathematical circle, with low values increasing the number of vertices, hence accuracy.


Performs PolyBoolean2D.OP_DIFFERENCE between individual polygons.


Clips a single polyline against a single polygon and returns an array of clipped polylines. This performs PolyBoolean2D.OP_DIFFERENCE between the polyline and the polygon. Returns an empty array if the polygon completely encloses polyline. This operation can be thought of as cutting a line with a closed shape.


Partitions a single polygon into a set of convex polygons with PolyDecomp2D.DECOMP_CONVEX_HM.


Grows a polygon by delta pixels. See also PolyOffset2D.deflate_polygons.


Grows a single non-closed path into a polygon by delta pixels. See also PolyOffset2D.deflate_polylines.


Generates a closed path approximating the shape of an ellipse with specified width and height.

The max_error parameter allows to increase accuracy of generated shape, with low values increasing the number of vertices, which is similar to circle.


Performs PolyBoolean2D.OP_XOR between individual polygons.


Shrinks a polygon by delta pixels. See also PolyOffset2D.inflate_polygons.


Performs PolyBoolean2D.OP_INTERSECTION between individual polygons.


Intersects polyline with polygon and returns an array of intersected polylines. This performs PolyBoolean2D.OP_INTERSECTION between the polyline and the polygon. This operation can be thought of as chopping a line with a closed shape.


Performs PolyBoolean2D.OP_UNION between individual polygons. If you need to merge multiple polygons, use PolyBoolean2D.merge_polygons instead.


Returns an array of 2D-dimensional raster coordinates approximating a circle using a Bresenham type algorithm.

Note: pixels are created per circle’s octant for performance reasons, so you should not rely on the order of returned pixels. If you do need ordered points, consider using methods such as polygon_to_pixels and circle with lower values for max_error parameter, but take in mind that the quality of the returned points is going to be sub-optimal in comparison.


Returns an array of 2D-dimensional raster coordinates going through a segment determined by start and end points using a Bresenham type algorithm. The line is a close approximation to a straight line between those points.


Returns +1 if the point is inside the polygon, 0 if the point is outside the polygon, and -1 if the point is exactly on the polygon’s boundary. Supports arbitrary polygons.


Returns polygon’s positive or negative area depending on whether vertices are ordered in clockwise or counterclockwise order. See also Geometry.is_polygon_clockwise. If the order is clockwise, the polygon can be interpreted as an inner polygon (hole), otherwise it’s an outer polygon (boundary).


Calculates the centroid (also known as “center of mass” or “center of gravity”) of the polygon and returns the consistent result regardless of polygon orientation, see Geometry.is_polygon_clockwise. For accurate results, the polygon must be strictly simple, meaning there should be no self-intersecting edges.


Returns the perimeter of an arbitrary polygon. See also polyline_length.


Returns an array of 2D-dimensional raster coordinates approximating a polygon going through points using pixel_line. Point coordinates in the input points are rounded to nearest integer values.

Note: this method does not fill the interior of the polygon. If you need this to raster polygons onto an image, use GoostImage.render_polygon instead.


Returns the total length of the segments representing the polyline. See also polygon_perimeter.


Returns an array of 2D-dimensional raster coordinates approximating a polyline going through points using pixel_line. Point coordinates in the input points are rounded to nearest integer values.


Generates rectangle vertices grown from the origin to specified half extents in all directions. The total width and height is twice the half extents.


Generates a regular polygon (triangle, rectangle, pentagon, hexagon etc.) with all equal sides and angles. The specified size can be seen as a radius, with an increasing number of sides approximating a circle.

The order of vertices returned is counterclockwise which makes it an outer polygon by default. To convert it to an inner polygon specifically, use PoolVector2Array.invert.


Simplifies a polyline by reducing the number of points using the Ramer-Douglas-Peucker (RDP) algorithm. Higher epsilon values result in fewer points retained.


Smoothers the polygon using the Catmull-Rom’s interpolating spline, resulting in larger number of vertices.

The density parameter configures the desired number of vertices in the output polygon: n = polygon.size() * density, where n is the point count computed. If density < 1.0, returns original polygon. The number of vertices is weighted per segment according to the polygon_perimeter.

The alpha parameter determines the type of the Catmull-Rom’s spline: uniform - alpha == 0, centripetal - alpha == 0.5, chordal - alpha > 0.5. The default value of 0.5 is recommended for eliminating self-intersections and cusps.

For faster, approximate smoothing method, see smooth_polygon_approx.


Approximately smoothers the polygon using the Chaikin’s algorithm resulting in larger number of vertices. Number of iterations can be specified to produce smoother polygons. The cut_distance determines at what distance new control points are selected from segments.

Unlike smooth_polygon, the resulting curve does not go through input vertices, but instead touches the segments of the original polygon.


Smoothers the polyline using the Catmull-Rom’s interpolating spline, resulting in larger number of vertices.

The density parameter configures the desired number of vertices in the output polyline: n = polyline.size() * density, where n is the point count computed. If density < 1.0, returns original polyline. The number of vertices is weighted per segment according to the polyline_length.

The alpha parameter determines the type of the Catmull-Rom’s spline: uniform - alpha == 0, centripetal - alpha == 0.5, chordal - alpha > 0.5. The default value of 0.5 is recommended for eliminating self-intersections and cusps.

For faster, approximate smoothing method, see smooth_polyline_approx.


Approximately smoothers the polyline using the Chaikin’s algorithm resulting in larger number of vertices. Number of iterations can be specified to produce smoother polylines. The cut_distance determines at what distance new control points are selected from segments.

Unlike smooth_polyline, the resulting curve does not go through input vertices, but instead touches the segments of the original polyline.

Unlike smooth_polygon_approx, this method always retains start and end points from the original polyline.


Decomposes the polygon into individual triangles using PolyDecomp2D.DECOMP_TRIANGLES_MONO.

Note that this method is different from Godot’s Geometry.triangulate_polygon, because it doesn’t return indices. If you need to render the resulting triangles, you’ll need to generate a continuous array of vertices and indices yourself. For example:

extends Node2D

func _draw():
    var polygon = GoostGeometry2D.circle(100)
    var triangles = GoostGeometry2D.triangulate_polygon(polygon)

    var vertices = PoolVector2Array()
    for tri in triangles:
        for p in tri:
            vertices.push_back(p)

    var indices = range(vertices.size())

    VisualServer.canvas_item_add_triangle_array(get_canvas_item(), indices, vertices, PoolColorArray([Color.white]))