tryNearestPoint - multiple declarations
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
)
);
Function tryNearestPoint
Finds the nearest point on an unbounded Line2.
The public result type follows the established metric family:
R == MetricScalar!T
For int, long, float, and double, projection is constructed exactly
through the rational-coordinate stage. Only the final binary64 coordinate
conversion is rounded, using round-to-nearest, ties-to-even.
real remains part of the metric scalar family. Because the exact dyadic
backend intentionally covers only binary64-backed topology scalars, the
real overload path uses exponent-aware floating-point construction.
Returns false for:
- a non-finite line;
- a non-finite query point;
- a degenerate line; or
- a projected coordinate that cannot be represented finitely in
MetricScalar!T.
Every failure path leaves result == Point2!R.
Unlike segment projection, an unbounded line never clamps to stored source points. The retained PP/PV construction form is not exposed and is not canonicalized through the public scalar type.
This is numerical construction, not a topological predicate.
No allocation is performed.
Complexity
O(1) time and O(1) auxiliary space.
Example
Example projecting onto an unbounded line through the public package API.
import geo;
alias P = Point2!int;
alias L = Line2!int;
const line =
L(
P(0, 0),
P(10, 0)
);
Point2!double nearest;
assert(
tryNearestPoint(
line: line,
point: P(3, 4),
result: nearest
)
);
assert(
nearest ==
Point2!double(
3.0,
0.0
)
);