xysort(x, y, order=F)
TRUE
, ordering vector is returned.
x
and
y
, giving a rearranged version of the input data points, plus an
optional
order
component.
x
and
y
data.
Points with
NA
s in either the original
x
or
y
coordinates are omitted.
x
,
y
, or any other associated data.
Adjacent values in the returned
x
and
y
data
are likely to be near one another in x-y space.
The idea is that a mechanical plotter will be able to visit all of the points
much faster when they are presented in the sorted order.
A greedy algorithm is used, finding the nearest neighbor to the first
point, then the nearest neighbor to that point, etc.
The algorithm has complexity
O(length(x)^2)
.
Jon Louis Bentley (1984).
A Case Study in Applied Algorithm Design.
IEEE Computer,
75-88.
Jon Louis Bentley (1982).
Writing Efficient Programs.
Prentice-Hall, Englewood Cliffs, NJ.
plot( xysort(x,y) ) xyo <- xysort(x, y, order=T) plot(xyo, type="n") text(xyo, label[xyo$order])