Skip to main content
image fixed (inlining HTTP images doesn't work anymore); for more info, see https://gist.github.com/Glorfindel83/9d954d34385d2ac2597bbe864466259f
Source Link

Your question is not precise enough. An array of points is only « clockwise » or « anti-clockwise » relative to a reference point. Otherwise, any array of three points can always be either CW or CCW. See the following picture: on the left, the points are ordered clockwise; on the right, the exact same points are ordered anticlockwise.

clockwise or anticlockwise http://zoy.org/%7Esam/cw-ccw.pngclockwise or anticlockwise

In your case, I believe using the barycenter of the points as a reference point is reasonable.

A good method for an unknown number of points could be the following one:

  • let P[0], P[1], ... P[n-1] be the list of points to sort
  • let M be the barycenter of all points
  • compute a[0], a[1], ... a[n-1] such that a[i] = atan2(P[i].y - M.y, P[i].x - M.x);
  • sort points relative to their a value, using qsort for instance.

However, you can be sure that a good sorting algorithm will perform poorly with three input values compared to an ad-hoc method. Using atan2 is still valid, but just don't use qsort.

Your question is not precise enough. An array of points is only « clockwise » or « anti-clockwise » relative to a reference point. Otherwise, any array of three points can always be either CW or CCW. See the following picture: on the left, the points are ordered clockwise; on the right, the exact same points are ordered anticlockwise.

clockwise or anticlockwise http://zoy.org/%7Esam/cw-ccw.png

In your case, I believe using the barycenter of the points as a reference point is reasonable.

A good method for an unknown number of points could be the following one:

  • let P[0], P[1], ... P[n-1] be the list of points to sort
  • let M be the barycenter of all points
  • compute a[0], a[1], ... a[n-1] such that a[i] = atan2(P[i].y - M.y, P[i].x - M.x);
  • sort points relative to their a value, using qsort for instance.

However, you can be sure that a good sorting algorithm will perform poorly with three input values compared to an ad-hoc method. Using atan2 is still valid, but just don't use qsort.

Your question is not precise enough. An array of points is only « clockwise » or « anti-clockwise » relative to a reference point. Otherwise, any array of three points can always be either CW or CCW. See the following picture: on the left, the points are ordered clockwise; on the right, the exact same points are ordered anticlockwise.

clockwise or anticlockwise

In your case, I believe using the barycenter of the points as a reference point is reasonable.

A good method for an unknown number of points could be the following one:

  • let P[0], P[1], ... P[n-1] be the list of points to sort
  • let M be the barycenter of all points
  • compute a[0], a[1], ... a[n-1] such that a[i] = atan2(P[i].y - M.y, P[i].x - M.x);
  • sort points relative to their a value, using qsort for instance.

However, you can be sure that a good sorting algorithm will perform poorly with three input values compared to an ad-hoc method. Using atan2 is still valid, but just don't use qsort.

fixed grammar
Source Link
sam hocevar
  • 23.9k
  • 2
  • 64
  • 95

Your question is not precise enough. An array of points is only « clockwise » or « anti-clockwise » relative to a reference point. Otherwise, any array of three points can always be either CW or CCW. See the following picture: on the left, the points are ordered clockwise; on the right, the exact same points are ordered anticlockwise.

clockwise or anticlockwise http://zoy.org/%7Esam/cw-ccw.png

In your case, I believe using the barycenter of the points as a reference point is reasonable.

A good method for an unknown number of points could be the following one:

  • let P[0], P[1], ... P[n-1] be the list of points to sort
  • let M be the barycenter of all points
  • compute a[0], a[1], ... a[n-1] such asthat a[i] = atan2(P[i].y - M.y, P[i].x - M.x);
  • sort points relative to their a value, using qsort for instance.

However, you can be sure that a good sorting algorithm will perform poorly with three input values compared to an ad-hoc method. Using atan2 is still valid, but just don't use qsort.

Your question is not precise enough. An array of points is only « clockwise » or « anti-clockwise » relative to a reference point. Otherwise, any array of three points can always be either CW or CCW. See the following picture: on the left, the points are ordered clockwise; on the right, the exact same points are ordered anticlockwise.

clockwise or anticlockwise http://zoy.org/%7Esam/cw-ccw.png

In your case, I believe using the barycenter of the points as a reference point is reasonable.

A good method for an unknown number of points could be the following one:

  • let P[0], P[1], ... P[n-1] be the list of points to sort
  • let M be the barycenter of all points
  • compute a[0], a[1], ... a[n-1] such as a[i] = atan2(P[i].y - M.y, P[i].x - M.x);
  • sort points relative to their a value, using qsort for instance.

However, you can be sure that a good sorting algorithm will perform poorly with three input values compared to an ad-hoc method. Using atan2 is still valid, but just don't use qsort.

Your question is not precise enough. An array of points is only « clockwise » or « anti-clockwise » relative to a reference point. Otherwise, any array of three points can always be either CW or CCW. See the following picture: on the left, the points are ordered clockwise; on the right, the exact same points are ordered anticlockwise.

clockwise or anticlockwise http://zoy.org/%7Esam/cw-ccw.png

In your case, I believe using the barycenter of the points as a reference point is reasonable.

A good method for an unknown number of points could be the following one:

  • let P[0], P[1], ... P[n-1] be the list of points to sort
  • let M be the barycenter of all points
  • compute a[0], a[1], ... a[n-1] such that a[i] = atan2(P[i].y - M.y, P[i].x - M.x);
  • sort points relative to their a value, using qsort for instance.

However, you can be sure that a good sorting algorithm will perform poorly with three input values compared to an ad-hoc method. Using atan2 is still valid, but just don't use qsort.

add diagram
Source Link
sam hocevar
  • 23.9k
  • 2
  • 64
  • 95

Your question is not precise enough. An array of points is only « clockwise » or « anti-clockwise » relative to a reference point. Otherwise, any array of three points can always be either CW or CCW. See the following picture: on the left, the points are ordered clockwise; on the right, the exact same points are ordered anticlockwise.

clockwise or anticlockwise http://zoy.org/%7Esam/cw-ccw.png

In your case, I believe using the barycenter of the points as a reference point is reasonable.

A good method for an unknown number of points could be the following one:

  • let P[0], P[1], ... P[n-1] be the list of points to sort
  • let M be the barycenter of all points
  • compute a[0], a[1], ... a[n-1] such as a[i] = atan2(P[i].y - M.y, P[i].x - M.x);
  • sort points relative to their a value, using qsort for instance.

However, you can be sure that a good sorting algorithm will perform poorly with three input values compared to an ad-hoc method. Using atan2 is still valid, but just don't use qsort.

Your question is not precise enough. An array of points is only « clockwise » or « anti-clockwise » relative to a reference point. Otherwise, any array of three points can always be either CW or CCW. In your case, I believe using the barycenter of the points as a reference point is reasonable.

A good method for an unknown number of points could be the following one:

  • let P[0], P[1], ... P[n-1] be the list of points to sort
  • let M be the barycenter of all points
  • compute a[0], a[1], ... a[n-1] such as a[i] = atan2(P[i].y - M.y, P[i].x - M.x);
  • sort points relative to their a value, using qsort for instance.

However, you can be sure that a good sorting algorithm will perform poorly with three input values compared to an ad-hoc method. Using atan2 is still valid, but just don't use qsort.

Your question is not precise enough. An array of points is only « clockwise » or « anti-clockwise » relative to a reference point. Otherwise, any array of three points can always be either CW or CCW. See the following picture: on the left, the points are ordered clockwise; on the right, the exact same points are ordered anticlockwise.

clockwise or anticlockwise http://zoy.org/%7Esam/cw-ccw.png

In your case, I believe using the barycenter of the points as a reference point is reasonable.

A good method for an unknown number of points could be the following one:

  • let P[0], P[1], ... P[n-1] be the list of points to sort
  • let M be the barycenter of all points
  • compute a[0], a[1], ... a[n-1] such as a[i] = atan2(P[i].y - M.y, P[i].x - M.x);
  • sort points relative to their a value, using qsort for instance.

However, you can be sure that a good sorting algorithm will perform poorly with three input values compared to an ad-hoc method. Using atan2 is still valid, but just don't use qsort.

Source Link
sam hocevar
  • 23.9k
  • 2
  • 64
  • 95
Loading