////////////////////////////////////////////////////NEED ADJUSTMENT IN DISTANCE MEASURE /// <summary> /// Recursively searches the tree for the nearest entry. Other queries /// call execute() on an IntProcedure when a matching entry is found; /// however nearest() must store the entry Ids as it searches the tree, /// in case a nearer entry is found. /// Uses the member variable nearestIds to store the nearest /// entry IDs. /// </summary> /// <remarks>TODO rewrite this to be non-recursive?</remarks> private double nearest(SubSequence p, Node <T> n, double nearestDistance) { for (int i = 0; i < n.entryCount; i++) { double tempDistance = n.entries[i].distance(p); if (n.isLeaf()) { // for leaves, the distance is an actual nearest distance if (tempDistance < nearestDistance) { nearestDistance = tempDistance; nearestIds.Clear(); } if (tempDistance <= nearestDistance) { nearestIds.Add(n.ids[i]); } } else { // for index nodes, only go into them if they potentially could have // a rectangle nearer than actualNearest if (tempDistance <= nearestDistance) { // search the child node nearestDistance = nearest(p, getNode(n.ids[i]), nearestDistance); } } } return(nearestDistance); }
/// <summary> /// Retrieve nearest items to a point in radius furthestDistance /// </summary> public List <T> Nearest(SubSequence p, double furthestDistance) { List <T> retval = new List <T>(); nearest(p, delegate(int id) { retval.Add(IdsToItems[id]); }, furthestDistance); return(retval); }
private void nearest(SubSequence p, intproc v, double furthestDistance) { Node <T> rootNode = getNode(rootNodeId); nearest(p, rootNode, furthestDistance); foreach (int id in nearestIds) { v(id); } nearestIds.Clear(); }
/** * Return the distance between this rectangle and the passed point. * If the rectangle contains the point, the distance is zero. * * @param p Point to find the distance to * * @return distance beween this rectangle and the passed point. */ internal double distance(SubSequence p) { double distanceSquared = 0; for (int i = 0; i < DIMENSIONS; i++) { double greatestMin = Math.Max(min[i], p.coordinates[i]); double leastMax = Math.Min(max[i], p.coordinates[i]); if (greatestMin > leastMax) { distanceSquared += ((greatestMin - leastMax) * (greatestMin - leastMax)); } } return((double)Math.Sqrt(distanceSquared)); }