Point2D.java
12.8 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/******************************************************************************
* Compilation: javac Point2D.java
* Execution: java Point2D x0 y0 N
* Dependencies: StdDraw.java StdRandom.java
*
* Immutable point data type for points in the plane.
*
******************************************************************************/
package edu.princeton.cs.algs4;
import java.util.Arrays;
import java.util.Comparator;
/**
* The {@code Point} class is an immutable data type to encapsulate a
* two-dimensional point with real-value coordinates.
* <p>
* Note: in order to deal with the difference behavior of double and
* Double with respect to -0.0 and +0.0, the Point2D constructor converts
* any coordinates that are -0.0 to +0.0.
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/12oop">Section 1.2</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public final class Point2D implements Comparable<Point2D> {
/**
* Compares two points by x-coordinate.
*/
public static final Comparator<Point2D> X_ORDER = new XOrder();
/**
* Compares two points by y-coordinate.
*/
public static final Comparator<Point2D> Y_ORDER = new YOrder();
/**
* Compares two points by polar radius.
*/
public static final Comparator<Point2D> R_ORDER = new ROrder();
private final double x; // x coordinate
private final double y; // y coordinate
/**
* Initializes a new point (x, y).
* @param x the x-coordinate
* @param y the y-coordinate
* @throws IllegalArgumentException if either {@code x} or {@code y}
* is {@code Double.NaN}, {@code Double.POSITIVE_INFINITY} or
* {@code Double.NEGATIVE_INFINITY}
*/
public Point2D(double x, double y) {
if (Double.isInfinite(x) || Double.isInfinite(y))
throw new IllegalArgumentException("Coordinates must be finite");
if (Double.isNaN(x) || Double.isNaN(y))
throw new IllegalArgumentException("Coordinates cannot be NaN");
if (x == 0.0) this.x = 0.0; // convert -0.0 to +0.0
else this.x = x;
if (y == 0.0) this.y = 0.0; // convert -0.0 to +0.0
else this.y = y;
}
/**
* Returns the x-coordinate.
* @return the x-coordinate
*/
public double x() {
return x;
}
/**
* Returns the y-coordinate.
* @return the y-coordinate
*/
public double y() {
return y;
}
/**
* Returns the polar radius of this point.
* @return the polar radius of this point in polar coordiantes: sqrt(x*x + y*y)
*/
public double r() {
return Math.sqrt(x*x + y*y);
}
/**
* Returns the angle of this point in polar coordinates.
* @return the angle (in radians) of this point in polar coordiantes (between -pi/2 and pi/2)
*/
public double theta() {
return Math.atan2(y, x);
}
/**
* Returns the angle between this point and that point.
* @return the angle in radians (between -pi and pi) between this point and that point (0 if equal)
*/
private double angleTo(Point2D that) {
double dx = that.x - this.x;
double dy = that.y - this.y;
return Math.atan2(dy, dx);
}
/**
* Returns true if a→b→c is a counterclockwise turn.
* @param a first point
* @param b second point
* @param c third point
* @return { -1, 0, +1 } if a→b→c is a { clockwise, collinear; counterclocwise } turn.
*/
public static int ccw(Point2D a, Point2D b, Point2D c) {
double area2 = (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x);
if (area2 < 0) return -1;
else if (area2 > 0) return +1;
else return 0;
}
/**
* Returns twice the signed area of the triangle a-b-c.
* @param a first point
* @param b second point
* @param c third point
* @return twice the signed area of the triangle a-b-c
*/
public static double area2(Point2D a, Point2D b, Point2D c) {
return (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x);
}
/**
* Returns the Euclidean distance between this point and that point.
* @param that the other point
* @return the Euclidean distance between this point and that point
*/
public double distanceTo(Point2D that) {
double dx = this.x - that.x;
double dy = this.y - that.y;
return Math.sqrt(dx*dx + dy*dy);
}
/**
* Returns the square of the Euclidean distance between this point and that point.
* @param that the other point
* @return the square of the Euclidean distance between this point and that point
*/
public double distanceSquaredTo(Point2D that) {
double dx = this.x - that.x;
double dy = this.y - that.y;
return dx*dx + dy*dy;
}
/**
* Compares two points by y-coordinate, breaking ties by x-coordinate.
* Formally, the invoking point (x0, y0) is less than the argument point (x1, y1)
* if and only if either {@code y0 < y1} or if {@code y0 == y1} and {@code x0 < x1}.
*
* @param that the other point
* @return the value {@code 0} if this string is equal to the argument
* string (precisely when {@code equals()} returns {@code true});
* a negative integer if this point is less than the argument
* point; and a positive integer if this point is greater than the
* argument point
*/
public int compareTo(Point2D that) {
if (this.y < that.y) return -1;
if (this.y > that.y) return +1;
if (this.x < that.x) return -1;
if (this.x > that.x) return +1;
return 0;
}
/**
* Compares two points by polar angle (between 0 and 2pi) with respect to this point.
*
* @return the comparator
*/
public Comparator<Point2D> polarOrder() {
return new PolarOrder();
}
/**
* Compares two points by atan2() angle (between -pi and pi) with respect to this point.
*
* @return the comparator
*/
public Comparator<Point2D> atan2Order() {
return new Atan2Order();
}
/**
* Compares two points by distance to this point.
*
* @return the comparator
*/
public Comparator<Point2D> distanceToOrder() {
return new DistanceToOrder();
}
// 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;
if (p.x > q.x) return +1;
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;
if (p.y > q.y) return +1;
return 0;
}
}
// compare points according to their polar radius
private static class ROrder implements Comparator<Point2D> {
public int compare(Point2D p, Point2D q) {
double delta = (p.x*p.x + p.y*p.y) - (q.x*q.x + q.y*q.y);
if (delta < 0) return -1;
if (delta > 0) return +1;
return 0;
}
}
// compare other points relative to atan2 angle (bewteen -pi/2 and pi/2) they make with this Point
private class Atan2Order implements Comparator<Point2D> {
public int compare(Point2D q1, Point2D q2) {
double angle1 = angleTo(q1);
double angle2 = angleTo(q2);
if (angle1 < angle2) return -1;
else if (angle1 > angle2) return +1;
else return 0;
}
}
// compare other points relative to polar angle (between 0 and 2pi) they make with this Point
private class PolarOrder implements Comparator<Point2D> {
public int compare(Point2D q1, Point2D q2) {
double dx1 = q1.x - x;
double dy1 = q1.y - y;
double dx2 = q2.x - x;
double dy2 = q2.y - y;
if (dy1 >= 0 && dy2 < 0) return -1; // q1 above; q2 below
else if (dy2 >= 0 && dy1 < 0) return +1; // q1 below; q2 above
else if (dy1 == 0 && dy2 == 0) { // 3-collinear and horizontal
if (dx1 >= 0 && dx2 < 0) return -1;
else if (dx2 >= 0 && dx1 < 0) return +1;
else return 0;
}
else return -ccw(Point2D.this, q1, q2); // both above or below
// Note: ccw() recomputes dx1, dy1, dx2, and dy2
}
}
// compare points according to their distance to this point
private class DistanceToOrder implements Comparator<Point2D> {
public int compare(Point2D p, Point2D q) {
double dist1 = distanceSquaredTo(p);
double dist2 = distanceSquaredTo(q);
if (dist1 < dist2) return -1;
else if (dist1 > dist2) return +1;
else return 0;
}
}
/**
* Compares this point to the specified point.
*
* @param other the other point
* @return {@code true} if this point equals {@code other};
* {@code false} otherwise
*/
@Override
public boolean equals(Object other) {
if (other == this) return true;
if (other == null) return false;
if (other.getClass() != this.getClass()) return false;
Point2D that = (Point2D) other;
return this.x == that.x && this.y == that.y;
}
/**
* Return a string representation of this point.
* @return a string representation of this point in the format (x, y)
*/
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
/**
* Returns an integer hash code for this point.
* @return an integer hash code for this point
*/
@Override
public int hashCode() {
int hashX = ((Double) x).hashCode();
int hashY = ((Double) y).hashCode();
return 31*hashX + hashY;
}
/**
* Plot this point using standard draw.
*/
public void draw() {
StdDraw.point(x, y);
}
/**
* Plot a line from this point to that point using standard draw.
* @param that the other point
*/
public void drawTo(Point2D that) {
StdDraw.line(this.x, this.y, that.x, that.y);
}
/**
* Unit tests the point data type.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
int x0 = Integer.parseInt(args[0]);
int y0 = Integer.parseInt(args[1]);
int n = Integer.parseInt(args[2]);
StdDraw.setCanvasSize(800, 800);
StdDraw.setXscale(0, 100);
StdDraw.setYscale(0, 100);
StdDraw.setPenRadius(0.005);
StdDraw.enableDoubleBuffering();
Point2D[] points = new Point2D[n];
for (int i = 0; i < n; i++) {
int x = StdRandom.uniform(100);
int y = StdRandom.uniform(100);
points[i] = new Point2D(x, y);
points[i].draw();
}
// draw p = (x0, x1) in red
Point2D p = new Point2D(x0, y0);
StdDraw.setPenColor(StdDraw.RED);
StdDraw.setPenRadius(0.02);
p.draw();
// draw line segments from p to each point, one at a time, in polar order
StdDraw.setPenRadius();
StdDraw.setPenColor(StdDraw.BLUE);
Arrays.sort(points, p.polarOrder());
for (int i = 0; i < n; i++) {
p.drawTo(points[i]);
StdDraw.show();
StdDraw.pause(100);
}
}
}
/******************************************************************************
* Copyright 2002-2016, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/