.BG
.FN xysort
.TL
xysort: Rearrange x-y Data for Fast Plotting
.CS
xysort(x, y, order)
.PP
.AG x,y
Coordinates of input points.
NAs are allowed.
.AG order
logical flag (default FALSE), should ordering vector be returned.
.RT
structure with components named
`x' and `y', giving a rearranged version of the input data points.
If `order' is TRUE, component `order' is also returned.
.RC x,y
rearranged values of original `x' and `y' data.
Points with NAs 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(len(x)^2)'.
See the reference.
.SH REFERENCES
Jon Louis Bentley,
"A Case Study in Applied Algorithm Design",
.ul
IEEE Computer,
Feb, 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 plot*
.KW sorting
.WR
