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

See also:

Static variables

staticread onlylength:Int

The amount of vertices in the polygon.

staticread onlypoints:Array<Point>

The underlying Array of vertices.

Static methods

staticarea(this:Array<Point>):Float

Calculates total area of the Polygon.

staticcentroid(this:Array<Point>):Point

Calculates a centroid of the Polygon and returns its position.

@:value({ isConvex : false })@:noDebugstaticcontains(this:Array<Point>, 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.

staticconvexHull(this:Array<Point>):Array<Point>

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

staticdistance(this:Array<Point>, 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

staticdistanceSq(this:Array<Point>, pt:Point, ?outside:Bool):Float

Same as distance but returns the squared value

staticfastTriangulate(this:Array<Point>):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

staticfindClosestPoint(this:Array<Point>, pt:Point, maxDist:Float):Null<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.

staticgetBounds(this:Array<Point>, ?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 })staticgetCollider(this:Array<Point>, 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.

staticisClockwise(this:Array<Point>):Bool

Tests if polygon points are in the clockwise order.

staticisConvex(this:Array<Point>):Bool

Tests if polygon is convex or concave.

@:value({ npoints : 0 })staticmakeCircle(x:Float, y:Float, radius:Float, npoints:Int = 0):Polygon

staticoptimize(this:Array<Point>, epsilon:Float):Polygon

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

staticprojectPoint(this:Array<Point>, pt:Point, ?out:Point):Null<Point>

Return the closest point on the edges of the polygon

Parameters:

pt

The point to test against.

out

Optional Point instance to which closest point is written. If not provided, returns new Point instance.

Returns:

A Point instance of the closest point on the edges of the polygon.

@:value({ oriented : false })staticrayIntersection(this:Array<Point>, r:Ray, bestMatch:Bool, oriented:Bool = false):Float

staticreverse(this:Array<Point>):Void

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

staticselfIntersecting(this:Array<Point>):Bool

Check if polygon self-intersect

@:value({ scale : 1. })statictoIPolygon(this:Array<Point>, scale:Float = 1.):IPolygon

Converts Polygon to Int-based IPolygon.

statictoSegments(this:Array<Point>):Segments

Returns new Segments instance containing polygon edges.

statictransform(this:Array<Point>, mat:Matrix):Void

Transforms Polygon points by the provided matrix.

statictransformed(this:Array<Point>, mat:Matrix):Polygon

Returns a new transformed Polygon points by the provided matrix.