Learn R Programming

trajr (version 1.5.1)

TrajSplitAtFirstCrossing: Split a trajectory into two parts, separated at the first boundary crossing

Description

This is basically a wrapper around TrajInPolygon and TrajSplit.

Usage

TrajSplitAtFirstCrossing(trj, boundary)

Value

A list with 1 or 2 elements. If trj lies entirely inside or outside boundary, then the list simply contains trj. If

trj crosses the boundary, then the list contains 2 trajectories. The first is the longest part of trj that lies entirely inside or outside boundary, and the second is the remainder of trj.

Arguments

trj

The trajectory to split.

boundary

A polygon defining the boundary. Can be any structure that xy.coords can handle, such as a data frame with x and y columns.

See Also

TrajInPolygon, TrajSplit

Examples

Run this code
# Square arena
boundary <- data.frame(x = c(-10, 10, 10, -10), y = c(-10, -10, 10, 10))

# Generate a random trajectory
set.seed(1)
trj <- TrajGenerate(n = 8, stepLength = 3, angularErrorSd = .4)
# Split the trajectory where it crosses the boundary
l <- TrajSplitAtFirstCrossing(trj, boundary)

# Plot the boundary and the two trajectories
plot(NULL, xlim = range(c(boundary$x, trj$x)), ylim = range(c(boundary$y, trj$y)), asp = 1)
polygon(boundary, border = "brown", lwd = 2)
lines(l[[1]], col = "#2040ff80", lwd = 3)
lines(l[[2]], col = "#ff204080", lwd = 3)

Run the code above in your browser using DataLab