curve2.closestPoint
The point of a segment, arc or circle closest to a point, with its curve parameter and distance.
Usage
Curve2.closestPoint(curve: Curve2, p: Point2): Result<CurvePoint>Parameters
curveCurve2A segment, an arc or a circle. Required. In model.
pPoint2The query point, for example the cursor in model units. Required. In model.
Returns
Ok with { point, t, distance }: the closest point, its parameter on the curve (in [0, 1]) and its distance from p. Outside an arc’s sweep it is the nearer end, exactly.
Errors
Guarantees
- distance equals Point2.distance(p, point), and no point of the curve is nearer
- ties go to the smaller t: a query at a circle’s centre gets t = 0
- The same result on every machine.
Use instead
curve2.nearest: to snap to the nearest of many curves
Example
const r = Curve2.closestPoint({ kind: 'circle', centre: Point2.of(0, 0), radius: 2 }, Point2.of(0, 5))
r.ok ? r.value.t : Result.error(r).code // → 0.25 (point (0, 2), distance 3), once loadGeometryKernel() has run