UNPKG

mindee

Version:

Mindee Client Library for Node.js

44 lines (43 loc) 1.22 kB
import { getCentroid, getMinMaxX, getMinMaxY, isPointInX, isPointInY } from "./polygonUtils.js"; /** A polygon, composed of several Points. */ export class Polygon extends Array { constructor(...args) { super(...args); } /** * Get the central point (centroid) of the polygon. */ getCentroid() { return getCentroid(this); } /** * Get the maximum and minimum X coordinates of the polygon. */ getMinMaxX() { return getMinMaxX(this); } /** * Get the maximum and minimum Y coordinates of the polygon. */ getMinMaxY() { return getMinMaxY(this); } /** * Determine if a Point is within the Polygon's X axis (same column). */ isPointInX(point) { const xCoords = this.getMinMaxX(); return isPointInX(point, xCoords.min, xCoords.max); } /** * Determine if a Point is within the Polygon's Y axis (same line). */ isPointInY(point) { const yCoords = this.getMinMaxY(); return isPointInY(point, yCoords.min, yCoords.max); } toString() { const points = this.map((point) => `(${point})`).join(", "); return `(${points})`; } }