Determines the heading between spatial points.

heading(from, to)

Arguments

from

The starting position; an object of class SpatVector.

to

The ending position; an object of class SpatVector.

Value

The heading between the points, in degrees.

Author

Eliot McIntire

Examples

library(terra)

origDTThreads <- data.table::setDTthreads(2L)
origNcpus <- options(Ncpus = 2L)

N <- 10L                       # number of agents
x1 <- stats::runif(N, -50, 50) # previous X location
y1 <- stats::runif(N, -50, 50) # previous Y location
x0 <- stats::rnorm(N, x1, 5)   # current X location
y0 <- stats::rnorm(N, y1, 5)   # current Y location

# using SpatVector
prev <- terra::vect(cbind(x = x1, y = y1))
curr <- terra::vect(cbind(x = x0, y = y0))
heading(prev, curr)
#>  [1] 271.06528 287.07493  94.33365 235.49469 352.35050 231.02068 125.03050
#>  [8] 337.04994  89.22764  95.68061

# using matrix
prev <- matrix(c(x1, y1), ncol = 2, dimnames = list(NULL, c("x","y")))
curr <- matrix(c(x0, y0), ncol = 2, dimnames = list(NULL, c("x","y")))
heading(prev, curr)
#>  [1] 271.06528 287.07493  94.33365 235.49469 352.35050 231.02068 125.03050
#>  [8] 337.04994  89.22764  95.68061

#using both
prev <- terra::vect(cbind(x = x1, y = y1))
curr <- matrix(c(x0, y0), ncol = 2, dimnames = list(NULL, c("x","y")))
heading(prev, curr)
#>  [1] 271.06528 287.07493  94.33365 235.49469 352.35050 231.02068 125.03050
#>  [8] 337.04994  89.22764  95.68061

prev <- matrix(c(x1, y1), ncol = 2, dimnames = list(NULL, c("x","y")))
curr <- terra::vect(cbind(x = x0, y = y0))
heading(prev, curr)
#>  [1] 271.06528 287.07493  94.33365 235.49469 352.35050 231.02068 125.03050
#>  [8] 337.04994  89.22764  95.68061

# clean up
data.table::setDTthreads(origDTThreads)
options(Ncpus = origNcpus)