2
2
3
3
import java .util .ArrayList ;
4
4
import java .util .Comparator ;
5
+ import java .util .HashSet ;
5
6
import java .util .LinkedList ;
6
7
import java .util .List ;
8
+ import java .util .Set ;
7
9
8
10
/**
9
11
* A quadtree is a tree data structure in which each internal node has exactly four children. Quadtrees
@@ -57,7 +59,7 @@ public String toString() {
57
59
58
60
private static class QuadNode implements Comparable <QuadNode > {
59
61
60
- private List <XYPoint > points = new ArrayList <XYPoint >(capacity );
62
+ private Set <XYPoint > points = new HashSet <XYPoint >(capacity );
61
63
private AxisAlignedBoundingBox aabb = null ;
62
64
private QuadNode northWest = null ;
63
65
private QuadNode northEast = null ;
@@ -70,8 +72,7 @@ private QuadNode(AxisAlignedBoundingBox aabb) {
70
72
71
73
private boolean insert (XYPoint p ) {
72
74
// Ignore objects which do not belong in this quad tree
73
- if (!aabb .containsPoint (p ))
74
- return false ; // object cannot be added
75
+ if (!aabb .containsPoint (p ) || points .contains (p )) return false ; // object cannot be added
75
76
76
77
// If there is space in this quad tree, add the object here
77
78
if (points .size () < capacity ) {
@@ -117,8 +118,7 @@ private void queryRange(AxisAlignedBoundingBox range, List<XYPoint> pointsInRang
117
118
if (!aabb .intersectsBox (range )) return ;
118
119
119
120
// Check objects at this quad level
120
- for (int p = 0 ; p < points .size (); p ++) {
121
- XYPoint xyPoint = points .get (p );
121
+ for (XYPoint xyPoint : points ) {
122
122
if (range .containsPoint (xyPoint )) pointsInRange .add (xyPoint );
123
123
}
124
124
@@ -132,6 +132,19 @@ private void queryRange(AxisAlignedBoundingBox range, List<XYPoint> pointsInRang
132
132
southEast .queryRange (range ,pointsInRange );
133
133
}
134
134
135
+ /**
136
+ * {@inheritDoc}
137
+ */
138
+ @ Override
139
+ public int hashCode () {
140
+ int hash = aabb .hashCode ();
141
+ hash = hash * 13 + ((northWest !=null )?northWest .hashCode ():1 );
142
+ hash = hash * 17 + ((northEast !=null )?northEast .hashCode ():1 );
143
+ hash = hash * 19 + ((southWest !=null )?southWest .hashCode ():1 );
144
+ hash = hash * 23 + ((southEast !=null )?southEast .hashCode ():1 );
145
+ return hash ;
146
+ }
147
+
135
148
/**
136
149
* {@inheritDoc}
137
150
*/
@@ -195,9 +208,9 @@ public AxisAlignedBoundingBox(XYPoint upperLeft, double height, double width) {
195
208
}
196
209
197
210
public boolean containsPoint (XYPoint p ) {
198
- if (p .x >maxX ) return false ;
211
+ if (p .x >= maxX ) return false ;
199
212
if (p .x <minX ) return false ;
200
- if (p .y >maxY ) return false ;
213
+ if (p .y >= maxY ) return false ;
201
214
if (p .y <minY ) return false ;
202
215
return true ;
203
216
}
@@ -216,6 +229,17 @@ public boolean intersectsBox(AxisAlignedBoundingBox aabb) {
216
229
return true ;
217
230
}
218
231
232
+ /**
233
+ * {@inheritDoc}
234
+ */
235
+ @ Override
236
+ public int hashCode () {
237
+ int hash = upperLeft .hashCode ();
238
+ hash = hash * 13 + (int )height ;
239
+ hash = hash * 19 + (int )width ;
240
+ return hash ;
241
+ }
242
+
219
243
/**
220
244
* {@inheritDoc}
221
245
*/
@@ -272,6 +296,17 @@ public XYPoint(double x, double y) {
272
296
this .y = y ;
273
297
}
274
298
299
+ /**
300
+ * {@inheritDoc}
301
+ */
302
+ @ Override
303
+ public int hashCode () {
304
+ int hash = 1 ;
305
+ hash = hash * 13 + (int )x ;
306
+ hash = hash * 19 + (int )y ;
307
+ return hash ;
308
+ }
309
+
275
310
/**
276
311
* {@inheritDoc}
277
312
*/
0 commit comments