summaryrefslogtreecommitdiff
path: root/experimental/tectonics/world.c
diff options
context:
space:
mode:
Diffstat (limited to 'experimental/tectonics/world.c')
-rw-r--r--experimental/tectonics/world.c26
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);
}