summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore4
-rw-r--r--experimental/tectonics/quadtree.c13
-rw-r--r--experimental/tectonics/tectonics.h1
-rw-r--r--experimental/tectonics/world.c26
4 files changed, 26 insertions, 18 deletions
diff --git a/.gitignore b/.gitignore
index 54d550c..5b38b15 100644
--- a/.gitignore
+++ b/.gitignore
@@ -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);
}