.BG
.FN xysort
.TL
Rearrange x-y Data for Fast Plotting
.CS
xysort(x, y, order=FALSE)
.XY
.NA
.AG order
if `TRUE', ordering vector is returned.
.RT
list with components named
`x' and `y', giving a rearranged version of the input data points, plus an
optional `order' component.
.RC x,y
rearranged values of original `x' and `y' data.
Points with `NA's in either the original `x' or `y' coordinates are omitted.
.RC order
a permutation vector is returned.
This permutation can be used to rearrange the original
`x', `y', or any other associated data.
.PP
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.
.PP
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)'.
.SH REFERENCES
Jon Louis Bentley,
"A Case Study in Applied Algorithm Design",
.ul
IEEE Computer,
February, 1984, pp. 75-88.
.sp
Jon Louis Bentley,
.ul
Writing Efficient Programs,
Prentice-Hall, Englewood Cliffs, NJ, 1982.
.EX
plot( xysort(x,y) )

xyo <- xysort(x, y, order=T)
plot(xyo, type="n")
text(xyo, label[xyo$order])
.KW dplot
.WR
