Let v_{0},...,v_{N-1}
be N points in a Cartesian xy plane defining the vertices of closed polygon (i.e. v_{N} = v_{0}).
Let P_{0}
a point lying inside the polygon, point that is not known a priori.
I am looking for a robust algorithm to sort the list v_{0},...,v_{N-1}
clockwise/counterclockwise in respect of P_{0}
.
I know that, if the polygon is convex, the following algorithm works well:
- compute
P_{0}
as the mean ofv_{0},...,v_{N-1}
; - compute the angle defined by
atan2 = (y_i-y_0/x_i-x_0)
for every vertex and save it into an array; - sort the vertices using the array of the angle as the key.
I am wondering if exist an algorithm able to deal with all kind of polygon, also non-convex.
v1,v2,v3,...,vn
, and for each vertex the coordinates. Does your list also mean that the polygon has sidesv1-v2, v2-v3,...,vn-v1
? Doessort counterclockwise the vertices
mean the same as walking the perimeter of the polygon in a counterclockwise direction ? $\endgroup$v_1,..., v_n
but I don not known the sides because the vertices are not sorted properly; so that, if I computev_1-v_2, ..., v_n-v_{n-1}, v_n-v_1
those value are unreasonable results. I want to sort the vertices clock/counterclockwise in order to make the perimeter "walkable" in a direction. @ High Performance Mark $\endgroup$