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 /experimental/tectonics/world.c | |
parent | 008cf63a05d7be6ed165747ec5e735e002de3b2d (diff) |
add quadtree freeing
Diffstat (limited to 'experimental/tectonics/world.c')
-rw-r--r-- | experimental/tectonics/world.c | 26 |
1 files changed, 10 insertions, 16 deletions
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); } |