-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexperiment.java
234 lines (179 loc) · 7.01 KB
/
experiment.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
/*************************************************************************
* Compilation: javac RangeSearchVisualizer.java
* Execution: java RangeSearchVisualizer input.txt
* Dependencies: PointSET.java KdTree.java Point2D.java RectHV.java
* StdDraw.java In.java
*
* Read points from a file (specified as a command-line arugment) and
* draw to standard draw. Also draw all of the points in the rectangle
* the user selects by dragging the mouse.
*
* The range search results using the brute-force algorithm are drawn
* in red; the results using the kd-tree algorithms are drawn in blue.
*
*************************************************************************/
import java.util.*;
public class experiment {
private static ArrayList<Point2D> kdpoints;
private static KdTree kdtree;
public static void main(String[] args) {
int N = Integer.parseInt(args[0]); // for how many points
StdDraw.clear();
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(.005);
//StdDraw.setCanvasSize(800, 800);
//StdDraw.setXscale(0, 100);
//StdDraw.setYscale(0, 100);
PointSET brute = new PointSET();
kdtree = new KdTree();
kdpoints = new ArrayList<Point2D>();
// Generates N points between 0-1 and intialize the data structures
Point2D[] points = new Point2D[N];
for (int i = 0; i < N; i++) {
double x = StdRandom.uniform(100);
double y = StdRandom.uniform(100);
x = x/100;
y = y/100;
points[i] = new Point2D(x, y);
points[i].draw();
//System.out.printf("%f %f\n", points[i].x(), points[i].y());
//kdtree.insert(points[i]);
kdpoints.add(points[i]);
brute.insert(points[i]);
}
buildTree(kdpoints,true);
StdDraw.show();
//Random rectangle between 0-0.1
double xmin = StdRandom.uniform(100);
double ymin = StdRandom.uniform(100);
xmin = xmin / 100;
ymin = ymin / 100;
double xmax = xmin + 0.1;
double ymax = ymin + 0.1;
//double xmin = x0>x1?x1:x0;
//double xmax = x0>x1?x0:x1;
//double y0 = StdRandom.uniform(10);
//double y1 = StdRandom.uniform(10);
//double ymin = y0>y1?y1:y0;
//double ymax = y0>y1?y0:y1;
// draw rectangle
RectHV rect = new RectHV(xmin, ymin, xmax, ymax);
//RectHV rect = new RectHV(0.3, 0.4, 0.5, 0.5); //uncomment this line to see the big rectangle, original rectangle is very small (0-.1)
StdDraw.setPenRadius();
StdDraw.setPenColor(StdDraw.BLACK);
rect.draw();
//////////////////////////////////////////////////
// draw the range search results for brute-force data structure in red
StdDraw.setPenRadius(.02);
StdDraw.setPenColor(StdDraw.RED);
long startTime, endTime;
//////
startTime = System.nanoTime();
Iterable<Point2D> bruteRange = brute.range(rect);
endTime = System.nanoTime();
///////
for (Point2D p : bruteRange) {
p.draw();
}
System.out.printf("Time Taken by Brute Force %d \n", endTime - startTime);
//StdDraw.clear();
// draw the range search results for kd-tree in blue
StdDraw.setPenRadius(.01);
StdDraw.setPenColor(StdDraw.BLUE);
/////////
startTime = System.nanoTime();
Iterable<Point2D> kdRange = kdtree.range(rect);
endTime = System.nanoTime();
/////////
for (Point2D p : kdRange){
p.draw();
}
System.out.printf("Time Taken by KDTree %d \n", endTime - startTime);
// System.out.printf("%f %f %f %f\n", xmin, ymin, xmax, ymax);
// draw the points
// StdDraw.setPenColor(StdDraw.BLACK);
// StdDraw.setPenRadius(.007);
// brute.draw();
// draw the rectangle
// StdDraw.setPenColor(StdDraw.BLACK);
// StdDraw.setPenRadius();
/*
// draw the range search results for brute-force data structure in red
StdDraw.setPenRadius(.03);
StdDraw.setPenColor(StdDraw.RED);
long startTime, endTime;
startTime = System.nanoTime();
for (Point2D p : brute.range(rect))
p.draw();
endTime = System.nanoTime();
System.out.printf("Time Taken by Brute Force %d \n", endTime - startTime);
// draw the range search results for kd-tree in blue
StdDraw.setPenRadius(.02);
StdDraw.setPenColor(StdDraw.BLUE);
startTime = System.nanoTime();
for (Point2D p : kdtree.range(rect))
p.draw();
endTime = System.nanoTime();
System.out.printf("Time Taken by KDTree %d \n", endTime - startTime);
StdDraw.show(40); */
}
public static void buildTree(ArrayList<Point2D> points, boolean xysort) {
//if xy sort = 1, sort by x
//otherwise sort by y
ArrayList<Point2D> sortedPoints;
if(points.size() > 2) {
if(xysort) {
sortedPoints = sortMeByX(points);
xysort = false;
}else {
sortedPoints = sortMeByY(points);
xysort = true;
}
int size = sortedPoints.size();
int median = size/2;
kdtree.insert(sortedPoints.get(median));
//kdtree.draw();
//StdDraw.show();
ArrayList<Point2D> right = new ArrayList<Point2D>(sortedPoints.subList(median+1,size));
ArrayList<Point2D> left = new ArrayList<Point2D>(sortedPoints.subList(0,median));
buildTree(right,xysort);
buildTree(left,xysort);
}else{
for(Point2D p : points ) {
kdtree.insert(p);
//kdtree.draw();
//StdDraw.show();
}
}
}
public static ArrayList<Point2D> sortMeByX(ArrayList<Point2D> points){
int n = points.size();
int c, d;
Point2D swap;
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (points.get(d).x() > points.get(d+1).x()) {
swap = points.get(d);
points.set(d, points.get(d+1));
points.set((d+1), swap);
}
}
}
return points;
}
public static ArrayList<Point2D> sortMeByY(ArrayList<Point2D> points){
int n = points.size();
int c, d;
Point2D swap;
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (points.get(d).y() > points.get(d+1).y()) {
swap = points.get(d);
points.set(d, points.get(d+1));
points.set((d+1), swap);
}
}
}
return points;
}
}