-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFast.java
83 lines (77 loc) · 2.59 KB
/
Fast.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
81
82
83
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Fast {
private static void solve(Point[] points) {
HashSet<TreeSet<Point>> result = new HashSet<>();
for (Point p : points) {
List<TreeSet<Point>> groups = groupBySlope(p, points);
for (TreeSet<Point> group : groups) {
result.add(group);
}
}
displayPoints(result);
}
private static List<TreeSet<Point>> groupBySlope(Point p, Point[] points) {
// group all points by slope with point p
HashMap<Double, TreeSet<Point>> groups = new HashMap<>();
for (Point q : points) {
double slope = p.slopeTo(q);
if (!groups.containsKey(slope)) {
groups.put(slope, new TreeSet<Point>());
}
groups.get(slope).add(q);
}
// select all groups with size > 3 and
List<TreeSet<Point>> result = new LinkedList<>();
for (Double key : groups.keySet()) {
TreeSet<Point> group = groups.get(key);
if (group.size() > 2) {
group.add(p);
result.add(group);
}
}
return result;
}
private static void displayPoints(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 Point[] readPoints(String filename) throws FileNotFoundException {
Scanner in = new Scanner(new File(filename));
int n = in.nextInt();
Point[] points = new Point[n];
for (int i = 0; i < n; i++) {
points[i] = new Point(in.nextInt(), in.nextInt());
}
return points;
}
public static void main(String[] args) throws FileNotFoundException {
String fileName = "inputs/collinear/input8.txt";
if (args.length != 0) {
fileName = args[0];
}
Point[] points = readPoints(fileName);
if (points.length > 3) {
StdDraw.setXscale(0, 32768);
StdDraw.setYscale(0, 32768);
StdDraw.show(0);
for (Point point : points) {
point.draw();
}
solve(points);
}
}
}