Function distance

Euclidean distance between two points.

MetricScalar!T distance(T)(
  Point2!T a,
  Point2!T b
) pure nothrow @nogc @safe
if (isGeoScalar!T);

Integer coordinate differences are obtained without signed overflow before conversion to MetricScalar!T. This avoids losing small differences merely because large integer coordinates were converted before subtraction.

The resulting metric value is floating-point. In particular, long-coordinate results may lose precision after conversion to double.

Distance is calculated directly with hypot rather than as

sqrt(squaredDistance(a, b))

so avoidable intermediate square overflow is not introduced.

Floating-point NaN and infinity are not rejected. Results follow normal floating-point arithmetic.

This function is a metric computation, not a robust exact distance comparison predicate.

No allocation is performed.

Complexity

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

Example

Example using the public package API.

import geo;

alias P = Point2!double;

assert(
    distance(
        P(0.0, 0.0),
        P(3.0, 4.0)
    ) == 5.0
);