-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDotTreeGUI.java
More file actions
212 lines (197 loc) · 7.07 KB
/
DotTreeGUI.java
File metadata and controls
212 lines (197 loc) · 7.07 KB
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
import java.awt.*;
import java.util.List;
import javax.swing.*;
/**
* Driver for interacting with a quadtree:
* inserting points, viewing the tree, and finding points near a mouse press
*
* @author Chris Bailey-Kellogg, Dartmouth CS 10, Spring 2015
* @author CBK, Spring 2016, updated for dots
* @author CBK, Fall 2016, generics, dots, extended testing
*/
public class DotTreeGUI extends DrawingGUI {
private static final int width=800, height=600; // size of the universe
private static final int dotRadius = 5; // to draw dot, so it's visible
private static final Color[] rainbow = {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.MAGENTA};
// to color different levels differently
private PointQuadtree<Dot> tree = null; // holds the dots
private char mode = 'a'; // 'a': adding; 'q': querying with the mouse
private int mouseX, mouseY; // current mouse location, when querying
private int mouseRadius = 10; // circle around mouse location, for querying
private boolean trackMouse = false; // if true, then print out where the mouse is as it moves
private List<Dot> found = null; // who was found near mouse, when querying
public DotTreeGUI() {
super("dottree", width, height);
}
/**
* DrawingGUI method, here keeping track of the location and redrawing to show it
*/
@Override
public void handleMouseMotion(int x, int y) {
if (mode == 'q') {
mouseX = x; mouseY = y;
repaint();
}
if (trackMouse) {
System.out.println(x+","+y);
}
}
/**
* DrawingGUI method, here either adding a new point or querying near the mouse
*/
@Override
public void handleMousePress(int x, int y) {
if (mode == 'a') {
// Add a new dot at the point
// TODO: YOUR CODE HERE
}
else if (mode == 'q') {
// Set "found" to what tree says is near the mouse press
// TODO: YOUR CODE HERE
}
else {
System.out.println("clicked at "+x+","+y);
}
repaint();
}
/**
* A simple testing procedure, making sure actual is expected, and printing a message if not
* @param x query x coordinate
* @param y query y coordinate
* @param r query circle radius
* @param expectedCircleRectangle how many times Geometry.circleIntersectsRectangle is expected to be called
* @param expectedInCircle how many times Geometry.pointInCircle is expected to be called
* @param expectedHits how many points are expected to be found
* @return 0 if passed; 1 if failed
*/
private int testFind(int x, int y, int r, int expectedCircleRectangle, int expectedInCircle, int expectedHits) {
Geometry.resetNumInCircleTests();
Geometry.resetNumCircleRectangleTests();
int errs = 0;
int num = tree.findInCircle(x, y, r).size();
String which = "("+x+","+y+")@"+r;
if (Geometry.getNumCircleRectangleTests() != expectedCircleRectangle) {
errs++;
System.err.println(which+": wrong # circle-rectangle, got "+Geometry.getNumCircleRectangleTests()+" but expected "+expectedCircleRectangle);
}
if (Geometry.getNumInCircleTests() != expectedInCircle) {
errs++;
System.err.println(which+": wrong # in circle, got "+Geometry.getNumInCircleTests()+" but expected "+expectedInCircle);
}
if (num != expectedHits) {
errs++;
System.err.println(which+": wrong # hits, got "+num+" but expected "+expectedHits);
}
return errs;
}
/**
* test tree 0 -- first three points from figure in handout
* hardcoded point locations for 800x600
*/
private void test0() {
found = null;
tree = new PointQuadtree<Dot>(new Dot(400,300), 0,0,800,600); // start with A
tree.insert(new Dot(150,450)); // B
tree.insert(new Dot(250,550)); // C
int bad = 0;
bad += testFind(0,0,900,3,3,3); // rect for all; circle for all; find all
bad += testFind(400,300,10,3,2,1); // rect for all; circle for A,B; find A
bad += testFind(150,450,10,3,3,1); // rect for all; circle for all; find B
bad += testFind(250,550,10,3,3,1); // rect for all; circle for all; find C
bad += testFind(150,450,200,3,3,2); // rect for all; circle for all; find B, C
bad += testFind(140,440,10,3,2,0); // rect for all; circle for A,B; find none
bad += testFind(750,550,10,2,1,0); // rect for A,B; circle for A; find none
if (bad==0) System.out.println("test 0 passed!");
}
/**
* test tree 1 -- figure in handout
* hardcoded point locations for 800x600
*/
private void test1() {
found = null;
tree = new PointQuadtree<Dot>(new Dot(300,400), 0,0,800,600); // start with A
tree.insert(new Dot(150,450)); // B
tree.insert(new Dot(250,550)); // C
tree.insert(new Dot(450,200)); // D
tree.insert(new Dot(200,250)); // E
tree.insert(new Dot(350,175)); // F
tree.insert(new Dot(500,125)); // G
tree.insert(new Dot(475,250)); // H
tree.insert(new Dot(525,225)); // I
tree.insert(new Dot(490,215)); // J
tree.insert(new Dot(700,550)); // K
tree.insert(new Dot(310,410)); // L
int bad = 0;
bad += testFind(150,450,10,6,3,1); // rect for A [D] [E] [B [C]] [K]; circle for A, B, C; find B
bad += testFind(500,125,10,8,3,1); // rect for A [D [G F H]] [E] [B] [K]; circle for A, D, G; find G
bad += testFind(300,400,15,10,6,2); // rect for A [D [G F H]] [E] [B [C]] [K [L]]; circle for A,D,E,B,K,L; find A,L
bad += testFind(495,225,50,10,6,3); // rect for A [D [G F H [I [J]]]] [E] [B] [K]; circle for A,D,G,H,I,J; find H,I,J
bad += testFind(0,0,900,12,12,12); // rect for all; circle for all; find all
if (bad==0) System.out.println("test 1 passed!");
}
/**
* DrawingGUI method, here toggling the mode between 'a' and 'q'
* and increasing/decresing mouseRadius via +/-
*/
@Override
public void handleKeyPress(char key) {
if (key=='a' || key=='q') mode = key;
else if (key=='+') {
mouseRadius += 10;
}
else if (key=='-') {
mouseRadius -= 10;
if (mouseRadius < 0) mouseRadius=0;
}
else if (key=='m') {
trackMouse = !trackMouse;
}
else if (key=='0') {
test0();
}
else if (key=='1') {
test1();
}
// TODO: YOUR CODE HERE -- your test cases
repaint();
}
/**
* DrawingGUI method, here drawing the quadtree
* and if in query mode, the mouse location and any found dots
*/
@Override
public void draw(Graphics g) {
if (tree != null) drawTree(g, tree, 0);
if (mode == 'q') {
g.setColor(Color.BLACK);
g.drawOval(mouseX-mouseRadius, mouseY-mouseRadius, 2*mouseRadius, 2*mouseRadius);
if (found != null) {
g.setColor(Color.BLACK);
for (Dot d : found) {
g.fillOval((int)d.getX()-dotRadius, (int)d.getY()-dotRadius, 2*dotRadius, 2*dotRadius);
}
}
}
}
/**
* Draws the dot tree
* @param g the graphics object for drawing
* @param tree a dot tree (not necessarily root)
* @param level how far down from the root qt is (0 for root, 1 for its children, etc.)
*/
public void drawTree(Graphics g, PointQuadtree<Dot> tree, int level) {
// Set the color for this level
g.setColor(rainbow[level % rainbow.length]);
// Draw this node's dot and lines through it
// TODO: YOUR CODE HERE
// Recurse with children
// TODO: YOUR CODE HERE
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new DotTreeGUI();
}
});
}
}