-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastCollinearPoints.java
91 lines (77 loc) · 2.75 KB
/
FastCollinearPoints.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
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;
import java.util.ArrayList;
import java.util.Arrays;
public class FastCollinearPoints {
private final ArrayList<LineSegment> lineSegments;
// finds all line segments containing 4 or more points
public FastCollinearPoints(Point[] points) {
if (points == null || Arrays.asList(points).contains(null)) throw new IllegalArgumentException();
int n = points.length;
Point[] copy = points.clone();
lineSegments = new ArrayList<>();
if (n == 1) return;
for (int p = 0; p < n; p++) {
Arrays.sort(copy);
if (p != 0 && copy[p].compareTo(copy[p - 1]) == 0) throw new IllegalArgumentException();
Point cacheP = points[p];
Arrays.sort(copy, cacheP.slopeOrder());
int q = -1;
int r = -1;
double slope1 = cacheP.slopeTo(copy[1]);
double slope2 = Double.NEGATIVE_INFINITY;
for (int i = 2; i <= n; i++) {
if (i != n) slope2 = cacheP.slopeTo(copy[i]);
if (slope2 == slope1) {
if (q == -1) {
q = i - 1;
r = i;
} else r++;
} else {
if (r - q + 2 >= 4 && cacheP.compareTo(copy[q]) < 0)
lineSegments.add(new LineSegment(cacheP, copy[r]));
q = -1;
r = -1;
}
slope1 = slope2;
}
}
}
public static void main(String[] args) {
// read the n points from a file
In in = new In(args[0]);
int n = in.readInt();
Point[] points = new Point[n];
for (int i = 0; i < n; i++) {
int x = in.readInt();
int y = in.readInt();
points[i] = new Point(x, y);
}
// draw the points
StdDraw.enableDoubleBuffering();
StdDraw.setXscale(0, 32768);
StdDraw.setYscale(0, 32768);
for (Point p : points) {
p.draw();
}
StdDraw.show();
// print and draw the line segments
FastCollinearPoints collinear = new FastCollinearPoints(points);
for (LineSegment segment : collinear.segments()) {
StdOut.println(segment);
segment.draw();
StdDraw.show();
StdDraw.pause(10);
}
StdOut.println(collinear.numberOfSegments());
}
// the number of line segments
public int numberOfSegments() {
return lineSegments.size();
}
// the line segments
public LineSegment[] segments() {
return lineSegments.toArray(new LineSegment[0]);
}
}