This looks like a repeated question but I tried the solution that already exists and none seems to work so far for me. .
this solution gives a hint but it works only for a regular geometry. I have a rather complicated geometry from which I extract boundary points which are unsorted.
Below is a picture of the geometry and of the boundary vertices that I extract from the geometry.
The (x,y) coordinates of points as in the image are :
import numpy as np
pts = np.array([[ 30. , -6.25 ],
[ 30. , -8.10127917],
[ 0. , -6.25 ],
[ 34.14082772, -6.75584268],
[ 36.49784598, -10. ],
[ 44.43561524, -10. ],
[ 100. , -10. ],
[ 100. , 10. ],
[ 84.1244615 , -10. ],
[ 84.1244615 , 10. ],
[ 36.49784598, 10. ],
[ 34.14082772, 6.75584268],
[ 44.43561524, 10. ],
[ 30. , 8.10127917],
[ 30. , 6.25 ],
[ 0. , 6.25 ],
[ -30. , 6.25 ],
[ -30. , 8.10127917],
[ -32.92183092, 9.05063958],
[ -35.84366185, 10. ],
[ -51.88274638, 10. ],
[-100. , 10. ],
[-100. , -10. ],
[ -83.96091546, 10. ],
[ -83.96091546, -10. ],
[ -35.84366185, -10. ],
[ -51.88274638, -10. ],
[ -32.92183092, -9.05063958],
[ -30. , -8.10127917],
[ -30. , -6.25 ],
[ -67.92183092, 10. ],
[ -67.92183092, -10. ],
[ 68.24892299, 10. ],
[ 52.37338449, 10. ],
[ 68.24892299, -10. ],
[ 52.37338449, -10. ]])
In the boundary vertex data, we can see that the points are unordered. Is there a way the points could be ordered clockwise/counter clockwise so that these points form a closed ring when connected successively?
My goal is to create a polygon or a linear ring as described here and later find if an arbitrary euclidean point lies inside the polyogn/ring
Update: the approach of computing angle between centroid of the pts
and the individual euclidean point in pts
also doesn't work. Here is a sample code of what I tried :
def sort_circular(pts):
cent = coords.mean(axis=0)
idx = list(np.arange(0, len(pts)+1, dtype=int))
angle = []
for i, cc in enumerate(coords):
dx,dy = cc[0] - center[0], cc[1]-center[1]
angle.append(math.degrees(math.atan2(float(dy), float(dx))))
#simultaneously sort angle and indices
_, idx_sorted = (list(t) for t in zip(*sorted(zip(angle, idx))))
pts_sorted = pts[idx_sorted]
return pts_sorted
The result from this is still not the way i expect it to be (image below):