struct vector *corner)
{
struct vector tmp;
- struct quadtree *nearest = tree, *node;
+ struct quadtree *nearest, *node;
double distance, dist;
int ret, i, directions;
return NULL;
}
- vector_sub(pos, &tree->pos, &tmp);
- distance = vector_abs(&tmp);
+ if (is_within(&tree->pos, corner)) {
+ vector_sub(pos, &tree->pos, &tmp);
+ distance = vector_abs(&tmp);
+ nearest = tree;
+ } else {
+ nearest = NULL;
+ }
directions = quadrants_to_search(tree, corner);
printf("%p: (%f %f)dist: %f, distance: %f\n",
node, node->pos.x, node->pos.y,
dist, distance);
- if (dist < distance) {
+ if (!nearest || dist < distance) {
nearest = node;
distance = dist;
}
}
- if (!is_within(&nearest->pos, corner)) {
+ if (nearest && !is_within(&nearest->pos, corner)) {
if (debug)
printf("Node %p (%f %f) is not within "
"search window (%f %f) (%f %f)\n",
corner[1].x, corner[1].y);
return NULL;
}
+ printf("%d: Returning %p\n",__LINE__, nearest);
return nearest;
}
nearest = quadtree_find_nearest(tree, pos, corner);
if (nearest != tree)
- return nearest;
+ goto out;
if (debug)
printf("Avoiding parent or NULL node %p\n", nearest);
printf("Re-setting nearest from %d\n", i);
}
+out:
+ printf("%d: Returning %p\n", __LINE__, nearest);
return nearest;
}