samedi 14 mars 2015

Nearest neighbor search



//returns a possible best nearest point
void KDTree::search_KD(KDNode *r, XY point){

KDNode *currentNode;
currentNode = r;

//if the current location is better than the best known location
if (distance(currentNode, point) < bestDistance){
bestDistance = distance(currentNode, point);
//update the best known location
guess = currentNode;
}

//when key = 0, compare x-values
if (root->key == 0){
//recursively search the left subtree on the next axis
if (point.getX() < currentNode->coordinates.getX()){
search_KD(r->left, point);
}
else {
search_KD(r->right, point);
}
}

//when key = 1, compare y-values
if (root->key == 1){
//recursively search the left subtree on the next axis
if (point.getY() < currentNode->coordinates.getY()){
search_KD(r->left, point);
}
else {
search_KD(r->right, point);
}
}
}


I'm trying to implement a nearest neighbor search. I've been using this link as the backbone for the code http://ift.tt/10T31WY. Please refer to page 9, So what I'm having trouble with is implementing the last part: if the circle with radius abs(point - currentNode) intersects a splitting plane. If you look at the last line in the code section of page 9, it tells me "to recursively search the other subtree on the next axis", How would I go about doing that. I would appreciate if the person who answers provided advice on how to tackle this part.


Note: I declare guess and bestDistance as global variable like the document suggests.




Aucun commentaire:

Enregistrer un commentaire