diff --git a/src/quad.c b/src/quad.c index 13280f1..e4ac4cf 100644 --- a/src/quad.c +++ b/src/quad.c @@ -4,7 +4,7 @@ #include #include #include -#include +#include // memcpy #include #include // DBL_MAX #include // size_t @@ -229,11 +229,19 @@ double qtree_nearest_neighbor(quadtree_t* qtree, point_t* query, point_t* neares static void node_remove_point(node_t* node, point_t* point) { if (node_is_leaf(node)) { for (int i = 0; i < node->count; ++i) { - if (node->points[i].id == point->id) { + if (node->points[i].id == point->id && node->count > 1) { +#if 1 // shift last point into deleted point - node->points[i] = node->points[--node->count]; + memcpy(&node->points[i], &node->points[--node->count], sizeof(node->points[0])); +#else + memcpy(&node->points[i], &node->points[node->count], sizeof(node->points[0])); +#endif + return; + } else if (node->points[i].id == point->id && node->count == 1) { + node->count--; return; } + } } else { // search top-down diff --git a/test/test.c b/test/test.c index fadcd84..f8d7d86 100644 --- a/test/test.c +++ b/test/test.c @@ -72,5 +72,6 @@ int main(int argc, char** argv) { NTEST_ASSERT(qtree.root->nw->se->sw->count == 1); qtree_remove_point(&qtree, &points[5]); NTEST_ASSERT(qtree.root->nw->se->sw->count == 0); + //qtree_del(&qtree); return 0; }