sort curvepoints by using the subsequent neighbours
sort curvepoints by using the subsequent neighbours
sortCurve(x, k = 5, start = NULL)
x |
k x m matrix containing the 2D or 3D coordinates |
k |
number of nearest neighbours to look at. Set high for very irregularly clustered curves. |
start |
integer: which row of x to use as a starting point. If NULL, it is assumed that the curve is open and the point where the angle between the two nearest neighbours is closest will be chosen. |
xsorted |
matrix with coordinates sorted along a curve |
index |
vector containing the sorting indices |
## generate a curve from a polynome x <- c(32,64,96,118,126,144,152.5,158) y <- c(99.5,104.8,108.5,100,86,64,35.3,15) fit <- lm(y~poly(x,2,raw=TRUE)) xx <- seq(30,160, length=50) layout(matrix(1:3,3,1)) curve <- cbind(xx,predict(fit, data.frame(x=xx))) ## permute order set.seed(42) plot(curve);lines(curve) curveunsort <- curve[sample(1:50),] ## now the curve is scrambled plot(curveunsort);lines(curveunsort,col=2) curvesort <- sortCurve(curveunsort) ## after sorting lines are nice again plot(curvesort$xsorted);lines(curvesort$xsorted,col=3)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.