All files is-on-polygon.ts

100% Statements 14/14
100% Branches 3/3
100% Functions 1/1
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31  1x 1x 1x                           1x 10x 10x   10x 20x 5x 5x 5x 20x   10x 10x  
import { type Cartesian, type Polygon } from './@types/geometry.ts';
import { isOnLine } from './is-on-line.ts';
import { toClosed } from './to-closed.ts';
import { toLineSegment } from './to-line-segment.ts';
 
// Determines whether a point is located on one of the edges of a polygon.
// Returns a boolean.
/**
 * Determines whether a given point lies exactly on the boundary of a polygon.
 * @param point - The Cartesian coordinates of the point to test.
 * @param polygon - The polygon, represented as an array of Cartesian points.
 * @param tolerance - Optional tolerance for floating-point comparisons (default is 1e-10).
 * @returns `true` if the point lies on any edge of the polygon within the given tolerance, otherwise `false`.
 * @group Geometry
 * @category Point
 * @category Polygon
 */
export function isOnPolygon(point: Cartesian, polygon: Polygon, tolerance = 1e-10): boolean {
  let on = false;
  const closed = toClosed(polygon);
 
  for (let i = 0, l = closed.length - 1; i < l; i++) {
    if (isOnLine(point, toLineSegment(closed[i], closed[i + 1]), { tolerance })) {
      on = true;
      break;
    }
  }
 
  return on;
}