From 06bf38a6820d8a48be810d94e81814dc30bcff63 Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Sun, 11 Apr 2010 19:16:18 +0300 Subject: [PATCH] planet: Add tree statistics The total mass and area of the subtree tree is calculated. Signed-off-by: Timo Kokkonen --- planet.c | 40 ++++++++++++++++++++++++++++++++-------- planet.h | 4 ++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/planet.c b/planet.c index 6a3e594..7f39cb6 100644 --- a/planet.c +++ b/planet.c @@ -4,9 +4,7 @@ #include "planet.h" #include "utils.h" -struct quadtree_ops planet_ops = { - .compare = planet_spatial_compare, -}; +struct quadtree_ops planet_ops; static void putpixel(struct SDL_Surface *screen, const int x, const int y, const unsigned char r, const unsigned char g, @@ -27,11 +25,7 @@ static void reshape_planet(struct planet *p) void init_planet(struct planet *p) { - p->speed.x = 0; - p->speed.y = 0; - p->pos.x = 0; - p->pos.y = 0; - p->mass = 0; + memset(p, 0, sizeof(*p)); reshape_planet(p); p->r = get_random() % 256; p->g = get_random() % 256; @@ -369,3 +363,33 @@ void planet_draw_iterator(struct quadtree *node, struct quadtree_iterator *it) draw_planet(i->screen, p, i->cam); } + +static void planet_recalculate_stats(struct quadtree *node) +{ + struct planet *p = tree_to_planet(node), *c; + struct vector vect; + double dist; + int i; + + p->tree_area = 0; + p->tree_mass = p->mass; + + for (i = 0; i < 4; i++) { + if (!node->child[i]) + continue; + + c = tree_to_planet(node->child[i]); + p->tree_mass += c->tree_mass; + + vector_sub(&p->pos, &c->pos, &vect); + dist = vector_abs(&vect); + dist += c->tree_area; + p->tree_area = MAX(p->tree_area, dist); + } +} + +struct quadtree_ops planet_ops = { + .compare = planet_spatial_compare, + .recalculate_stats = planet_recalculate_stats, +}; + diff --git a/planet.h b/planet.h index 0673c87..89f232a 100644 --- a/planet.h +++ b/planet.h @@ -17,6 +17,10 @@ struct planet { double mass; float radius; unsigned char r, g, b; + + /* info about the planets in sub branches */ + float tree_area; /* total area occupied by the tree */ + double tree_mass; /* total mass occupied by the tree */ }; struct planet_search_iterator { -- 2.44.0