Repository navigation
Expand file tree
/
Copy pathBruteCollinearPoints.java
More file actions
84 lines (72 loc) · 2.76 KB
/
Copy pathBruteCollinearPoints.java
File metadata and controls
84 lines (72 loc) · 2.76 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
package recognition;
import java.util.Arrays;
public class BruteCollinearPoints {
private int segmentNumber = 0;
private LineSegment[] Segment = null;
// finds all line segments containing 4 points
public BruteCollinearPoints(Point[] point) {
if (null == point) {
throw new NullPointerException();
}
pointCheck(point);
LineSegment[] lineSegment = null;
Point[] points = new Point[point.length];
for (int i = 0; i < point.length; i++) {
points[i] = point[i];
}
Arrays.sort(points);
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
for (int n = j + 1; n < points.length; n++) {
for (int m = n + 1; m < points.length; m++) {
if (isSlopeEqual(points[i], points[j], points[n])
&& isSlopeEqual(points[i], points[j], points[m])) {
if (null == lineSegment) {
lineSegment = new LineSegment[20];
} else if (segmentNumber == lineSegment.length) {
LineSegment[] copySegment = new LineSegment[2 * lineSegment.length];
for (int k = 0; k < lineSegment.length; k++) {
copySegment[k] = lineSegment[k];
}
lineSegment = copySegment;
copySegment = null;
}
lineSegment[segmentNumber] = new LineSegment(points[i], points[m]);
segmentNumber++;
}
}
}
}
}
Segment = new LineSegment[segmentNumber];
for (int i = 0; i < segmentNumber; i++) {
Segment[i] = lineSegment[i];
}
lineSegment = null;
}
private void pointCheck(Point[] points) {
for (Point point : points) {
if (null == point) {
throw new NullPointerException();
}
}
for (int i = 0; i < points.length - 1; i++) {
for (int j = i + 1; j < points.length; j++) {
if (points[i].compareTo(points[j]) == 0) {
throw new IllegalArgumentException();
}
}
}
}
private boolean isSlopeEqual(Point p0, Point p1, Point p2) {
return p0.slopeTo(p1) == p0.slopeTo(p2);
}
// the number of line segments
public int numberOfSegments() {
return segmentNumber;
}
// the line segments
public LineSegment[] segments() {
return Segment.clone();
}
}