// a nearest neighbor in the set to point p; null if the set is empty
 public Point2D nearest(Point2D p) {
   if (p == null) throw new NullPointerException("point");
   Point2D nearestPoint = null;
   double nearestDist = MAX_DISTANCE;
   for (Point2D currPoint : points) {
     double currDist = currPoint.distanceTo(p);
     if (currDist < nearestDist) {
       nearestDist = currDist;
       nearestPoint = currPoint;
     }
   }
   return nearestPoint;
 }
Example #2
0
    public Point2D nearest(Point2D p, Point2D nearest) {

      if (slightlyEquals(p, nearest)) return nearest;
      double minDistance = p.distanceSquaredTo(nearest);
      if (left != null) {
        final double distanceTo = p.distanceTo(left.value);
        if (distanceTo <= minDistance) {
          minDistance = distanceTo;
          nearest = left.value;
        }
      }
      if (right != null) {
        final double distanceTo = p.distanceTo(right.value);
        if (distanceTo <= minDistance) {
          minDistance = distanceTo;
          nearest = right.value;
        }
      }
      Point2D lnearest = nearest;
      if (left != null && left.rect.distanceTo(nearest) < minDistance) {
        lnearest = left.nearest(p, nearest);
      }
      Point2D rnearest = nearest;
      if (right != null && right.rect.distanceTo(nearest) < minDistance) {
        rnearest = right.nearest(p, nearest);
      }

      final double ldist = p.distanceTo(lnearest);
      final double rdist = p.distanceTo(rnearest);
      if (ldist < minDistance) {
        nearest = lnearest;
        minDistance = ldist;
      }
      if (rdist < minDistance) {
        nearest = rnearest;
        minDistance = rdist;
      }
      return nearest;
    }
Example #3
0
  private void nearest(
      final Node node, final Holder<Point2D> nearest, final Point2D p, final boolean vertical) {

    if (node == null) {
      return;
    } else if (node.key.compareTo(p) == 0) {
      nearest.value = node.key;
      return;
    } else if (p.distanceTo(node.key) < p.distanceTo(nearest.value)) {
      nearest.value = node.key;
    }

    if (onTheLeft(p, node.key, vertical)) {
      nearest(node.left, nearest, p, !vertical);
      if (distanceToVertical(p, node.key, vertical) < p.distanceTo(nearest.value)) {
        nearest(node.right, nearest, p, !vertical);
      }
    } else if (onTheRight(p, node.key, vertical)) {
      nearest(node.right, nearest, p, !vertical);
      if (distanceToVertical(p, node.key, vertical) < p.distanceTo(nearest.value)) {
        nearest(node.left, nearest, p, !vertical);
      }
    }
  }
Example #4
0
  private Point2D find(KdNode nd, double curDist, Point2D target, int turn) {
    Point2D curClosest = null;

    if (nd == null) return curClosest;

    // Pruning
    if (nd.rect.distanceTo(target) > curDist) {
      return null;
    }

    if (nd.point.distanceTo(target) < curDist) {
      curClosest = nd.point;
      curDist = nd.point.distanceTo(target);
    }

    // Always search the plane that target is on according to
    // current node
    if ((turn == 0 && target.x() < nd.point.x()) || (turn == 1 && target.y() < nd.point.y())) {
      Point2D left = find(nd.left, curDist, target, 1 - turn);
      if (left != null && left.distanceTo(target) < curDist) {
        curClosest = left;
        curDist = left.distanceTo(target);
      }
      Point2D right = find(nd.right, curDist, target, 1 - turn);
      if (right != null && right.distanceTo(target) < curDist) {
        curClosest = right;
        curDist = right.distanceTo(target);
      }
    } else {
      Point2D right = find(nd.right, curDist, target, 1 - turn);
      if (right != null && right.distanceTo(target) < curDist) {
        curClosest = right;
        curDist = right.distanceTo(target);
      }
      Point2D left = find(nd.left, curDist, target, 1 - turn);
      if (left != null && left.distanceTo(target) < curDist) {
        curClosest = left;
        curDist = left.distanceTo(target);
      }
    }
    return curClosest;
  }
Example #5
0
 private double distanceTo(Point2D p1, Point2D p2) {
   if (p2 == null) return Double.MAX_VALUE;
   return p1.distanceTo(p2);
 }