-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKdTreeVisualizer.java
200 lines (159 loc) · 5.79 KB
/
KdTreeVisualizer.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
/*************************************************************************
* Compilation: javac KdTreeVisualizer.java
* Execution: java KdTreeVisualizer
* Dependencies: StdDraw.java Point2D.java KdTreeST.java
*
* Add the points that the user clicks in the standard draw window
* to a kd-tree and draw the resulting kd-tree.
*
*************************************************************************/
import java.util.ArrayList;
public class KdTreeVisualizer {
private static ArrayList<Point2D> points;
private static KdTree kdtree;
private static PointSET brute;
public static void main(String[] args) {
boolean treeBuilt = true;
boolean isDragging = false;
boolean paintMe = false;
boolean done = false;
long startTime, endTime;
double x0 = 0.0;
double x1 = 0.0;
double y0 = 0.0;
double y1 = 0.0;
points = new ArrayList<Point2D>();
kdtree = new KdTree();
brute = new PointSET();
while (treeBuilt) {
StdDraw.setPenRadius(.005);
if (StdDraw.mousePressed()) {
double x = StdDraw.mouseX();
double y = StdDraw.mouseY();
System.out.printf("%8.6f %8.6f\n", x, y);
Point2D p = new Point2D(x, y);
p.draw();
points.add(p);
brute.insert(p);
//kdtree.insert(p);
//StdDraw.clear();
//kdtree.draw();
}
if (StdDraw.hasNextKeyTyped()) {
buildTree(points,true, 100);
treeBuilt = false;
}
StdDraw.show(50);
}
while (true) {
StdDraw.show(40);
// user starts to drag a rectangle
if (StdDraw.mousePressed() && !isDragging) {
x0 = StdDraw.mouseX();
y0 = StdDraw.mouseY();
isDragging = true;
paintMe = false;
//continue;
}
// user is dragging a rectangle
else if (StdDraw.mousePressed() && isDragging) {
x1 = StdDraw.mouseX();
y1 = StdDraw.mouseY();
paintMe = true;
//continue;
}
// mouse no longer pressed
else if (!StdDraw.mousePressed() && isDragging) {
isDragging = false;
paintMe = false;
done = true;
}
RectHV rect = new RectHV(Math.min(x0, x1), Math.min(y0, y1),
Math.max(x0, x1), Math.max(y0, y1));
if((x0-x1)*(y0-y1) != 0 && paintMe){
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.clear();
rect.draw();
kdtree.draw();
//buildTree(points,true, 0);
StdDraw.setPenColor(StdDraw.BLUE);
for (Point2D p : kdtree.range(rect))
p.draw();
}
if((done) && !(isDragging) && (x0-x1)*(y0-y1) != 0 ){
//////
startTime = System.nanoTime();
Iterable<Point2D> bruteRange = brute.range(rect);
endTime = System.nanoTime();
///////
System.out.printf("Time Taken by Brute Force %d \n", endTime - startTime);
/////////
startTime = System.nanoTime();
Iterable<Point2D> kdRange = kdtree.range(rect);
endTime = System.nanoTime();
/////////
System.out.printf("Time Taken by KDTree %d \n", endTime - startTime);
done = false;
}
}
}
public static void buildTree(ArrayList<Point2D> points, boolean xysort, int t) {
//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(t);
ArrayList<Point2D> right = new ArrayList<Point2D>(sortedPoints.subList(median+1,size));
ArrayList<Point2D> left = new ArrayList<Point2D>(sortedPoints.subList(0,median));
buildTree(right,xysort,t);
buildTree(left,xysort,t);
}else{
for(Point2D p : points ) {
kdtree.insert(p);
kdtree.draw();
StdDraw.show(t);
}
}
}
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;
}
}