Nanik
Nanik

Reputation: 633

C - Convex Polygon - Sort Point (Clockwise)

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?

Upvotes: 1

Views: 1900

Answers (2)

cnicutar
cnicutar

Reputation: 182774

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.

Upvotes: 3

Siarhei Tyshkavets
Siarhei Tyshkavets

Reputation: 392

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 ).

Upvotes: 3

Related Questions