示例#1
0
 // a nearest neighbor in the set to point p; null if the set is empty
 public Point2D nearest(Point2D p) {
   if (null == p) {
     throw new java.lang.NullPointerException();
   }
   if (this.isEmpty()) {
     return null;
   }
   Point2D res = null;
   double dis = Double.MAX_VALUE;
   LinkedList<Node> q = new LinkedList<Node>();
   q.add(root);
   while (!q.isEmpty()) {
     Node tp = q.pollFirst();
     double cur = p.distanceSquaredTo(tp.getP());
     if (cur < dis) {
       dis = cur;
       res = tp.getP();
     }
     Node lch = tp.getLb();
     Node rch = tp.getRt();
     if (null != lch && lch.getRect().distanceSquaredTo(p) < dis) {
       q.add(lch);
     }
     if (null != rch && rch.getRect().distanceSquaredTo(p) < dis) {
       q.add(rch);
     }
   }
   return res;
 }
示例#2
0
 // all points that are inside the rectangle
 public Iterable<Point2D> range(RectHV rect) {
   LinkedList<Point2D> res = new LinkedList<Point2D>();
   if (null == this.root) {
     return res;
   }
   LinkedList<Node> q = new LinkedList<Node>();
   q.add(this.root);
   while (!q.isEmpty()) {
     Node tp = q.pollFirst();
     if (rect.contains(tp.getP())) {
       res.add(tp.getP());
     }
     Node lch = tp.getLb();
     if (null != lch && rect.intersects(lch.getRect())) {
       q.add(lch);
     }
     Node rch = tp.getRt();
     if (null != rch && rect.intersects(rch.getRect())) {
       q.add(rch);
     }
   }
   return res;
 }