Documentation
Polygon is an array of X,Y coordinates. E.g. [0,0, 1,0, 1,1, 0,1] is a rectangle.
In coordinate system, where X goes right, Y goes up and edges are arrows going from lower to higher indexed vertex, the inner side of polygon is on the left side of arrow.
PolyK functions
-
IsSimple ( p : Array ) : Boolean
Checks, if polygon is simple. Polygon is simple, when its edges don't cross each other.
-
IsConvex ( p : Array ) : Boolean
Checks, if polygon is convex. Polygon is convex, when each inner angle is <= 180°.
-
GetArea ( p : Array ) : Number
Returns the area of polygon.
-
GetAABB ( p : Array ) : Object
Returns the Axis-aligned Bounding Box of polygon in this format:
{x:0, y:0, width:0, height:0}Where "x" is the smallest X coordinate of all vertices, "y" is the smallest Y coordinate, "width" and "height" are box dimensions.
-
Triangulate ( p : Array ) : Array
Computes the triangulation. Output array is array of triangles (triangle = 3 indices of polygon vertices). E.g.:
var ids = PolyK.Triangulate([0,0, 1,0, 1,1, 0,1]); // ids == [0,1,2, 0,2,3]
Works with simple polygons only.
-
Slice ( p : Array, ax : Number, ay : Number, bx : Number, by : Number ) : Array
Slices the polygon with line segment A-B, defined by [ax,ay] and [bx,by]. A, B must not lay inside a polygon. Returns an array of polygons.
Works with simple polygons only.
-
ContainsPoint ( p : Array, ax : Number, ay : Number, ) : Boolean
Checks, if polygon contains [ax,ay].
Works with simple polygons only.
-
Raycast ( p : Array, x : Number, y : Number, dx : Number, dy : Number) : Object
Finds the closest point of polygon, which lays on ray defined by [x,y] (origin) and [dx,dy] (direction).
Returns an object in this format:
{dist:0, edge:0, norm:{x:0, y:0}, refl:{x:0, y:0}}"dist" is the distance of the polygon point, "edge" is the number of the edge, on which intersection occurs, "norm" is the normal in that place, "refl" is reflected direction.
Works with simple polygons only.
-
ClosestEdge ( p : Array, x : Number, y : Number) : Object
Finds the point on polygon edges, which is closest to [x,y]. Returns an object in this format:
{dist:0, edge:0, point:{x:0, y:0}, norm:{x:0, y:0}}"dist" is the distance of the polygon point, "edge" is the number of the closest edge, "point" is the closest point on that edge, "norm" is the normal from "point" to [x,y].