planar-geometry-kit
v0.3.1
Published
2D geometry helpers: distances, polygon area and centroid (shoelace), point-in-polygon, segment intersection, convex hull and the haversine great-circle distance.
Maintainers
Readme
planar-geometry-kit
Small, dependency-free 2D computational-geometry helpers, plus a haversine great-circle distance for the occasional geo problem.
Points are [x, y] tuples. Polygons are arrays of points in either winding order
(functions that care about orientation say so).
Install
npm install planar-geometry-kitUsage
const geom = require("planar-geometry-kit");
geom.distance([0, 0], [3, 4]); // 5
geom.area([[0, 0], [4, 0], [0, 3]]); // 6 (triangle)
geom.centroid([[0, 0], [2, 0], [2, 2], [0, 2]]); // [1, 1]
geom.pointInPolygon([2, 2], [[0,0],[4,0],[4,4],[0,4]]); // true
geom.convexHull([[0,0],[4,0],[4,4],[0,4],[2,2]]); // the 4 corners
geom.haversine([0, 0], [0, 1]); // ~111.19 kmAPI
Points and segments:
distance(a, b),distanceSquared(a, b),midpoint(a, b)cross(a, b, c)— signed cross product; orientation testonSegment(p, q, r)— isqon the collinear segmentp-r?segmentsIntersect(p1, p2, p3, p4)— includes touching/collinear cases
Polygons:
signedArea(poly)/area(poly)— shoelace formulaperimeter(poly)centroid(poly)— area-weighted, with a vertex-average fallback for zero-area inputpointInPolygon(point, poly)— ray casting; boundary points count as insideconvexHull(points)— Andrew's monotone chain, CCW order
Spherical:
haversine(a, b, radius = EARTH_RADIUS_KM)—[lat, lon]in degrees → distanceEARTH_RADIUS_KM— IUGG mean radius (6371.0088 km)
License
MIT
