Reorder coordinates based on distance from each other in R -


i have x & y coordinates (black points) make irregular rectangle. order these coordinates each point connects nearest neighbor in order. if add lines, points across each other connect. coordinates connected lines data

plot(xy) lines(xy,col="red") 

this seems bit of rube goldberg contraption , i'm not sure how robust is, but...

the basic idea separate points "upper" group , "lower" group finding line goes between (xmin, y(xmin)) , (xmax, y(xmax)). work if none of y values fall on "wrong" side of line. is, if points don't have big departure convexity.

once have upper , lower points, sort each group x separately, reverse order of upper group after plot lower group, next line connect highest x-value of upper group (rather lowest x-value of upper group).

load("xy.rdata")  # convert matrix data frame xy = as.data.frame(xy) names(xy) = c("x","y")  # range of xy$x; xy$y values @ min , max xy$x values x.rng = c(min(xy$x), max(xy$x)) yofx = xy$y[which(xy$x %in% x)]  ## separate points "upper" , "lower" halves using (x,y) values calculated  # slope of dividing line slope=diff(yofx)/diff(x.rng)  # add "dividing line" data frame xy = cbind(xy, y.mid = slope*(xy$x - xy$x[1]) + yofx[1])  # group points "upper" , "lower" using dividing line created xy$group = ifelse(xy$y > xy$y.mid, "upper", "lower")  ## order points xy$group , xy$x xy = xy[order(xy$group, xy$x), ]   ## reverse order of "upper" points # reset rownames rownames(xy) = 1:nrow(xy)  # range of rownames "upper" group  rn = as.numeric(range(rownames(xy[xy$group=="upper",])))  # reverse order of "upper" points xy = xy[c(1:(rn[1]-1), rn[2]:rn[1]), ]  ## add copy of first point end of data frame close polygon xy = rbind(xy, xy[1,])  plot(xy$x, xy$y, type="l", lwd=0.8, las=1) # add points if wish #points(xy[,1:2],col=ifelse(xy$group=="upper","red","blue"),pch=16,cex=0.5) 

enter image description here


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -