Rearrange x-y Data for Fast Plotting

DESCRIPTION:

Returns a list with the data re-ordered in such a way that pen plotters will plot faster.

USAGE:

xysort(x, y, order=F) 

REQUIRED ARGUMENTS:

x,y
coordinates of points. The coordinates can be given by two vector arguments or by a single arguments x which is a times series, a two-column matrix, or a list containing components named x and y. Missing values (NA) are allowed.

OPTIONAL ARGUMENTS:

order
if TRUE, ordering vector is returned.

VALUE:

list with components named x and y, giving a rearranged version of the input data points, plus an optional order component.
x,y
rearranged values of original x and y data. Points with NAs in either the original x or y coordinates are omitted.
order
a permutation vector is returned. This permutation can be used to rearrange the original x , y, or any other associated data.

DETAILS:

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).

REFERENCES:

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.

SEE ALSO:

, .

EXAMPLES:

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