]> git.itanic.dy.fi Git - sdl-planets/commitdiff
planet: Add tree statistics
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Sun, 11 Apr 2010 16:16:18 +0000 (19:16 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Tue, 13 Apr 2010 19:01:14 +0000 (22:01 +0300)
The total mass and area of the subtree tree is calculated.

Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
planet.c
planet.h

index 6a3e59464f2b8cfcc2ddc74146cdb5da34d8c21b..7f39cb6d187be6b4ee6aabf9a561945396cab740 100644 (file)
--- 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,
+};
+
index 0673c87bb6267ec84c39db7573835e95c18a60bc..89f232ac3d706dcc9d7f924e73f1434c40d7b5d5 100644 (file)
--- 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 {