The minimum distance between two arbitrary geometries is non-trivial and is only exactly defined for non-circular geometries. distance_squared() will return the exact squared distance between geometries with x and y being recycled to the maximum length of either. distance_matrix will return a matrix of distances given as numerics (and thus not exact), with the geometries of x in the rows and the geometries of y in the columns so that the value of mat[i, j] corresponds to the distance between x[i] and y[j].

distance_squared(x, y)

approx_distance_matrix(x, y)

Arguments

x, y

eometry vectors or bounding boxes

Value

A euclid_exact_numeric vector for distance_squared() and a numeric matrix for distance_matrix()

See also

Other Measures: approx_angle(), geometry_measures

Examples

# Calculate distances between lines and rays in 3D
p <- point(sample(100, 20), sample(100, 20), sample(100, 20))
l <- line(p[1:5], p[6:10])
r <- ray(p[11:15], p[16:20])

# Pairwise exact distance
distance_squared(l, r)
#> <exact numerics [5]>
#> [1]  817.36926  305.86743   49.73382 3450.47128  129.60459

# All distances
approx_distance_matrix(l, r)
#>          [,1]      [,2]      [,3]     [,4]      [,5]
#> [1,] 28.58967 45.221360 29.636183  8.59046 43.796695
#> [2,] 34.15255 17.489066 48.260429 33.65818  4.881363
#> [3,] 19.34672 14.758211  7.052221 39.37752 16.671874
#> [4,] 18.29615 20.579345 16.860223 58.74071  2.819346
#> [5,] 28.27396  7.959155 26.388822 23.81777 11.384401