Distance between two 3D points

Hello.
How to create a function to calculate the distance between two points, but in 3D.

Here's the Euclidean Distance if you want to roll your own. I'm sure there are packages that have better implementations than this, as well as other distance metrics.

euclidean_distance <- function(x1, y1, z1, x2, y2, z2) {
    sqrt(
        sum(
            (x2 - x1)^2,
            (y2 - y1)^2,
            (z2 - z1)^2
        )
    )
}

pt1 <- list(
    x = 1,
    y = 2,
    z = 4
)
pt2 <- list(
    x = 7,
    y = 22,
    z = 0
)

euclidean_distance(
    x1 = pt1$x,
    x2 = pt2$x,
    y1 = pt1$y,
    y2 = pt2$y,
    z1 = pt1$z,
    z2 = pt2$z
)
#> [1] 21.26029

Created on 2022-04-27 by the reprex package (v1.0.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.