private void nearest(Node candidate, Point2D goal, RectHV subrange, Node neares) { if (candidate == null) { return; } if (candidate.p.distanceTo(goal) < neares.p.distanceTo(goal)) { neares.p = candidate.p; neares.p.GetNode = candidate.p.GetNode; } if (candidate.vertical) { RectHV one = new RectHV(subrange.Xmin, subrange.Ymin, candidate.x, subrange.Ymax); RectHV tw = new RectHV(candidate.x, subrange.Ymin, subrange.Xmax, subrange.Ymax); if (one.contains(goal)) { nearest(candidate.left, goal, one, neares); if (tw.distanceTo(goal) < neares.p.distanceTo(goal)) { nearest(candidate.right, goal, tw, neares); } } else { nearest(candidate.right, goal, tw, neares); if (one.distanceTo(goal) < neares.p.distanceTo(goal)) { nearest(candidate.left, goal, one, neares); } } } else { RectHV one = new RectHV(subrange.Xmin, subrange.Ymin, subrange.Xmax, candidate.y); RectHV tw = new RectHV(subrange.Xmin, candidate.y, subrange.Xmax, subrange.Ymax); if (one.contains(goal)) { nearest(candidate.left, goal, one, neares); if (tw.distanceTo(goal) < neares.p.distanceTo(goal)) { nearest(candidate.right, goal, tw, neares); } } else { nearest(candidate.right, goal, tw, neares); if (one.distanceTo(goal) < neares.p.distanceTo(goal)) { nearest(candidate.left, goal, one, neares); } } } }
private void rangeM(Node n, RectHV range, RectHV subrange, Queue <Point2D> q) { if (n == null) { return; } if (!range.intersects(subrange)) { return; } if (range.contains(n.p)) { q.Enqueue(n.p); } if (n.vertical) { RectHV one = new RectHV(subrange.Xmin, subrange.Ymin, n.x, subrange.Ymax); RectHV tw = new RectHV(n.x, subrange.Ymin, subrange.Xmax, subrange.Ymax); if (one.intersects(range)) { rangeM(n.left, range, one, q); } if (tw.intersects(range)) { rangeM(n.right, range, tw, q); } } else { RectHV one = new RectHV(subrange.Xmin, subrange.Ymin, subrange.Xmax, n.y); RectHV tw = new RectHV(subrange.Xmin, n.y, subrange.Xmax, subrange.Ymax); if (one.intersects(range)) { rangeM(n.left, range, one, q); } if (tw.intersects(range)) { rangeM(n.right, range, tw, q); } } }