Skip to content

Commit cb25164

Browse files
author
phishman3579
committed
Caught a bug with multiple points
git-svn-id: https://java-algorithms-implementation.googlecode.com/svn/trunk@388 032fbc0f-8cab-eb90-e552-f08422b9a96a
1 parent 9b378ee commit cb25164

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

src/com/jwetherell/algorithms/data_structures/QuadTree.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import java.util.ArrayList;
44
import java.util.Comparator;
5+
import java.util.HashSet;
56
import java.util.LinkedList;
67
import java.util.List;
8+
import java.util.Set;
79

810
/**
911
* A quadtree is a tree data structure in which each internal node has exactly four children. Quadtrees
@@ -57,7 +59,7 @@ public String toString() {
5759

5860
private static class QuadNode implements Comparable<QuadNode> {
5961

60-
private List<XYPoint> points = new ArrayList<XYPoint>(capacity);
62+
private Set<XYPoint> points = new HashSet<XYPoint>(capacity);
6163
private AxisAlignedBoundingBox aabb = null;
6264
private QuadNode northWest = null;
6365
private QuadNode northEast = null;
@@ -70,8 +72,7 @@ private QuadNode(AxisAlignedBoundingBox aabb) {
7072

7173
private boolean insert(XYPoint p) {
7274
// 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
7576

7677
// If there is space in this quad tree, add the object here
7778
if (points.size() < capacity) {
@@ -117,8 +118,7 @@ private void queryRange(AxisAlignedBoundingBox range, List<XYPoint> pointsInRang
117118
if (!aabb.intersectsBox(range)) return;
118119

119120
// 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) {
122122
if (range.containsPoint(xyPoint)) pointsInRange.add(xyPoint);
123123
}
124124

@@ -132,6 +132,19 @@ private void queryRange(AxisAlignedBoundingBox range, List<XYPoint> pointsInRang
132132
southEast.queryRange(range,pointsInRange);
133133
}
134134

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+
135148
/**
136149
* {@inheritDoc}
137150
*/
@@ -195,9 +208,9 @@ public AxisAlignedBoundingBox(XYPoint upperLeft, double height, double width) {
195208
}
196209

197210
public boolean containsPoint(XYPoint p) {
198-
if (p.x>maxX) return false;
211+
if (p.x>=maxX) return false;
199212
if (p.x<minX) return false;
200-
if (p.y>maxY) return false;
213+
if (p.y>=maxY) return false;
201214
if (p.y<minY) return false;
202215
return true;
203216
}
@@ -216,6 +229,17 @@ public boolean intersectsBox(AxisAlignedBoundingBox aabb) {
216229
return true;
217230
}
218231

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+
219243
/**
220244
* {@inheritDoc}
221245
*/
@@ -272,6 +296,17 @@ public XYPoint(double x, double y) {
272296
this.y = y;
273297
}
274298

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+
275310
/**
276311
* {@inheritDoc}
277312
*/

0 commit comments

Comments
 (0)