-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFast.java
269 lines (211 loc) · 6.41 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//package PatternRecognition;
import java.util.ArrayList;
import java.util.Arrays;
//import edu.princeton.cs.introcs.In;
//import edu.princeton.cs.introcs.StdDraw;
public class Fast {
private static ArrayList<Point> drawStart = new ArrayList<Point>();
private static ArrayList<Point> drawEnd = new ArrayList<Point>();
private static Point[] readInput(String filename) {
In in = new In(filename);
int n = in.readInt();
Point[] points = new Point[n];
for (int i = 0; i < n; i++) {
points[i] = new Point(in.readInt(), in.readInt());
}
return points;
}
private static void drawSetup()
{
StdDraw.setXscale(0, 32768);
StdDraw.setYscale(0, 32768);
//StdDraw.setPenRadius(0.01); // make the points a bit larger
}
private static int[] toSlopeSort(int k, Point[] points) // points[k] is the origin, sort by points[k] --> points[i]
{
int N = points.length;
//if (N >= 3)
//{
// p is the base
double[] slope = new double[N-1]; // slope array:
int[] index = new int[N-1]; // index array: 1 2 3 4 5 6 ...
for (int j=0; j<points.length;j++) // initialization
{
if (j < k)
{
slope[j] = points[k].slopeTo(points[j]);
//System.out.print(points[k].toString()+" "+points[j].toString()+" "+slope[j]);
//System.out.print("\n");
index[j] = j;
}
if (j > k)
{
slope[j-1] = points[k].slopeTo(points[j]);
//System.out.print(points[k].toString()+" "+points[j].toString()+" "+slope[j-1]);
//System.out.print("\n");
index[j-1] = j;
}
}
/* for (int i=1;i<slope.length;i++)
{
System.out.print(slope[i]+" ");
}
System.out.print(" "+points[k].toString()+"\n");*/
for (int i=1;i<slope.length;i++)
{
for (int j=i-1;j>=0;j--)
{
if (slope[j+1]<slope[j])
{
double tmpSlope = slope[j+1];
slope[j+1] = slope[j];
slope[j] = tmpSlope;
int tmpIndex = index[j+1];
index[j+1] = index[j];
index[j] = tmpIndex;
}
}
}
/* for (int i=1;i<slope.length;i++)
{
System.out.print(slope[i]+" ");
}
System.out.print("\n");*/
//}
return index;
}
private static Point[] toSort(Point[] points) // sort input according to its coordinates
{
for (int i=1;i<points.length;i++)
{
for (int j=i-1;j>=0;j--)
{
if (points[j+1].compareTo(points[j]) == 1) //if smaller, swap
{
Point tmpPoint = points[j+1];
points[j+1] = points[j];
points[j] = tmpPoint;
}
}
}
return points;
}
private static void drawLine(Point[] points) // sorted points
{
int N = points.length;
assert N >= 2;
for (int i = 0; i < N; i++ )
{
points[i].draw();
}
points[0].drawTo(points[N-1]);
StdDraw.show(0);
}
private static void printLine(Point[] points)
{
int N = points.length;
assert N >= 2;
for (int i = 0; i < N-1; i++)
{
System.out.printf (points[i].toString() + " -> ");
}
System.out.printf (points[N-1].toString()+"\n");
}
private static void checkAndDraw(Point pointK, Point[] restpoints) //input base point and the rest points which has the save slopes
{
Point p ;
Point q ;
Point[] points = toSort(restpoints);
Point[] pointsNew = new Point[points.length+1]; //merge with pointK
//compare pointK to the rest sorted points
boolean insert = false;
if (pointK.compareTo(points[points.length-1]) != 1) //if pointK is larger than the rest
pointsNew[pointsNew.length-1] = pointK;
for (int i=0;i<points.length;i++)
{
if ((pointK.compareTo(points[i]) == 1) && (!insert) ) //means smaller
{
pointsNew[i] = pointK;
pointsNew[i+1] = points[i];
insert = true; //inserted
}
else if ((pointK.compareTo(points[i]) != 1) && (!insert) )
pointsNew[i] = points[i];
else
pointsNew[i+1] = points[i];
}
/* for (int i=0;i<pointsNew.length;i++)
{
System.out.print(pointsNew[i]+" ");
}
System.out.print("\n");*/
p = pointsNew[0];
q = pointsNew[pointsNew.length-1];
boolean flag = false; //true: exist
for (int i=0;i<drawStart.size();i++)
{
if ((drawStart.get(i).compareTo(p) == 0)&&(drawEnd.get(i).compareTo(q) == 0))
flag = true;
}
if (!flag) //draw
{
drawStart.add(p);
drawEnd.add(q);
drawLine(pointsNew);
printLine(pointsNew);
}
}
public static void main(String[] args) {
final int MINI_POINT= 4;
int start;
int end;
boolean flag; // found
Point[] points = readInput (args[0]);
drawSetup();
for (int k=0;k<points.length;k++)
{
//int k = 1
int[] index = toSlopeSort(k, points);
//initial
start = 0; end = 0; flag = false; //flag: start found
for (int i=0;i<index.length-1;i++)
{
//System.out.print(points[k].slopeTo(points[index[i]])+" "+points[k].slopeTo(points[index[i+1]])+"\n");
if ( ( points[k].slopeTo(points[index[i]]) == points[k].slopeTo(points[index[i+1]]) ) && (!flag) )
{
start = i;
flag = true;
//System.out.print(start+"\n");
}
if ( ( ( points[k].slopeTo(points[index[i]]) < points[k].slopeTo(points[index[i+1]]) ) && flag ) )
{
end = i;
flag = false;
//System.out.print(end+"\n");
}
else if ((i+1==index.length-1) && flag)
{
end = i+1;
flag = false;
//System.out.print(end+"\n");
}
if (end-start+1>=MINI_POINT-1) // MINI_POINT = 4 --> 3 slopes
{
Point[] pointsFound = new Point[end-start+1];
for (int ii=0;ii<pointsFound.length;ii++)
{
pointsFound[ii] = points[index[start+ii]];
}
/* for (int x=0;x<pointsFound.length;x++)
{
System.out.print(pointsFound[x]+" ");
}
System.out.print("\n");*/
checkAndDraw(points[k],pointsFound);
}
}
}
// display to screen all at once
StdDraw.show(0);
}
}