geometry-fns
A lightweight geometric algorithm library
Installation
npm add geometry-fns
pnpm add geometry-fns
yarn add geometry-fns
<script src="https://cdn.jsdelivr.net/npm/geometry-fns/dist/index.umd.min.js"></script>
<script>
// Global variable GeometryFns available
</script>
API Documentation
Core Functions
areIntersected(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, x4: number, y4: number): boolean
Determine if two line segments intersectInput:
- Coordinates of two line segments (x1,y1)-(x2,y2) and (x3,y3)-(x4,y4)
Returns:
- true (intersecting) | false (non-intersecting)
Expand to view examples
tsimport { areIntersected } from 'geometry-fns'; // Check intersection between two line segments areIntersected(381, 216, 211, 216, 211, 124, 381, 124); // false // Practical example: Check line crossing areIntersected(0, 0, 2, 2, 0, 2, 2, 0); // true
centroidOf(points: number[]): [number, number] | null
Calculate polygon centroid coordinatesInput:
- Polygon vertex coordinates [x1,y1,x2,y2,...]
Returns:
- [cx, cy] coordinate pair or null (invalid input)
Expand to view examples
tsimport { centroidOf } from 'geometry-fns'; // Calculate rectangle centroid centroidOf([381, 216, 211, 216, 211, 124, 381, 124]); // [296, 170] // Calculate triangle centroid centroidOf([154, 97, 341, 95, 255, 240]); // [250, 144] // Handle invalid input centroidOf([]); // null centroidOf([1, 1, 2, 2]); // [1.5, 1.5]
crossProduct(x1: number, y1: number, x2: number, y2: number): number
Calculate 2D vector cross productInput:
- Two vectors represented by coordinates (x1,y1) and (x2,y2)
Returns:
- Positive (counter-clockwise), Negative (clockwise), Zero (collinear)
Expand to view examples
tsimport { crossProduct } from 'geometry-fns'; // Basic cross product calculation crossProduct(1, 0, 0, 1); // 1 (counter-clockwise) crossProduct(0, 1, 1, 0); // -1 (clockwise) // Check if three points are collinear function isCollinear(p1, p2, p3) { return crossProduct(p2.x - p1.x, p2.y - p1.y, p3.x - p1.x, p3.y - p1.y) === 0; } // Determine point position relative to line segment function pointLocation(ax, ay, bx, by, px, py) { const cp = crossProduct(bx - ax, by - ay, px - ax, py - ay); return cp > 0 ? 'left' : cp < 0 ? 'right' : 'onLine'; }
distanceBetween(x1: number, y1: number, x2: number, y2: number): number
Calculate Euclidean distance between two pointsInput:
- Coordinates of two points (x1,y1) and (x2,y2)
Returns:
- Distance value ≥ 0
Expand to view examples
tsimport { distanceBetween } from 'geometry-fns'; // Calculate horizontal distance (same y-coordinate) distanceBetween(381, 216, 211, 216); // 170 // Calculate general distance distanceBetween(0, 0, 3, 4); // 5
isConvexPolygon(points: number[]): boolean
Determine if a polygon is convexInput:
- Polygon vertex coordinates [x1,y1,x2,y2,...]
Returns:
- true (convex) | false (concave/invalid input)
Expand to view examples
tsimport { isConvexPolygon } from 'geometry-fns'; // Check rectangle convexity (rectangles are always convex) isConvexPolygon([381, 216, 211, 216, 211, 124, 381, 124]); // true // Handle invalid coordinates isConvexPolygon([381, 216, 211, 216, 211]); // false // Check triangle convexity isConvexPolygon([244, 92, 322, 205, 430, 105]); // true
lineCrossedQuadrangle(lineCoords: number[], quadCoords: number[]): number[][] | null
Determine intersection between line and quadrilateralInput:
- lineCoords: [x1,y1,x2,y2] line segment coordinates
- quadCoords: [x1,y1,x2,y2,x3,y3,x4,y4] quadrilateral vertices
Returns:
- Array of intersection points [[x1,y1,x2,y2], ...] or null (no intersection)
Expand to view examples
tsimport { lineCrossedQuadrangle } from 'geometry-fns'; // Check line-quadrilateral intersection lineCrossedQuadrangle( [0.2387, 0.2622, 0.4462, 0.76], // line coordinates [0.3187, 0.4, 0.51, 0.5311, 0.305, 0.6556, 0.1837, 0.3422] // quadrilateral vertices ); // Returns intersection points: [[0.51, 0.5311, 0.305, 0.6556], [0.1837, 0.3422, 0.3187, 0.4]] // No intersection case lineCrossedQuadrangle( [0.2387, 0.2622, 0.235, 0.7444], [] // empty quadrilateral ); // null