-
Notifications
You must be signed in to change notification settings - Fork 1
/
Point3D.java
169 lines (149 loc) · 4.78 KB
/
Point3D.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Inspiration : https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Point3D.java.html
*/
import java.util.Comparator;
import java.util.Random;
import java.util.Arrays;
// Immutable class for 3D points
class Point3D implements Comparable<Point3D> {
final double x;
final double y;
final double z;
public static final Comparator<Point3D> X_ORDER = new XOrder();
public static final Comparator<Point3D> Y_ORDER = new YOrder();
public static final Comparator<Point3D> Z_ORDER = new ZOrder();
public Point3D(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
// Compare points according to their x-coordinate
private static class XOrder implements Comparator<Point3D> {
public int compare(Point3D p, Point3D 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<Point3D> {
public int compare(Point3D p, Point3D q) {
if (p.y < q.y) {
return -1;
} else if (p.y > q.y) {
return 1;
} else {
return 0;
}
}
}
// Compare points according to their y-coordinate
private static class ZOrder implements Comparator<Point3D> {
public int compare(Point3D p, Point3D q) {
if (p.z < q.z) {
return -1;
} else if (p.z > q.z) {
return 1;
} else {
return 0;
}
}
}
// Find distance between 2 points
public double distanceTo(Point3D other) {
return Math.sqrt((other.x - x) * (other.x - x) +
(other.y - y) * (other.y - y) +
(other.z - z) * (other.z - z));
}
/**
* Compares two points by x-coordinate, breaking ties by y-coordinate.
*/
@Override
public int compareTo(Point3D 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,%.2f)", x, y, z);
}
/**
* 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;
}
Point3D that = (Point3D)other;
return this.x == that.x && this.y == that.y && this.z == that.z;
}
/**
* Returns an integer hash code for this object.
*/
@Override
public int hashCode() {
int hashX = ((Double) x).hashCode();
int hashY = ((Double) y).hashCode();
int hashZ = ((Double) z).hashCode();
return ((31 * hashX) + hashY) * 31 + hashZ;
}
// Return an array of Point3Ds with random coordinates
public static Point3D[] generateRandomPointArray(int n, double min, double max) {
if (n <= 0) {
throw new IllegalArgumentException("Argument n can't be less than 1");
}
Point3D[] points = new Point3D[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();
double z = min + (max - min) * rand.nextDouble();
points[i] = new Point3D(x, y, z);
}
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;
Point3D[] points = generateRandomPointArray(n, min, max);
System.out.println("\nSorted by x coordinates");
Arrays.sort(points, Point3D.X_ORDER);
Point3D.print(points);
System.out.println("\nSorted by y coordinates");
Arrays.sort(points, Point3D.Y_ORDER);
Point3D.print(points);
System.out.println("\nSorted by z coordinates");
Arrays.sort(points, Point3D.Z_ORDER);
Point3D.print(points);
}
}