UNPKG

mindee

Version:

Mindee Client Library for Node.js

124 lines (123 loc) 3.55 kB
/** * Get the central point (centroid) given a list of points. */ export 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. */ export 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. */ export 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. */ export 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. */ export 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. */ export 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. */ export 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. */ export 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. */ export function relativeX(polygon) { const sum = polygon .map((point) => point[0]) .reduce((prev, cur) => prev + cur); return polygon.length * sum; } export 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]; } export 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]; } export function compareOnY(polygon1, polygon2) { const sort = getMinYCoordinate(polygon1) - getMinYCoordinate(polygon2); if (sort === 0) { return 0; } return sort < 0 ? -1 : 1; } export function compareOnX(polygon1, polygon2) { const sort = getMinXCoordinate(polygon1) - getMinXCoordinate(polygon2); if (sort === 0) { return 0; } return sort < 0 ? -1 : 1; } export function adjustForRotation(polygon, orientation) { if (orientation === 90) { return polygon.map(([x, y]) => [y, 1 - x]); } if (orientation === 180) { return polygon.map(([x, y]) => [1 - x, 1 - y]); } if (orientation === 270) { return polygon.map(([x, y]) => [1 - y, x]); } return polygon; }