euclidean: Euclidean distance helpers

Description Usage Arguments Functions See Also Examples

Description

A little tribe to help with euclidean distances calculations

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
ed(x, y)

edi(x, y, r = 0.5)

ed_pw(x, y)

ed_nearest(x, y)

ed_furthest(x, y)

ed_calliper(x)

ed_minrad(x)

Arguments

x, y

coo_single, matrices or vectors of length 2 (when sensible)

r

numeric how much of the distance d(x -> y) should we travel?

Functions

See Also

Other geometry: geometry

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
x <- c(0, 0)
y <- c(1, 1)

ed(x, y) # sqrt(2)
edi(x, y, 0.25) # c(0.25, 0.25)

bot %>% dplyr::slice(1:2) %>% coo_sample(12)  %>% dplyr::pull(coo) -> b
ed_pw(b[[1]], b[[2]])
# checking
purrr::map_dbl(1:12, ~ed(b[[1]][.x, ], b[[2]][.x, ]))

bot %>% pick(1) %>% ed_nearest(c(0, 0))

# nearest and furthest points

set.seed(2329) # for the sake of reproducibility when building the pkg
foo <- tibble::tibble(x=runif(5), y=runif(5))
z <- c(0.5, 0.5)
plot(foo, pch="")
text(foo, labels=1:5)
points(z[1], z[2], col="red", pch=20)

(nearest <- ed_nearest(foo, z))
segments(z[1], z[2],
         as.numeric(foo[nearest$id, 1]), as.numeric(foo[nearest$id, 2]), col="blue")

(furthest <- ed_furthest(foo, z))
segments(z[1], z[2],
         as.numeric(foo[furthest$id, 1]), as.numeric(foo[furthest$id, 2]), col="red")


(calli <- ed_calliper(foo))
segments(as.numeric(foo[calli$ids[1], 1]), as.numeric(foo[calli$ids[1], 2]),
         as.numeric(foo[calli$ids[2], 1]), as.numeric(foo[calli$ids[2], 2]), col="green")

MomX/Momocs2 documentation built on May 13, 2020, 4:28 a.m.