private void checkBestDistance(KdTree.Node node, FastQueue<KdTreeResult> neighbors) {
		double distSq = distance.distance((P)node.point,target);
		// <= because multiple nodes could be at the bestDistanceSq
		if( distSq <= mostDistantNeighborSq) {
			if( neighbors.size() < searchN ) {
				// the list of nearest neighbors isn't full yet so it doesn't know what the distance will be
				// so just keep on adding them to the list until it is full
				KdTreeResult r = neighbors.grow();
				r.distance = distSq;
				r.node = node;
				if( neighbors.size() == searchN ) {
					// find the most distant
					mostDistantNeighborSq = -1;
					for( int i = 0; i < searchN; i++ ) {
						r = neighbors.get(i);
						if( r.distance > mostDistantNeighborSq ) {
							mostDistantNeighborSq = r.distance;
							mostDistantNeighborIndex = i;
						}
					}
				}
			} else {
				for( int i = 0; i < searchN; i++ ) {
					KdTreeResult r = neighbors.get(i);
					if( r.distance > mostDistantNeighborSq ) {
						throw new RuntimeException("Most distant isn't the most distant");
					}
				}

				// Write over the most distant neighbor since we known this node must be closer
				// and update the maximum distance
				KdTreeResult r = neighbors.get(mostDistantNeighborIndex);
				r.node = node;
				r.distance = distSq;

				// If there are multiple points then there can be more than one point with the value of
				// 'bestDistanceSq', which is why two searches are required
				mostDistantNeighborSq = -1;
				for( int i = 0; i < searchN; i++ ) {
					r = neighbors.get(i);
					if( r.distance > mostDistantNeighborSq ) {
						mostDistantNeighborSq = r.distance;
						mostDistantNeighborIndex = i;
					}
				}
			}
		}
	}
private void checkBestDistance ( KdTree . Node node , FastQueue < KdTreeResult > neighbors ) { double distSq = distance . distance ( ( P ) node . point , target ) ; if ( distSq <= mostDistantNeighborSq ) { if ( neighbors . size ( ) < searchN ) { KdTreeResult r = neighbors . grow ( ) ; r . distance = distSq ; r . node = node ; if ( neighbors . size ( ) == searchN ) { mostDistantNeighborSq = - 1 ; for ( int i = 0 ; i < searchN ; i ++ ) { r = neighbors . get ( i ) ; if ( r . distance > mostDistantNeighborSq ) { mostDistantNeighborSq = r . distance ; mostDistantNeighborIndex = i ; } } } } else { for ( int i = 0 ; i < searchN ; i ++ ) { KdTreeResult r = neighbors . get ( i ) ; if ( r . distance > mostDistantNeighborSq ) { throw new RuntimeException ( "Most distant isn't the most distant" ) ; } } KdTreeResult r = neighbors . get ( mostDistantNeighborIndex ) ; r . node = node ; r . distance = distSq ; mostDistantNeighborSq = - 1 ; for ( int i = 0 ; i < searchN ; i ++ ) { r = neighbors . get ( i ) ; if ( r . distance > mostDistantNeighborSq ) { mostDistantNeighborSq = r . distance ; mostDistantNeighborIndex = i ; } } } } }
See if the node being considered is a new nearest-neighbor