@pbroom/color-kit/planeOperations

nearestPointOnPath

Returns the point on a polyline closest to point.

functionimport { nearestPointOnPath } from '@pbroom/color-kit/plane';operations.ts:147
function nearestPointOnPath(
  path: PlanePoint[],
  point: PlanePoint,
): PlanePoint | null;

The path is treated as an open polyline: only consecutive points are joined, so append the first point to include a closing segment. The projection is exact (not sampled) and the result lies on a segment, possibly at a vertex. When two segments are equally close, the earlier one wins.

Parameters

pathPlanePoint[]
Polyline points in the same coordinate space as point.
pointPlanePoint
Query point.

Returns

PlanePoint | nullThe nearest point on the path, or null when path has fewer than two points.

Example

TypeScript
import { nearestPointOnPath } from '@pbroom/color-kit/plane';

const path = [{ x: 0, y: 0 }, { x: 1, y: 0 }, { x: 1, y: 1 }];

nearestPointOnPath(path, { x: 0.4, y: 0.3 }); // → { x: 0.4, y: 0 }
nearestPointOnPath(path, { x: 0.9, y: 0.5 }); // → { x: 1, y: 0.5 }

See also

Also exported from