6 public class ClockSort
extends PolygonSorter
{
8 public ClockSort(double[][] points
) {
13 * Compare polygon vertices in clock wise order starting at six hour. If two
14 * points share the same rad, then the farest to the center is chosen.
16 * @param a: a point to compare
17 * @param b: a second point to compare
21 public int compare(PointDouble a
, PointDouble b
) {
22 if (a
.x
- center
.x
>= 0 && b
.x
- center
.x
< 0) {
25 if (a
.x
- center
.x
< 0 && b
.x
- center
.x
>= 0) {
28 if (a
.x
- center
.x
== 0 && b
.x
- center
.x
== 0) {
29 if (a
.y
- center
.y
>= 0 || b
.y
- center
.y
>= 0) {
30 return (a
.y
> b
.y
) ?
+1 : -1;
32 return (b
.y
> a
.y
) ?
+1 : -1;
35 // compute the cross product of vectors (center -> a) x (center -> b)
36 double det
= (a
.x
- center
.x
) * (b
.y
- center
.y
) - (b
.x
- center
.x
) * (a
.y
- center
.y
);
44 // points a and b are on the same line from the center
45 // check which point is closer to the center
46 double d1
= (a
.x
- center
.x
) * (a
.x
- center
.x
) + (a
.y
- center
.y
) * (a
.y
- center
.y
);
47 double d2
= (b
.x
- center
.x
) * (b
.x
- center
.x
) + (b
.y
- center
.y
) * (b
.y
- center
.y
);
48 return (d1
> d2
) ?
+1 : -1;