diff options
Diffstat (limited to 'experimental/tectonics/world.c')
-rw-r--r-- | experimental/tectonics/world.c | 43 |
1 files changed, 38 insertions, 5 deletions
diff --git a/experimental/tectonics/world.c b/experimental/tectonics/world.c index cd56cdd..a1dd4de 100644 --- a/experimental/tectonics/world.c +++ b/experimental/tectonics/world.c @@ -8,6 +8,25 @@ struct centroid_t { int updates; }; +static void rebuild_tree(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); + world->tree.nw = NULL; + world->tree.ne = NULL; + world->tree.sw = NULL; + world->tree.se = NULL; + } + world->tree.id = -1; + + for (int i=0; i<world->n_points; i++) { + insert(&(world->tree), world->points, i); + } +} + static void relax_points(struct world_t *world, int iterations) { struct centroid_t *centroid = @@ -26,12 +45,19 @@ static void relax_points(struct world_t *world, int iterations) double x = rand01(); double y = rand01(); int closest = get_closest(&(world->tree), + world->points, (struct point_t){x, y}); - int t = centroid[closest].updates; - centroid[closest].x = (t*centroid[closest].x + x)/(t+1); - centroid[closest].y = (t*centroid[closest].y + y)/(t+1); - centroid[closest].updates += 1; + if (closest == -1) { + printf("WARN: bad closest point!\n"); + } + else { + struct centroid_t *c = centroid + closest; + int u = c->updates; + c->x = (u*c->x + x)/(u+1); + c->y = (u*c->y + y)/(u+1); + c->updates += 1; + } } for (int i=0; i<world->n_points; i++) { @@ -41,6 +67,8 @@ static void relax_points(struct world_t *world, int iterations) pt->y = c->y; } + rebuild_tree(world); + free(centroid); } @@ -63,8 +91,13 @@ void create_world(struct world_t *world, int n_points) pt->x = rand01(); pt->y = rand01(); - insert(&(world->tree), world->points, i); + //insert(&(world->tree), world->points, i); } + + rebuild_tree(world); + + for (int i=0; i<10; i++) + relax_points(world, 100000); } |