Given two point patterns `X`

and `Y`

this
function finds, for each point of `X`

,
the nearest point of `Y`

. The distance between these points
is also computed.
If the argument `k`

is specified, then the `k`

-th nearest
neighbours will be found.

Alternatively if `X`

is a point pattern and `Y`

is a line
segment pattern, the function finds the nearest line segment to each point
of `X`

, and computes the distance.

The return value is a data frame, with rows corresponding to
the points of `X`

. The first column gives the nearest neighbour
distances (i.e. the `i`

th entry is the distance
from the `i`

th point of `X`

to the nearest element of
`Y`

). The second column gives the indices of the nearest
neighbours (i.e.\ the `i`

th entry is the index of
the nearest element in `Y`

.)
If `what="dist"`

then only the vector of distances is returned.
If `what="which"`

then only the vector of indices is returned.

The argument `k`

may be an integer or an integer vector.
If it is a single integer, then the `k`

-th nearest neighbours
are computed. If it is a vector, then the `k[i]`

-th nearest
neighbours are computed for each entry `k[i]`

. For example, setting
`k=1:3`

will compute the nearest, second-nearest and
third-nearest neighbours. The result is a data frame.

Note that this function is not symmetric in `X`

and `Y`

.
To find the nearest neighbour in `X`

of each point in `Y`

,
where `Y`

is a point pattern, use `nncross(Y,X)`

.

The arguments `iX`

and `iY`

are used when
the two point patterns `X`

and `Y`

have some points in
common. In this situation `nncross(X, Y)`

would return some zero
distances. To avoid this, attach a unique integer identifier to
each point, such that two points are identical if their
identifying numbers are equal. Let `iX`

be the vector of
identifier values for the points in `X`

, and `iY`

the vector of identifiers for points in `Y`

. Then the code
will only compare two points if they have different values of the
identifier. See the Examples.