-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBruteCollinearPoints.java
48 lines (45 loc) · 2.07 KB
/
BruteCollinearPoints.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
import java.util.ArrayList;
import java.util.Arrays;
public class BruteCollinearPoints {
private LineSegment [] segments;
public BruteCollinearPoints(Point[] points) {
if (points == null) throw new java.lang.NullPointerException("null argument to constructor");
checkNullEntries(points);
ArrayList<LineSegment> segmentsList = new ArrayList<LineSegment>();
Point[] pointsCopy = Arrays.copyOf(points, points.length);
Arrays.sort(pointsCopy);
checkDuplicatedEntries(pointsCopy);
for (int i = 0; i < (pointsCopy.length - 3); ++i)
for (int j = i + 1; j < (pointsCopy.length - 2); ++j)
for (int k = j + 1; k < (pointsCopy.length - 1); ++k)
for (int l = k + 1; l < (pointsCopy.length); ++l) {
if (pointsCopy[i].slopeTo(pointsCopy[j]) == pointsCopy[i].slopeTo(pointsCopy[l]) &&
pointsCopy[i].slopeTo(pointsCopy[j]) == pointsCopy[i].slopeTo(pointsCopy[k])) {
LineSegment tempLineSegment = new LineSegment(pointsCopy[i], pointsCopy[l]);
if (!segmentsList.contains(tempLineSegment))
segmentsList.add(tempLineSegment);
}
}
segments = segmentsList.toArray(new LineSegment[segmentsList.size()]);
}
public int numberOfSegments() {
return segments.length;
}
public LineSegment[] segments() {
return segments;
}
private void checkDuplicatedEntries(Point[] points) {
for (int i = 0; i < points.length - 1; i++) {
if (points[i].compareTo(points[i + 1]) == 0) {
throw new IllegalArgumentException("Duplicated entries in given points");
}
}
}
private void checkNullEntries(Point[] points) {
for (int i = 0; i < points.length - 1; i++) {
if (points[i] == null) {
throw new java.lang.NullPointerException("One of the point in points array is null");
}
}
}
}