forked from sctracy/algs-collinear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoint.java
106 lines (91 loc) · 3.27 KB
/
Point.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
/*************************************************************************
* Name:
* Email:
*
* Compilation: javac Point.java
* Execution:
* Dependencies: StdDraw.java
*
* Description: An immutable data type for points in the plane.
*
*************************************************************************/
import java.util.Comparator;
public class Point implements Comparable<Point> {
// compare points by slope
public final Comparator<Point> SLOPE_ORDER = new SlopeOrder();
private final int x; // x coordinate
private final int y; // y coordinate
// create the point (x, y)
public Point(int x, int y) {
/* DO NOT MODIFY */
this.x = x;
this.y = y;
}
private class SlopeOrder implements Comparator<Point> {
public int compare(Point p, Point q) {
double pSlope = slopeTo(p);
double qSlope = slopeTo(q);
if (pSlope > qSlope) return 1;
else if (pSlope < qSlope) return -1;
else return 0;
}
}
// plot this point to standard drawing
public void draw() {
/* DO NOT MODIFY */
StdDraw.point(x, y);
}
// draw line between this point and that point to standard drawing
public void drawTo(Point that) {
/* DO NOT MODIFY */
StdDraw.line(this.x, this.y, that.x, that.y);
}
// slope between this point and that point
public double slopeTo(Point that) {
/* YOUR CODE HERE */
if (this.x == that.x && this.y == that.y) {
/* treat the slope of a degenerate line segment as negative infinity. */
return Double.NEGATIVE_INFINITY;
} else if (this.x == that.x) {
/* treat the slope of a vertical line segment as positive infinity */
return Double.POSITIVE_INFINITY;
} else if (this.y == that.y) {
/* treat the slope of a horizontal line segment as positive zero */
return +0.0;
}
return (double) (that.y - this.y) / (that.x - this.x);
}
// is this point lexicographically smaller than that one?
// comparing y-coordinates and breaking ties by x-coordinates
public int compareTo(Point that) {
/* YOUR CODE HERE */
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;
}
// return string representation of this point
public String toString() {
/* DO NOT MODIFY */
return "(" + x + ", " + y + ")";
}
// unit test
public static void main(String[] args) {
/* YOUR CODE HERE */
Point p = new Point(0, 0);
Point q1 = new Point(0, 2);
Point q2 = new Point(2, 0);
Point q3 = new Point(3, 4);
Point q4 = new Point(4, 3);
StdOut.println(p.slopeTo(q1));
StdOut.println(p.slopeTo(q2));
StdOut.println(p.slopeTo(q3));
StdOut.println(p.slopeTo(q4));
StdOut.println("==================");
StdOut.println(p.compareTo(q1));
StdOut.println(p.compareTo(q2));
StdOut.println(p.compareTo(q3));
StdOut.println(p.compareTo(q4));
}
}