An abstract around an Array of Points that define a polygonal shape that can be collision-tested against.

See also:

Variables

@:implread onlylength:Int

The amount of vertices in the polygon.

@:implread onlypoints:Array<Point>

The underlying Array of vertices.

Methods

@:implarea():Float

Calculates total area of the Polygon.

@:implcentroid():Point

Calculates a centroid of the Polygon and returns its position.

@:value({ isConvex : false })@:impl@:noDebugcontains(p:Point, isConvex:Bool = false):Bool

Tests if Point p is inside this Polygon.

Parameters:

p

The point to test against.

isConvex

Use simplified collision test suited for convex polygons. Results are undefined if polygon is concave.

@:implconvexHull():Array<Point>

Returns a new Polygon containing a convex hull of this Polygon. See Monotone chain algorithm for more details.

@:impldistance(pt:Point, ?outside:Bool):Float

Return the distance of pt to the closest edge. If outside is true, only return a positive value if pt is outside the polygon, zero otherwise If outside is false, only return a positive value if pt is inside the polygon, zero otherwise

@:impldistanceSq(pt:Point, ?outside:Bool):Float

Same as distance but returns the squared value

@:implfastTriangulate():Array<Int>

Uses EarCut algorithm to quickly triangulate the polygon. This will not create the best triangulation possible but is quite solid wrt self-intersections and merged points. Returns the points indexes

@:implfindClosestPoint(pt:Point, maxDist:Float):Point

Returns closest Polygon vertex to Point pt within set maximum distance.

Parameters:

pt

The point to test against.

maxDist

Maximum distance vertex can be away from pt before it no longer considered close.

Returns:

A Point instance in the Polygon representing closest vertex (not the copy). null if no vertices were found near the pt within maxDist.

@:implgetBounds(?b:Bounds):Null<Bounds>

Returns bounding box of the Polygon.

Parameters:

b

Optional Bounds instance to be filled. Returns new Bounds instance if null.

@:value({ isConvex : false })@:implgetCollider(isConvex:Bool = false):PolygonCollider

Returns new PolygonCollider instance containing this Polygon.

Parameters:

isConvex

Use simplified collision test suited for convex polygons. Results are undefined if polygon is concave.

@:implisClockwise():Bool

Tests if polygon points are in the clockwise order.

@:implisConvex():Bool

Tests if polygon is convex or concave.

@:imploptimize(epsilon:Float):Polygon

Creates a new optimized polygon by eliminating almost colinear edges according to epsilon distance.

@:implprojectPoint(pt:Point):Point

Return the closest point on the edges of the polygon

@:implrayIntersection(r:Ray, bestMatch:Bool):Float

@:implreverse():Void

Reverses the Polygon points ordering. Can be used to change polygon from anti-clockwise to clockwise.

@:implselfIntersecting():Bool

Check if polygon self-intersect

@:value({ scale : 1. })@:impltoIPolygon(scale:Float = 1.):IPolygon

Converts Polygon to Int-based IPolygon.

@:impltoSegments():Segments

Returns new Segments instance containing polygon edges.

@:impltransform(mat:Matrix):Void

Transforms Polygon points by provided matrix.