-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPoint2D.java
140 lines (124 loc) · 3.79 KB
/
Point2D.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Inspiration : https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Point2D.java.html
*/
import java.util.Comparator;
import java.util.Random;
import java.util.Arrays;
// Immutable class for 2D points
class Point2D implements Comparable<Point2D> {
final double x;
final double y;
public static final Comparator<Point2D> X_ORDER = new XOrder();
public static final Comparator<Point2D> Y_ORDER = new YOrder();
public Point2D(double x, double y) {
this.x = x;
this.y = y;
}
// Compare points according to their x-coordinate
private static class XOrder implements Comparator<Point2D> {
public int compare(Point2D p, Point2D q) {
if (p.x < q.x) {
return -1;
} else if (p.x > q.x) {
return 1;
} else {
return 0;
}
}
}
// Compare points according to their y-coordinate
private static class YOrder implements Comparator<Point2D> {
public int compare(Point2D p, Point2D q) {
if (p.y < q.y) {
return -1;
} else if (p.y > q.y) {
return 1;
} else {
return 0;
}
}
}
// Find distance between 2 points
public double distanceTo(Point2D other) {
return Math.sqrt((other.x - x) * (other.x - x) +
(other.y - y) * (other.y - y));
}
/**
* Compares two points by x-coordinate, breaking ties by y-coordinate.
*/
@Override
public int compareTo(Point2D that) {
if (this.x < that.x) {
return -1;
}
if (this.x > that.x) {
return 1;
}
if (this.y < that.y) {
return -1;
}
if (this.y > that.y) {
return 1;
}
return 0;
}
/**
* Returns a string representation of this object
*/
@Override
public String toString() {
return String.format("(%.2f,%.2f)", x, y);
}
/**
* Compares this point to the specified point.
*/
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (other == null || other.getClass() != this.getClass()) {
return false;
}
Point2D that = (Point2D)other;
return this.x == that.x && this.y == that.y;
}
/**
* Returns an integer hash code for this object.
*/
@Override
public int hashCode() {
int hashX = ((Double) x).hashCode();
int hashY = ((Double) y).hashCode();
return 31 * hashX + hashY;
}
// Return an array of Point2Ds with random coordinates
public static Point2D[] generateRandomPointArray(int n, double min, double max) {
if (n <= 0) {
throw new IllegalArgumentException("Argument n can't be less than 1");
}
Point2D[] points = new Point2D[n];
Random rand = new Random();
for (int i = 0; i < n; ++i) {
double x = min + (max - min) * rand.nextDouble();
double y = min + (max - min) * rand.nextDouble();
points[i] = new Point2D(x, y);
}
return points;
}
// Utility functions to print array
public static void print(Object[] array) {
for (Object a : array) {
System.out.print(a + " ");
}
System.out.println();
}
public static void main(String []args){
int n = 10;
double min = 0.0;
double max = 10.0;
Point2D[] points = generateRandomPointArray(n, min, max);
Arrays.sort(points, Point2D.X_ORDER);
Point2D.print(points);
}
}