-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCollinearPoints.java
More file actions
80 lines (66 loc) · 1.97 KB
/
Copy pathCollinearPoints.java
File metadata and controls
80 lines (66 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.Comparator;
import edu.princeton.cs.algs4.StdDraw;
public class Point implements Comparable<Point> {
private final int x;
private final int y;
private static class ByOrder implements Comparator<Point> {
public int compare(Point pointA, Point pointB) {
if (this.slopeTo(pointA) < this.slopeTo(pointB)) {
return -1;
} else if (this.slopeTo(pointA) == this.slopeTo(pointB)) {
return 0;
} else {
return 1;
}
}
}
public static final Comparator<Point> BY_ORDER = new ByOrder();
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public void draw() {
StdDraw.point(x, y);
}
public void drawTo(Point that) {
StdDraw.line(this.x, this.y, that.x, that.y);
}
public int compareTo(Point that) {
if ((this.y < that.y) || ((this.y == that.y) && (this.x < that.x))) {
return -1;
} else if ((this.y == that.y) && (this.x == that.x)) {
return 0;
} else {
return 1;
}
}
public double slopeTo(Point that) {
int num = that.y - this.y;
int den = that.x - this.y;
// Case 1 line is vertical, same x coordinates
if (den == 0) {
return Double.POSITIVE_INFINITY;
}
// Case 2 line is horizontal, same y coordinates
if (num == 0) {
return 0.0;
}
// Case 3 if points are same
if (this.compareTo(that) == 0) {
return Double.NEGATIVE_INFINITY;
}
return (1.0 * num) / (1.0 * den);
}
public Comparator<Point> slopeOrder() {
return BY_ORDER;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
public static void main(String[] args) {
Point p1 = new Point(1, 2);
Point p2 = new Point(100, 100);
p1.draw();
p1.drawTo(p2);
}
}