diff options
author | sanine <sanine.not@pm.me> | 2022-01-25 23:07:41 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-01-25 23:07:41 -0600 |
commit | 3c0ee746d9d2822c1bf04e76267c14785b4d54df (patch) | |
tree | 07df48552bdc9a6c18743d185bbe4539458465a9 | |
parent | 008cf63a05d7be6ed165747ec5e735e002de3b2d (diff) |
add quadtree freeing
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | experimental/tectonics/quadtree.c | 13 | ||||
-rw-r--r-- | experimental/tectonics/tectonics.h | 1 | ||||
-rw-r--r-- | experimental/tectonics/world.c | 26 |
4 files changed, 26 insertions, 18 deletions
@@ -1,4 +1,4 @@ *~ *.o -tests -tectonics +experimental/tectonics/tests +experimental/tectonics/tectonics diff --git a/experimental/tectonics/quadtree.c b/experimental/tectonics/quadtree.c index dead9f2..0f3f9c7 100644 --- a/experimental/tectonics/quadtree.c +++ b/experimental/tectonics/quadtree.c @@ -133,3 +133,16 @@ void draw_quadtree(cairo_t *cr, struct quadtree_node_t *node) draw_quadtree(cr, node->se); } } + + +void free_tree(struct quadtree_node_t *node) +{ + if (node->nw != NULL) { + free_tree(node->nw); + free_tree(node->ne); + free_tree(node->sw); + free_tree(node->se); + } + + free(node); +} diff --git a/experimental/tectonics/tectonics.h b/experimental/tectonics/tectonics.h index 5f39ff7..85b86fe 100644 --- a/experimental/tectonics/tectonics.h +++ b/experimental/tectonics/tectonics.h @@ -48,6 +48,7 @@ bool insert(struct quadtree_node_t *node, int get_closest(struct quadtree_node_t *root, struct point_t pt); void draw_quadtree(cairo_t *cr, struct quadtree_node_t *root); +void free_tree(struct quadtree_node_t *root); /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * diff --git a/experimental/tectonics/world.c b/experimental/tectonics/world.c index cfc5d14..cd56cdd 100644 --- a/experimental/tectonics/world.c +++ b/experimental/tectonics/world.c @@ -8,21 +8,6 @@ struct centroid_t { int updates; }; -static int closest_point(struct world_t *world, double x, double y) -{ - double dist = 1e9; - int closest = -1; - for (int i=0; i<world->n_points; i++) { - struct point_t *pt = world->points + i; - double d = distance(x, y, pt->x, pt->y); - if (d <= dist) { - dist = d; - closest = i; - } - } - return closest; -} - static void relax_points(struct world_t *world, int iterations) { struct centroid_t *centroid = @@ -40,7 +25,8 @@ static void relax_points(struct world_t *world, int iterations) printf("%02f%%\n", 100*((double)i+1)/iterations); double x = rand01(); double y = rand01(); - int closest = closest_point(world, x, y); + int closest = get_closest(&(world->tree), + (struct point_t){x, y}); int t = centroid[closest].updates; centroid[closest].x = (t*centroid[closest].x + x)/(t+1); @@ -54,6 +40,8 @@ static void relax_points(struct world_t *world, int iterations) pt->x = c->x; pt->y = c->y; } + + free(centroid); } void create_world(struct world_t *world, int n_points) @@ -82,6 +70,12 @@ void create_world(struct world_t *world, int n_points) void free_world(struct world_t *world) { + if (world->tree.nw != NULL) { + free_tree(world->tree.nw); + free_tree(world->tree.ne); + free_tree(world->tree.sw); + free_tree(world->tree.se); + } free(world->points); } |