Function tryNearestPoint
Finds the nearest point on a segment to a point.
The result uses MetricScalar!T because the nearest point of an integral segment is not generally representable with integral coordinates.
Returns false when:
- an input coordinate is NaN or infinite; or - a required metric difference cannot be represented finitely in the metric computation type.
On failure, result remains Point2!(MetricScalar!T).init. A successful result contains only finite coordinates.
A degenerate segment returns its single endpoint converted to MetricScalar!T. Endpoint projections are represented in the same way. Consequently, long coordinates may be rounded when represented as double.
Interior nearest points are constructed using floating-point metric arithmetic. The constructed coordinates are not an exact topological representation and must not be treated as an exact predicate result.
No allocation is performed.
Complexity
O(1) time and O(1) auxiliary space.
Example
Example constructing the nearest point in the metric scalar type.
import geo;
const segment =
Segment2!int(
Point2!int(0, 0),
Point2!int(10, 0)
);
Point2!double nearest;
assert(
tryNearestPoint(
segment,
Point2!int(3, 4),
nearest
)
);
static assert(
is(typeof(nearest) == Point2!double)
);
assert(
nearest ==
Point2!double(
3.0,
0.0
)
);