-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fast.java
66 lines (50 loc) · 1.75 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
import java.util.*;
public class Fast {
public static void main(String[] args)
{
String filename = args[0];
In in = new In(filename);
int N = in .readInt();
Point[] points = new Point[N];
Point[] aux = new Point[N];
StdDraw.setXscale(0, 32768);
StdDraw.setYscale(0, 32768);
StdDraw.show(2000);
for (int i = 0; i < N; i++) {
int x = in .readInt();
int y = in .readInt();
Point p = new Point(x, y);
p.draw();
points[i] = p;
aux[i] = p;
}
Arrays.sort(points);
for (int i = 0; i < N; i++) {
Arrays.sort(aux,points[i].SLOPE_ORDER);
int lastJ = 1 , j;
for (j = 2; j < N;j++)
{
if(points[i].slopeTo(aux[j]) != points[i].slopeTo(aux[j-1]))
{
printCollinearPoints(points,aux,i,j,lastJ);
lastJ = j;
}
}
if(lastJ != j-1)
printCollinearPoints(points,aux,i,j,lastJ);
}
StdDraw.show(0);
}
private static void printCollinearPoints(Point[] points,Point[] aux, int i,int j, int lastJ)
{
Arrays.sort(aux,lastJ,j);
if(points[i].compareTo(aux[lastJ])<0 && (j-lastJ)>=3) //smaller than the smallest one
{
StdOut.print(points[i] + " -> ");
for (int k = lastJ; k < j-1; k++)
StdOut.print(aux[k] + " -> ");
StdOut.println(aux[j-1]);
points[i].drawTo(aux[j-1]);
}
}
}