Learn R Programming

Morpho (version 2.12)

sortCurve: sort curvepoints by using the subsequent neighbours

Description

sort curvepoints by using the subsequent neighbours

Usage

sortCurve(x, k = 5, start = NULL)

Value

xsorted

matrix with coordinates sorted along a curve

index

vector containing the sorting indices

Arguments

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.

Examples

Run this code

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

Run the code above in your browser using DataLab