/** Creates a new instance. */ public QuadTree() { root = new QuadNode(new Rectangle2D.Double(0, 0, 800, 600)); } public QuadTree(Rectangle2D.Double bounds) {
public void reorganize() { root.join(); outside.putAll(root.objects); root.objects.clear(); Iterator<Map.Entry<T,Rectangle2D.Double>> i = outside.entrySet().iterator(); Map.Entry<T,Rectangle2D.Double> entry = i.next(); Rectangle2D.Double treeBounds = (Rectangle2D.Double) (entry.getValue()).clone(); while (i.hasNext()) { entry = i.next(); Rectangle2D.Double bounds = entry.getValue(); treeBounds.add(bounds); } root.bounds = treeBounds; i = outside.entrySet().iterator(); while (i.hasNext()) { entry = i.next(); root.add(entry.getKey(), entry.getValue()); } outside.clear(); } public void remove(T o) {
public Collection<T> findContains(Point2D.Double p) { HashSet<T> result = new HashSet<T>(); root.findContains(p, result); for (Map.Entry<T,Rectangle2D.Double> entry : outside.entrySet()) { if (entry.getValue().contains(p)) { result.add(entry.getKey()); } } return result; } public Collection<T> findIntersects(Rectangle2D r) {
public QuadTree(Rectangle2D.Double bounds) { root = new QuadNode(bounds); }
public void remove(T o) { outside.remove(o); root.remove(o); } public Collection<T> findContains(Point2D.Double p) {
public Collection<T> findInside(Rectangle2D.Double r) { HashSet<T> result = new HashSet<T>(); root.findInside(r, result); for (Map.Entry<T,Rectangle2D.Double> entry : outside.entrySet()) { if (r.contains(entry.getValue())) { result.add(entry.getKey()); } } return result; }
public Collection<T> findIntersects(Rectangle2D.Double r) { HashSet<T> result = new HashSet<T>(); root.findIntersects(r, result); for (Map.Entry<T,Rectangle2D.Double> entry : outside.entrySet()) { if (entry.getValue().intersects(r)) { result.add(entry.getKey()); } } return result; } public Collection<T> findInside(Rectangle2D.Double r) {
public void add(T o, Rectangle2D.Double bounds) { if (root.bounds.contains(bounds)) { root.add(o, (Rectangle2D.Double) bounds.clone()); } else { outside.put(o, (Rectangle2D.Double) bounds.clone()); if (outside.size() > maxOutside) { reorganize(); } } } public void reorganize() {