July 12, 2017 - Søren Alsbjerg Hørup

I needed a polygon library for my latest fun project (a TypeScript Build Engine). Google and NPM showed me the way to PolyK.JS, a very simple Polygon library that can do calculations on simple polygons, including Convex ones.

To my suprise, the library includes TypeScript  definitions making the lib super easy to consume for my application. Supported operations include:

  • GetArea Gets the area of the polygon.
  • Triangulate Triangulates the simple polygon
  • Slice Slices the polygon.
  • Raycast Finds the closest point on the polygon given the intersection of a ray.
  • ClosestEdge Finds the closest edge of the polygon.
  • ContainsPoint Checks if a given point is within the polygon.

The library assumes that the polygon’s vertices are defined in an number[] array of X,Y coordinates. Super easy to use and highly recommended!