tryPointSegmentDistance - multiple declarations

Function tryPointSegmentDistance

Computes the Euclidean distance from a point to a segment.

bool tryPointSegmentDistance(T, R)(
  Segment2!T segment,
  Point2!T point,
  out R result
) pure nothrow @nogc @safe
if (isGeoScalar!T && is(R == MetricScalar!T));

Returns false when:

- an input coordinate is NaN or infinite; or - a required metric difference cannot be represented finitely in MetricScalar!T.

On failure, result is zero. A successful call produces a finite result.

A degenerate segment is treated as its single endpoint.

Integral coordinate differences are obtained before conversion to the metric computation type, avoiding signed overflow and preserving small differences between large integer coordinates.

For an interior projection, the perpendicular distance is computed from scaled direction and offset vectors. No rounded nearest-point coordinate is constructed.

This is a metric computation, not an exact topological predicate.

No allocation is performed.

Complexity

O(1) time and O(1) auxiliary space.

Function tryPointSegmentDistance

Deprecated v1 point-first overload.

bool tryPointSegmentDistance(T, R)(
  Point2!T point,
  Segment2!T segment,
  out R result
) pure nothrow @nogc @safe
if (isGeoScalar!T && is(R == MetricScalar!T));

Use tryPointSegmentDistance(segment, point, result) so that the segment is the UFCS receiver consistently with tryNearestPoint.

Example

Example computing point-to-segment distance and handling failure.

import geo;

alias P = Point2!double;
alias S = Segment2!double;

const segment =
    S(
        P(0.0, 0.0),
        P(10.0, 0.0)
    );

double result;

assert(
    tryPointSegmentDistance(
        segment,
        P(5.0, 3.0),
        result
    )
);

assert(result == 3.0);

result = 123.0;

assert(
    !tryPointSegmentDistance(
        segment,
        P(double.nan, 0.0),
        result
    )
);

assert(result == 0.0);