Question

I have a convex polygon expressed by points. Points are expressed by array of x-coordinates and array of y-coordinates.

For example:

X = {6, 1, 5, 0, 3}
Y = {4, 0, 0, 4, 6}

How can I sort this points by clockwise? Number of points is not always the same, but polygon is still convex.


Is there a solution without using atan2 or other function from math.h?

Was it helpful?

Solution

I think you can get away by converting them to polar coordinates. C has atan2 so you could get away with:

atan2(y[0], x[0]);

After you obtain the respective angles you can use them to sort the points.

OTHER TIPS

I'd recommend you to sort them by polar angle, but it's better to have a point inside of convex polygon as an origin to do that. to get such a point, you can just use a middle point of any diagonal of your poligon, for example ( (x[0] + x[2])/2, (y[0]+y[2])/2 ).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top