curve2.nearest

The curve nearest to a point among many, with its index, closest point, parameter and distance.

GeometricSince 0.18.0

Usage

TypeScript
Curve2.nearest(curves: readonly Curve2[], p: Point2): Result<NearestCurvePoint>

Parameters

  • curvesreadonly Curve2[]

    The candidates, for example the curves near the cursor. Required. In model.

  • pPoint2

    The query point, for example the cursor in model units. Required. In model.

Returns

Ok with { index, point, t, distance }: the index into curves of the nearest one and its closest point, as Curve2.closestPoint gives it.

Errors

Guarantees

  • distance is the smallest Curve2.closestPoint distance over curves, and ties go to the lowest index
  • The same result on every machine.

Example

TypeScript
const curves = [{ kind: 'line', a: Point2.of(0, 2), b: Point2.of(4, 2) }, { kind: 'circle', centre: Point2.of(10, 0), radius: 1 }]
const r = Curve2.nearest(curves, Point2.of(8, 0))
r.ok ? r.value.index : Result.error(r).code // → 1 (point (9, 0), distance 1), once loadGeometryKernel() has run

See also