# nncross

##### Nearest Neighbours Between Two Patterns

Given two point patterns `X`

and `Y`

,
finds the nearest neighbour in `Y`

of each point of `X`

.
Alternatively `Y`

may be a line segment pattern.

##### Usage

```
nncross(X, Y, iX=NULL, iY=NULL,
what = c("dist", "which"),
...,
sortby=c("range", "var", "x", "y"),
is.sorted.X = FALSE,
is.sorted.Y = FALSE)
```

##### Arguments

- X
- Point pattern (object of class
`"ppp"`

). - Y
- Either a point pattern (object of class
`"ppp"`

) or a line segment pattern (object of class`"psp"`

). - iX, iY
- Optional identifiers, applicable only in the case where
`Y`

is a point pattern, used to determine whether a point in`X`

is identical to a point in`Y`

. See Details. - what
- Character string specifying what information should be returned.
Either the nearest neighbour distance (
`"dist"`

), the identifier of the nearest neighbour (`"which"`

), or both. - sortby
- Determines which coordinate to use to sort the point patterns. See Details.
- is.sorted.X, is.sorted.Y
- Logical values attesting whether the point patterns
`X`

and`Y`

have been sorted. See Details. - ...
- Ignored.

##### Details

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.

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.

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.

##### Value

- By default (if
`what=c("dist", "which")`

) a data frame with two columns: dist Nearest neighbour distance which Nearest neighbour index in `Y`

- If
`what="dist"`

, a vector of nearest neighbour distances.If

`what="which"`

, a vector of nearest neighbour indices.

##### Sorting data and pre-sorted data

For efficiency, the algorithm sorts the point patterns `X`

and `Y`

into increasing order of the $x$ coordinate
or increasing order of the the $y$ coordinate. By default
(if `sortby="range"`

),
the sorting will occur on the coordinate that has the larger range of
values (according to the frame of the enclosing window of `Y`

).
If `sortby = "var"`

), sorting will occur on the coordinate that
has the greater variance (in the pattern `Y`

).
Setting `sortby="x"`

or `sortby = "y"`

will specify that
sorting should occur on the $x$ or $y$ coordinate, respectively.

If the point pattern `X`

is already
sorted, then the corresponding argument `is.sorted.X`

should be set to `TRUE`

, and `sortby`

should be set
equal to `"x"`

or `"y"`

to indicate which coordinate
is sorted.

Similarly if `Y`

is already sorted, then `is.sorted.Y`

should be set to `TRUE`

, and `sortby`

should be set
equal to `"x"`

or `"y"`

to indicate which coordinate
is sorted.

If both `X`

and `Y`

are sorted *on the same coordinate
axis* then both `is.sorted.X`

and `is.sorted.Y`

should be set to `TRUE`

, and `sortby`

should be set
equal to `"x"`

or `"y"`

to indicate which coordinate
is sorted.

##### See Also

`nndist`

for nearest neighbour
distances in a single point pattern.

##### Examples

```
# two different point patterns
X <- runifpoint(15)
Y <- runifpoint(20)
N <- nncross(X,Y)$which
# note that length(N) = 15
plot(superimpose(X=X,Y=Y), main="nncross", cols=c("red","blue"))
arrows(X$x, X$y, Y[N]$x, Y[N]$y, length=0.15)
# two patterns with some points in common
Z <- runifpoint(50)
X <- Z[1:30]
Y <- Z[20:50]
iX <- 1:30
iY <- 20:50
N <- nncross(X,Y, iX, iY)$which
N <- nncross(X,Y, iX, iY, what="which") #faster
plot(superimpose(X=X, Y=Y), main="nncross", cols=c("red","blue"))
arrows(X$x, X$y, Y[N]$x, Y[N]$y, length=0.15)
# point pattern and line segment pattern
X <- runifpoint(15)
Y <- rpoisline(10)
N <- nncross(X,Y)
```

*Documentation reproduced from package spatstat, version 1.28-1, License: GPL (>= 2)*