UNPKG

mindee

Version:

Mindee Client Library for Node.js

127 lines (126 loc) 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCentroid = getCentroid; exports.isPointInY = isPointInY; exports.getMinMaxY = getMinMaxY; exports.isPointInPolygonY = isPointInPolygonY; exports.relativeY = relativeY; exports.isPointInX = isPointInX; exports.getMinMaxX = getMinMaxX; exports.isPointInPolygonX = isPointInPolygonX; exports.relativeX = relativeX; exports.getMinYCoordinate = getMinYCoordinate; exports.getMinXCoordinate = getMinXCoordinate; exports.compareOnY = compareOnY; exports.compareOnX = compareOnX; /** * Get the central point (centroid) given a list of points. */ function getCentroid(vertices) { const numbVertices = vertices.length; const xSum = vertices .map((point) => point[0]) .reduce((prev, cur) => prev + cur); const ySum = vertices .map((point) => point[1]) .reduce((prev, cur) => prev + cur); return [xSum / numbVertices, ySum / numbVertices]; } /** * Determine if a Point is within two Y coordinates. */ function isPointInY(centroid, yMin, yMax) { return yMin <= centroid[1] && centroid[1] <= yMax; } /** * Get the maximum and minimum Y coordinates in a given list of Points. */ function getMinMaxY(vertices) { const points = vertices.map((point) => point[1]); return { min: Math.min(...points), max: Math.max(...points) }; } /** * Determine if a Point is within a Polygon's Y axis. */ function isPointInPolygonY(centroid, polygon) { const yCoords = getMinMaxY(polygon); return isPointInY(centroid, yCoords.min, yCoords.max); } /** * Calculate the relative Y position of a Polygon. * * Can be used to order (sort) words in the same column. */ function relativeY(polygon) { const sum = polygon .map((point) => point[1]) .reduce((prev, cur) => prev + cur); return polygon.length * sum; } /** * Determine if a Point is within two X coordinates. */ function isPointInX(centroid, xMin, xMax) { return xMin <= centroid[0] && centroid[0] <= xMax; } /** * Get the maximum and minimum X coordinates in a given list of Points. */ function getMinMaxX(vertices) { const points = vertices.map((point) => point[0]); return { min: Math.min(...points), max: Math.max(...points) }; } /** * Determine if a Point is within a Polygon's X axis. */ function isPointInPolygonX(centroid, polygon) { const xCoords = getMinMaxX(polygon); return isPointInX(centroid, xCoords.min, xCoords.max); } /** * Calculate the relative X position of a Polygon. * * Can be used to order (sort) words in the same line. */ function relativeX(polygon) { const sum = polygon .map((point) => point[0]) .reduce((prev, cur) => prev + cur); return polygon.length * sum; } function getMinYCoordinate(polygon) { return polygon.sort((point1, point2) => { if (point1[1] < point2[1]) { return -1; } else if (point1[1] > point2[1]) { return 1; } return 0; })[0][1]; } function getMinXCoordinate(polygon) { return polygon.sort((point1, point2) => { if (point1[0] < point2[0]) { return -1; } else if (point1[0] > point2[0]) { return 1; } return 0; })[0][0]; } function compareOnY(polygon1, polygon2) { const sort = getMinYCoordinate(polygon1) - getMinYCoordinate(polygon2); if (sort === 0) { return 0; } return sort < 0 ? -1 : 1; } function compareOnX(polygon1, polygon2) { const sort = getMinXCoordinate(polygon1) - getMinXCoordinate(polygon2); if (sort === 0) { return 0; } return sort < 0 ? -1 : 1; }