Description Usage Arguments Value Examples
This function creates a two-dimensional movement path as a series of steps.
Movement can be correlated (current step length and direction depend on
previous step) or uncorrelated. The initial position and orientation of the
moving object are given by the arguments x0, y0, dir0
, while the
length and direction of each step, together with the conditions for accepting
a proposed step and detecting when movement should end are given by functions
supplied as arguments. The algorithm proceeds as follows:
The stop.cond
function is called to check if movement is
finished. Note: the first check takes place before any steps have been taken
so a function can reject an unacceptable start position or orientation.
An angle is drawn from the function next.angle
and the
object's orientation is updated. If turning.angles
is TRUE
the
angle is treated as a turning angle which is added to the previous orientation.
If turning.angles
is FALSE
, the object's new orientation is simply
set to the angle. In both cases, the new orientation is normalized to (-pi, pi].
A step length is chosen using function next.step
.
A new candidate position is calculated for the given orientation and step length.
The candidate position is tested with the accept.pos
function;
if accepted, the object moves to this position; otherwise a new candidate
position is chosen. If no position is accepted after accept.limit
attempts, the movement terminates.
This process continues until the stop.cond
function signals completion
or no more candidate positions can be found.
1 2 3 |
x0,y0 |
Ordinates of the starting location. |
dir0 |
Initial orientation (radians). If NULL or missing, a random angle in the interval (-pi, pi] will be used. |
next.angle |
A function to generate angles. The function should take a single argument (a walk.data object). |
next.steplen |
Either a function to generate step lengths, or a numeric value for constant step length. If a function, it should take a single argument (a walk.data object). |
accept.pos |
A function to test if a candidate position is acceptable.
The function should take two arguments: |
stop.cond |
A function to test if movement is finished. The function should take a single argument (a walk.data object). |
turning.angles |
If |
accept.limit |
Maximum number of candidate positions to test at each move. If exceeded, the movement is finished. |
id |
An identifer (integer or character) to store in the |
A named list (class walk.data
) with elements:
identifier (integer or character)
vector of X ordinates
vector of Y ordinates
vector of orientations (radians)
first X ordinate (same as x[1]
)
last X ordinate (same as x[ length(x) ]
)
first Y ordinate (same as y[1]
)
last Y ordinate (same as y[ length(y) ]
)
vector of step lengths (first element will be 0)
direct line distance from the start to the end of the path
distance along the path (sum of step lengths)
number of steps taken
one of: 'active'
while movement is continuing (ie. when
walk data is passed to angle, step length and stopping functions);
'finished'
if movement terminated normally;
'failed'
if movement terminated because no further acceptable positions
could be found.
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 | ## Not run:
# Start position and orientation
x0 <- 0
y0 <- 0
d0 <- runif(1, -pi, pi)
# Function to draw turning angles from a truncated Laplace distribution
# (walk.data argument is ignored)
angle <- function(wdat) rtrunclaplace(1, 0, 1, -pi, pi)
# Function for step lengths (walk.data argument ignored)
steplen <- function(wdat) 1.0
# Function to test acceptance of a proposed new position.
# As an example, we will reject any positions below a given Y.
posfn <- function(x, y) y > 10
# Function to test if movement is finished.
# As an example, we stop when either the direct distance from
# the start point or the total path distance exceed given values.
stopfn <- function(wdat) wdat$dist.direct > 20 || wdat$dist.path > 50
w <- make_walk(
x0, y0, d0,
next.angle = anglefn,
next.step = lenfn,
accept.pos = posfn,
stop.cond = stopfn)
# simple plot of the movement path with base graphics
with(w, plot(x, y, type="l"))
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.