I have a long list of xy coordinates like the following:
>>> data = [(x1,y1),(x2,y2),(x3,y3),...]
Every pair of coordinates represents a point of a contour in an image and I would like to sort them like they are arranged along the contour (shortest path). The shape of the contour is very complex (it's the shape of a country), that's why a ConvexHull won't work.
I tried out this code, but it is not precise enough:
>>> import math
>>> import matplotlib.patches as patches
>>> import pylab
>>> pp=[(1,1),(2,3),(3,4)]
# compute centroid
>>> cent=(sum([p[0] for p in pp])/len(pp),sum([p[1] for p in pp])/len(pp))
# sort by polar angle
>>> pp.sort(key=lambda p: math.atan2(p[1]-cent[1],p[0]-cent[0]))
# plot points
>>> pylab.scatter([p[0] for p in pp],[p[1] for p in pp])
# plot polyline
>>> pylab.gca().add_patch(patches.Polygon(pp,closed=False,fill=False))
>>> pylab.grid()
>>> pylab.show()
I already tried like suggested in this case but it did not work out well, because my list of coordinates is too long.
As the points are very close to each other the solution to this question might seem too complicated for my case.