时间给的非常充裕(N^4),而且是穷举的方式 比较简单。 下一个 Fast class 就相对难一些了
Brute Collinear Points:
public class BruteCollinearPoints {
private LineSegment[] segarr = new LineSegment[1];
private int segnum = 0;
public BruteCollinearPoints(Point[] points) {
// finds all line segments containing 4 points
int n = points.length;
for (int p = 0; p < n; p++) {
for (int q = p + 1; q < n; q++) {
for (int r = q + 1; r < n; r++)
if (points[p].slopeTo(points[q])
== points[p].slopeTo(points[r])) {
for (int s = r + 1; s < n; s++) {
if (points[p].slopeTo(points[q])
== points[p].slopeTo(points[s])) {
segarrin(new LineSegment
(points[p], points[s])); //??first try
}
}
}
}
}
}
private void segarrin(LineSegment line) {
// Debugged;
if (segarr.length == segnum) {
LineSegment[] temp = segarr;
segarr = new LineSegment[(segnum + 1) * 2];
for (int i = 0; i < segnum; i++) {
segarr[i] = temp[i];
}
}
segarr[segnum++] = line;
}
public int numberOfSegments() {
return segnum;
}
public LineSegment[] segments() {
return segarr;
}
private void printer() {
for (int i = 0; i < segnum; i++) {
segarr[i].draw();
System.out.println(segarr[i].toString());
}
}
}