-
Notifications
You must be signed in to change notification settings - Fork 24
/
BruteCollinearPoints.java
76 lines (71 loc) · 2.16 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
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
package three;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author sunyue
* @version 1.0 2017/1/13 11:22
*/
public class BruteCollinearPoints {
private LineSegment[] ls;
/**
* finds all line segments containing 4 points
*
* @param points
*/
public BruteCollinearPoints(Point[] points) {
// Corner cases
if (points == null) throw new NullPointerException("argument is null");
int n = points.length;
for (int i = 0; i < n; i++) {
if (points[i] == null) throw new NullPointerException("array contains null point");
for (int j = i + 1; j < n; j++) {
if (points[i].compareTo(points[j]) == 0)
throw new IllegalArgumentException("array contains a repeated point");
}
}
Point[] ps = points.clone();
Arrays.sort(ps);
List<LineSegment> list = new ArrayList<>();
// Brute force
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
for (int l = k + 1; l < n; l++) {
Point[] p = new Point[4];
p[0] = ps[i];
p[1] = ps[j];
p[2] = ps[k];
p[3] = ps[l];
double s1 = p[0].slopeTo(p[1]);
double s2 = p[0].slopeTo(p[2]);
if (s1 != s2) continue;
double s3 = p[0].slopeTo(p[3]);
if (s1 == s3) {
Arrays.sort(p);
list.add(new LineSegment(p[0], p[3]));
}
}
}
}
}
// transform to array
ls = list.toArray(new LineSegment[list.size()]);
}
/**
* the number of line segments
*
* @return
*/
public int numberOfSegments() {
return ls.length;
}
/**
* the line segments
*
* @return
*/
public LineSegment[] segments() {
return ls.clone();
}
}