R/dists.R
dist_btw_points_3d.Rd
This function calculates the Euclidean distance(s) between points in three-dimensional space.
dist_btw_points_3d(x1, x2, y1, y2, z1, z2)
A numeric vector that defines the x-coordinate(s) of the origin (for each point pair).
A numeric vector that defines the x-coordinate(s) of the destination (for each point pair).
A numeric vector that defines the y-coordinate(s) of the origin (for each point pair).
A numeric vector that defines the y-coordinate(s) of the destination (for each point pair).
A numeric vector that defines the z-coordinate(s) of the origin (for each point pair).
A numeric vector that defines the z-coordinate(s) of the destination (for each point pair).
The function returns a numeric vector that is equal to the Euclidean distance(s) between each pair of points in three-dimensional space.
The distance between any two points in three dimensional space is given by Pythagoras' Theorem: \(\sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2}\).
This function does not currently support non-planar coordinates.
#### Example (1): A single pair of points
dist_btw_points_3d(1, 2, 1, 2, 1, 2)
#> [1] 1.732051
sqrt((2 - 1)^2 + (2 - 1)^2 + (2 - 1)^2)
#> [1] 1.732051
#### Example (2): Multiple pairs of points (e.g., an animal movement path)
xy <- data.frame(
x = c(1, 2, 3, 4),
y = c(1, 2, 3, 4),
z = c(1, 2, 3, 4)
)
dist_btw_points_3d(
xy$x, dplyr::lead(xy$x),
xy$y, dplyr::lead(xy$y),
xy$z, dplyr::lead(xy$z)
)
#> [1] 1.732051 1.732051 1.732051 NA