summaryrefslogtreecommitdiffstats
path: root/Algorithms/Part-I/3-Collinear/Fast.java
blob: 2640d655acfed209c77d707b3379be435cd375a0 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* vim: set expandtab tabstop=4 shiftwidth=4 : */

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public class Fast
{

    public static void main(String[] args)
    {
        class Solution implements Comparable<Solution>
        {
            private Point[] pts;
            private int n = 0;

            public Solution(int n)
            {
                pts = new Point[n];
            }

            public void add(Point p)
            {
                pts[n++] = p;
            }

            public void sort()
            {
                Insertion.sort(pts);
            }

            public void draw()
            {
                pts[0].drawTo(pts[n - 1]);
            }

            public void print()
            {
                String out = "";
                for (int i = 0; i < n; i++)
                    out += pts[i]+" -> ";
                System.out.println(out.substring(0, (out.length() - 4)));
            }

            public boolean equals(Object o)
            {
                if (o == this) return true;
                if (o == null) return false;
                if (o.getClass() != this.getClass()) return false;

                Solution other = (Solution) o;
                if (n != other.n) return false;

                for (int i = 0; i < n; i++)
                    if (pts[i] != other.pts[i])
                        return false;
                return true;

            }

            public int compareTo(Solution that)
            {
                for (int i = 0; i < n; i++)
                {
                    int r = pts[0].compareTo(that.pts[0]);
                    if (r != 0) return r;
                }
                return 0;
            }
        }

        In in = new In(args[0]);
        int n = in.readInt();

        StdDraw.setXscale(0, 32768);
        StdDraw.setYscale(0, 32768);

        Point[] pts = new Point[n];
        for (int i = 0; i < n; i++)
        {
            pts[i] = new Point(in.readInt(), in.readInt());
        }

        List<Solution> res = new ArrayList<Solution>();
        Point[] others = new Point[n];
        for (int i = 0; i < n; i++)
        {
            Point ref = pts[i];
            ref.draw();
            for (int j = 0; j < n; j++)
                others[j] = pts[j];

            Arrays.sort(others, ref.SLOPE_ORDER);

            int start = 0;
            double slope = Double.NEGATIVE_INFINITY;
            for (int j = 1; j < n; j++)
            {
                double sl = ref.slopeTo(others[j]);
                if (sl == Double.NEGATIVE_INFINITY)
                    continue;

                if (slope != sl)
                {
                    if ((start != 0) && ((j - start) > 2)) {
                        Solution sol = new Solution(j - start + 1);
                        sol.add(ref);
                        for (int k = start; k < j; k++)
                            sol.add(others[k]);
                        sol.sort();
                        if (!res.contains(sol))
                            res.add(sol);
                    }
                    slope = sl;
                    start = j;
                }

            }
            if ((n -  start) > 2) {
                Solution sol = new Solution(n - start + 1);
                sol.add(ref);
                for (int k = start; k < n; k++)
                    sol.add(others[k]);
                sol.sort();
                if (!res.contains(sol))
                    res.add(sol);
            }
        }

        Solution[] sorted = new Solution[res.size()];
        res.toArray(sorted);
        Insertion.sort(sorted);
        for (Solution sol : sorted)
        {
            sol.draw();
            sol.print();
        }
        StdDraw.show(0);
    }
}