How to sort points to construct a polygon in python?

Viewed 39

I have a data of x and y that I want to construct a polygon representing this data. To do that I should sort the points in a way that lines representing the polygon are drawn correctly. How can I do that in python? I think there should be a double constrain or something, one for the angle and the other is for the distance maybe. Any ideas?!

The data is in here: Data

what I want is something like this: enter image description here

I have tried the following code but the result is incorrect:

mean = np.mean(data, axis=0)
# Compute angles
angles = np.arctan2((data-mean)[:, 1], (data-mean)[:, 0])
# Transform angles from [-pi,pi] -> [0, 2*pi]
angles[angles < 0] = angles[angles < 0] + 2 * np.pi
# Sort
sorting_indices = np.argsort(angles)
sorted_data = data[sorting_indices]

Here is what it result: enter image description here

1 Answers

In a list of 58 points there is more then one possible polygon, even if you rule out self-intersecting ones.

The problem you are encountering is that your list of points basically does not have enough information to allow you to select only one of the possibilities. (Unless for example the sequence of the points in the list is relevant, which doesn't seem to be the case here.)

Related