Repository navigation
Expand file tree
/
Copy pathPointTest.java
More file actions
57 lines (49 loc) · 1.97 KB
/
Copy pathPointTest.java
File metadata and controls
57 lines (49 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import edu.princeton.cs.algs4.Knuth;
import java.util.Arrays;
public class PointTest {
public static void main(String[] args) {
// testPointCompare();
// testSlope();
testComparator();
}
private static void testPointCompare() {
Point axisOrigin = new Point(0, 0);
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
Point a = new Point(x, y);
System.out.println(a + " to " + axisOrigin + " -> "+ a.compareTo(axisOrigin));
}
}
}
private static void testSlope() {
Point axisOrigin = new Point(0, 0);
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
Point a = new Point(x, y);
System.out.println(a + " to " + axisOrigin + " -> "+ a.slopeTo(axisOrigin));
}
}
Point twoThirds = new Point(3, 2); // slope 2/3 to origin
System.out.println(twoThirds + " to " + axisOrigin + " -> "+ twoThirds.slopeTo(axisOrigin));
}
private static void testComparator() {
Point originPoint = new Point(0, 0);
Point lowSlopePoint = new Point(1, 1);
Point highSlopePoint = new Point(1, 2);
Point samePoint = new Point(0, 0);
Point verticalPoint = new Point(0, 1);
Point oneEightyPoint = new Point (-1, 0);
Point[] pointsArray = {originPoint, highSlopePoint, lowSlopePoint, samePoint, verticalPoint
,oneEightyPoint};
Knuth.shuffle(pointsArray);
System.out.println("*********Shuffled points:*********");
for (Point p : pointsArray) {
System.out.println(p + " " + String.valueOf(p.slopeTo(originPoint)));
}
System.out.println("*********Sorted points:*********");
Arrays.sort(pointsArray, originPoint.slopeOrder());
for (Point p : pointsArray) {
System.out.println(p + " " + String.valueOf(p.slopeTo(originPoint)));
}
}
}