-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBruteCollinearPoints.java
More file actions
93 lines (82 loc) · 3.18 KB
/
BruteCollinearPoints.java
File metadata and controls
93 lines (82 loc) · 3.18 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
import java.util.ArrayList;
import java.util.Arrays;
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdDraw;
public class BruteCollinearPoints {
private final LineSegment[] lines;
private final int totalSegments;
public BruteCollinearPoints(Point[] points) {
if (points == null) throw new IllegalArgumentException();
Point[] pointsData = new Point[points.length];
for (int a = 0; a < points.length; a++) {
if (points[a] == null) throw new IllegalArgumentException();
pointsData[a] = points[a];
}
int total = 0;
ArrayList<Integer> start = new ArrayList<Integer>();
ArrayList<Integer> end = new ArrayList<Integer>();
Arrays.sort(pointsData);
for (int i = 0; i < pointsData.length; i++) {
for (int j = i + 1; j < pointsData.length; j++) {
if (pointsData[i].compareTo(pointsData[j]) == 0) throw new IllegalArgumentException();
for (int k = j + 1; k < pointsData.length; k++) {
if (doubleEq(pointsData[i].slopeTo(pointsData[j]), pointsData[i].slopeTo(pointsData[k]))) {
for (int a = k + 1; a < pointsData.length; a++) {
if (doubleEq(pointsData[i].slopeTo(pointsData[k]), pointsData[i].slopeTo(pointsData[a]))) {
total++;
start.add(total - 1, i);
end.add(total - 1, a);
break;
}
}
}
}
}
}
totalSegments = total;
lines = new LineSegment[totalSegments];
for (int m = 0; m < totalSegments; m++) {
lines[m] = new LineSegment(pointsData[start.get(m)], pointsData[end.get(m)]);
}
}
private boolean doubleEq(double a, double b) {
if (a < b) return false;
else if (a > b) return false;
else return true;
}
public int numberOfSegments() {
int ret = totalSegments;
return ret;
}
public LineSegment[] segments() {
LineSegment[] ret = new LineSegment[lines.length];
for (int i = 0; i < lines.length; i++) ret[i] = lines[i];
return ret;
}
public static void main(String[] args) {
In in = new In(args[0]);
int n = in.readInt();
Point[] pointsData = new Point[n];
for (int i = 0; i < n; i++) {
int x = in.readInt();
int y = in.readInt();
pointsData[i] = new Point(x, y);
}
// draw the pointsData
StdDraw.enableDoubleBuffering();
StdDraw.setXscale(0, 32768);
StdDraw.setYscale(0, 32768);
for (Point p : pointsData) {
p.draw();
}
StdDraw.show();
// print and draw the line segments
BruteCollinearPoints collinear = new BruteCollinearPoints(pointsData);
for (LineSegment segment : collinear.segments()) {
StdOut.println(segment);
segment.draw();
}
StdDraw.show();
}
}