#include "planet.h"
#include "utils.h"
+struct quadtree_ops planet_ops = {
+ .compare = planet_spatial_compare,
+};
+
static void putpixel(struct SDL_Surface *screen, const int x, const int y,
const unsigned char r, const unsigned char g,
const unsigned char b)
total_mass,
radius);
- quadtree_add(&p->tree, &new_planet->tree,
- planet_spatial_compare);
+ quadtree_add(&p->tree, &new_planet->tree, &planet_ops);
sum += new_planet->mass;
}
_merge_planets(a, b);
list_del(&b->list);
- quadtree_del(&b->tree, planet_spatial_compare);
+ quadtree_del(&b->tree, &planet_ops);
free(b);
return a;
{
struct quadtree *parent;
- parent = quadtree_del(node, planet_spatial_compare);
- quadtree_add(parent, node, planet_spatial_compare);
+ parent = quadtree_del(node, &planet_ops);
+ quadtree_add(parent, node, &planet_ops);
}
struct planet *move_planet(struct planet *p, const double time)
continue;
}
- tree_parent = quadtree_del(&p->tree, planet_spatial_compare);
+ tree_parent = quadtree_del(&p->tree, &planet_ops);
p->pos = new_pos;
- quadtree_add(tree_parent, &p->tree, planet_spatial_compare);
+ quadtree_add(tree_parent, &p->tree, &planet_ops);
return tree_to_planet(tree_parent);
}
* account at all.
*/
-struct quadtree *quadtree_add(struct quadtree *parent,
- struct quadtree *new,
- int (*compare)(struct quadtree *a,
- struct quadtree *b))
+struct quadtree *quadtree_add(struct quadtree *parent, struct quadtree *new,
+ struct quadtree_ops *ops)
{
int ret;
if (parent == new)
validate_tree(parent);
- ret = compare(parent, new);
+ ret = ops->compare(parent, new);
if (ret < 0 || ret >= 4) {
printf("Invalid comparison result of %d\n", ret);
}
if (parent->child[ret])
- return quadtree_add(parent->child[ret], new, compare);
+ return quadtree_add(parent->child[ret], new, ops);
parent->child[ret] = new;
new->parent = parent;
static void
_quadtree_reposition_reqursively(struct quadtree *root,
struct quadtree *node,
- int (*compare)(struct quadtree *a,
- struct quadtree *b))
+ struct quadtree_ops *ops)
{
int i;
node->child[i] == node->parent)
trap();
- _quadtree_reposition_reqursively(root, node->child[i], compare);
+ _quadtree_reposition_reqursively(root, node->child[i], ops);
node->child[i] = 0;
}
node->children = 0;
/* Then remove this node under the new root. */
- quadtree_add(root, node, compare);
+ quadtree_add(root, node, ops);
}
/*
* value will be the original root of the tree.
*/
struct quadtree *quadtree_del(struct quadtree *node,
- int (*compare)(struct quadtree *a,
- struct quadtree *b))
+ struct quadtree_ops *ops)
{
struct quadtree *parent = 0;
int i;
}
_quadtree_reposition_reqursively(parent,
node->child[i],
- compare);
+ ops);
node->child[i] = 0;
}
continue;
_quadtree_reposition_reqursively(node->parent, node->child[i],
- compare);
+ ops);
node->child[i] = 0;
}
long int depth; /* The deepest subtree branch */
};
+struct quadtree_ops {
+ int (*compare)(struct quadtree *a, struct quadtree *b);
+};
+
static inline void init_quadtree(struct quadtree *t)
{
memset(t, 0, sizeof(*t));
void (*callback)(struct quadtree *head, struct quadtree_iterator *it);
};
-struct quadtree *quadtree_add(struct quadtree *parent,
- struct quadtree *new,
- int (*compare)(struct quadtree *a,
- struct quadtree *b));
+struct quadtree *quadtree_add(struct quadtree *parent, struct quadtree *new,
+ struct quadtree_ops *ops);
-struct quadtree *quadtree_del(struct quadtree *node,
- int (*compare)(struct quadtree *a,
- struct quadtree *b));
+struct quadtree *quadtree_del(struct quadtree *node, struct quadtree_ops *ops);
int walk_quadtree(const struct quadtree_iterator *iterator);