-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrute.java
80 lines (72 loc) · 2.69 KB
/
Brute.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
77
78
79
80
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Brute {
private static void solve(List<Point> points) {
int length = points.size();
Collections.sort(points);
HashSet<TreeSet<Point>> solutions = new HashSet<>();
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
double slope1 = points.get(i).slopeTo(points.get(j));
for (int k = j + 1; k < length; k++) {
double slope2 = points.get(j).slopeTo(points.get(k));
if (slope1 != slope2) continue;
for (int l = k + 1; l < length; l++) {
double slope3 = points.get(k).slopeTo(points.get(l));
if (slope2 == slope3) {
TreeSet<Point> solution = new TreeSet<>();
solution.add(points.get(i));
solution.add(points.get(j));
solution.add(points.get(k));
solution.add(points.get(l));
solutions.add(solution);
}
}
}
}
}
displaySolutions(solutions);
}
private static void displaySolutions(Set<TreeSet<Point>> solutions) {
for (TreeSet<Point> solution : solutions) {
solution.first().drawTo(solution.last());
int i = 0;
int size = solution.size();
for (Point point : solution) {
System.out.print(point);
if (i < size - 1) {
System.out.print(" -> ");
}
i++;
}
System.out.println();
}
StdDraw.show(0);
}
private static ArrayList<Point> readPoints(String filename) throws FileNotFoundException {
Scanner in = new Scanner(new File(filename));
int n = in.nextInt();
ArrayList<Point> points = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
points.add(new Point(in.nextInt(), in.nextInt()));
}
return points;
}
public static void main(String[] args) throws FileNotFoundException {
String fileName = "inputs/collinear/equidistant.txt";
if (args.length != 0) {
fileName = args[0];
}
ArrayList<Point> points = readPoints(fileName);
if (points.size() > 3) {
StdDraw.setXscale(0, 32768);
StdDraw.setYscale(0, 32768);
StdDraw.show(0);
for (Point point : points) {
point.draw();
}
solve(points);
}
}
}