Merge: Loose Tokens
[nit.git] / benchmarks / polygons / java / code / ClockSort.java
1
2 /**
3 *
4 * @author Johan
5 */
6 public class ClockSort extends PolygonSorter {
7
8 public ClockSort(double[][] points) {
9 super(points);
10 }
11
12 /**
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.
15 *
16 * @param a: a point to compare
17 * @param b: a second point to compare
18 * @return
19 */
20 @Override
21 public int compare(PointDouble a, PointDouble b) {
22 if (a.x - center.x >= 0 && b.x - center.x < 0) {
23 return +1;
24 }
25 if (a.x - center.x < 0 && b.x - center.x >= 0) {
26 return -1;
27 }
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;
31 }
32 return (b.y > a.y) ? +1 : -1;
33 }
34
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);
37 if (det < 0.0d) {
38 return +1;
39 }
40 if (det > 0.0d) {
41 return -1;
42 }
43
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;
49 }
50 }