Skip to contents

Given a data frames containing tracking information for a given tracklet, this function compute the euclidean distance along the trajectory according to a given step.

Usage

distTraveled(df, step = 1)

Arguments

df

A data frame containing at x, y coordinates named "x.pos", "y.pos", for a tracklet.

step

A numeric value specifying the number of time unit or records between the two positions from which distance should be computed.

Value

This function returns a vector containing the distance between each points of the trajectory according to the specified step.

Author

Quentin PETITJEAN

Examples


set.seed(2023)
# generate a dummy tracklet
## start to specify some parameters to generate the tracklet
TrackL <-
  100 # the length of the tracklet or a sequence to randomly sample tracklet's length
TrackDatTemp <-
  trajr::TrajGenerate(sample(TrackL, 1), random = TRUE, fps = 1)
TrackDat <- list(
  data.frame(
    x.pos = TrackDatTemp[["x"]] - min(TrackDatTemp[["x"]]),
    y.pos = TrackDatTemp[["y"]] - min(TrackDatTemp[["y"]]),
    frame = TrackDatTemp[["time"]]
  )
)
# compute the distance traveled between each point of the trajectory
TrackDat[[1]][["distTraveled"]] <-
  MoveR::distTraveled(TrackDat[[1]], step = 1)

# check the result
str(TrackDat)
#> List of 1
#>  $ :'data.frame':	81 obs. of  4 variables:
#>   ..$ x.pos       : num [1:81] 0 2.07 2.35 2.45 1.91 ...
#>   ..$ y.pos       : num [1:81] 47.3 46.2 44.3 42.2 40.2 ...
#>   ..$ frame       : num [1:81] 0 1 2 3 4 5 6 7 8 9 ...
#>   ..$ distTraveled: num [1:81] 2.35 1.98 2.1 2.03 2.06 ...

# sum all the distance to compute the total distance traveled by the particle over the trajectory
sum(TrackDat[[1]][["distTraveled"]], na.rm = T)
#> [1] 163.0277