From 3c0ee746d9d2822c1bf04e76267c14785b4d54df Mon Sep 17 00:00:00 2001
From: sanine <sanine.not@pm.me>
Date: Tue, 25 Jan 2022 23:07:41 -0600
Subject: add quadtree freeing

---
 experimental/tectonics/world.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)

(limited to 'experimental/tectonics/world.c')

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);
 }
 
-- 
cgit v1.2.1