Distance Between Points

A distance function or metric, \(d(x, y)\), must satisfy four criteria.

  1. The result is always positive. \(d(x, y) \geq 0\)

  2. The order doesn't matter. \(d(x, y) = d(y, x)\)

  3. If the distance is zero, then \(x\) must be equal to \(y\). If \(d(x, y) = 0\), then \(x = y\).

  4. The triangle inequality holds. \(d(a, b) + d(b, c) \geq d(a, c)\)

struct Point{T}
    x::T
    y::T
    z::T
end

function cartesiandistance(p1::Point, p2::Point)
    sqrt((p1.x - p2.x)^2 + (p1.y - p2.y)^2 + (p1.z - p2.z)^2)
end

p1 = Point(5, -2, 3)
p2 = Point(-1, 0, 2)

@show cartesiandistance(p1, p2)

> cartesiandistance(p1, p2) = 6.4031242374328485