6 * http://stackoverflow.com/questions/6989100/sort-points-in-clockwise-order
8 public class AntiClockSort
extends PolygonSorter
{
10 public AntiClockSort(double[][] points
) {
15 * Compare polygon vertices in counter-clock wise order starting at six
16 * hour. If two points share the same rad, then the farest to the center is
19 * @param a: a point to compare
20 * @param b: a second point to compare
24 public int compare(PointDouble a
, PointDouble b
) {
25 if (a
.x
- center
.x
>= 0 && b
.x
- center
.x
< 0) {
28 if (a
.x
- center
.x
< 0 && b
.x
- center
.x
>= 0) {
31 if (a
.x
- center
.x
== 0 && b
.x
- center
.x
== 0) {
32 if (a
.y
- center
.y
>= 0 || b
.y
- center
.y
>= 0) {
33 return (a
.y
> b
.y
) ?
-1 : +1;
35 return (b
.y
> a
.y
) ?
-1 : +1;
38 // compute the cross product of vectors (center -> a) x (center -> b)
39 double det
= (a
.x
- center
.x
) * (b
.y
- center
.y
) - (b
.x
- center
.x
) * (a
.y
- center
.y
);
47 // points a and b are on the same line from the center
48 // check which point is closer to the center
49 double d1
= (a
.x
- center
.x
) * (a
.x
- center
.x
) + (a
.y
- center
.y
) * (a
.y
- center
.y
);
50 double d2
= (b
.x
- center
.x
) * (b
.x
- center
.x
) + (b
.y
- center
.y
) * (b
.y
- center
.y
);
51 return (d1
> d2
) ?
-1 : +1;