summaryrefslogtreecommitdiff
path: root/experimental/tectonics/quadtree.c
diff options
context:
space:
mode:
Diffstat (limited to 'experimental/tectonics/quadtree.c')
-rw-r--r--experimental/tectonics/quadtree.c73
1 files changed, 41 insertions, 32 deletions
diff --git a/experimental/tectonics/quadtree.c b/experimental/tectonics/quadtree.c
index 3cb6639..820844d 100644
--- a/experimental/tectonics/quadtree.c
+++ b/experimental/tectonics/quadtree.c
@@ -1,7 +1,11 @@
#include <math.h>
-#include "tectonics.h"
+#include <stddef.h>
+#include <stdlib.h>
+#include "util.h"
+#include "geometry.h"
-bool contains_point(struct quad_region_t region, struct point_t pt)
+bool quad_contains_point(struct quad_region_t region,
+ struct point_t pt)
{
if (region.center.x - region.half_dim > pt.x ||
region.center.x + region.half_dim <= pt.x)
@@ -14,7 +18,8 @@ bool contains_point(struct quad_region_t region, struct point_t pt)
}
-struct quadtree_node_t new_node(struct point_t center, double half_dim)
+struct quadtree_node_t quadtree_new_node(struct point_t center,
+ double half_dim)
{
struct quadtree_node_t node;
@@ -29,7 +34,8 @@ struct quadtree_node_t new_node(struct point_t center, double half_dim)
}
-void subdivide(struct quadtree_node_t *node, struct point_t *points)
+void quadtree_subdivide(struct quadtree_node_t *node,
+ struct point_t *points)
{
node->nw = malloc(sizeof(struct quadtree_node_t));
node->ne = malloc(sizeof(struct quadtree_node_t));
@@ -41,27 +47,30 @@ void subdivide(struct quadtree_node_t *node, struct point_t *points)
struct point_t c_nw =
{ center.x - quarter_dim, center.y - quarter_dim };
- *(node->nw) = new_node(c_nw, quarter_dim);
+ *(node->nw) = quadtree_new_node(c_nw, quarter_dim);
struct point_t c_ne =
{ center.x + quarter_dim, center.y - quarter_dim };
- *(node->ne) = new_node(c_ne, quarter_dim);
+ *(node->ne) = quadtree_new_node(c_ne, quarter_dim);
struct point_t c_sw =
{ center.x - quarter_dim, center.y + quarter_dim };
- *(node->sw) = new_node(c_sw, quarter_dim);
+ *(node->sw) = quadtree_new_node(c_sw, quarter_dim);
struct point_t c_se =
{ center.x + quarter_dim, center.y + quarter_dim };
- *(node->se) = new_node(c_se, quarter_dim);
+ *(node->se) = quadtree_new_node(c_se, quarter_dim);
if (node->id != -1) {
/* move point to a child */
- if (contains_point(node->nw->region, points[node->id]))
+ if (quad_contains_point(node->nw->region,
+ points[node->id]))
node->nw->id = node->id;
- else if (contains_point(node->ne->region, points[node->id]))
+ else if (quad_contains_point(node->ne->region,
+ points[node->id]))
node->ne->id = node->id;
- else if (contains_point(node->sw->region, points[node->id]))
+ else if (quad_contains_point(node->sw->region,
+ points[node->id]))
node->sw->id = node->id;
else
node->se->id = node->id;
@@ -69,10 +78,10 @@ void subdivide(struct quadtree_node_t *node, struct point_t *points)
}
}
-bool insert(struct quadtree_node_t *node,
- struct point_t *points, int id)
+bool quadtree_insert(struct quadtree_node_t *node,
+ struct point_t *points, int id)
{
- if (!contains_point(node->region, points[id]))
+ if (!quad_contains_point(node->region, points[id]))
return false;
if (node->id == -1 && node->nw == NULL) {
@@ -81,12 +90,12 @@ bool insert(struct quadtree_node_t *node,
}
if (node->nw == NULL)
- subdivide(node, points);
+ quadtree_subdivide(node, points);
- if (insert(node->nw, points, id)) return true;
- if (insert(node->ne, points, id)) return true;
- if (insert(node->sw, points, id)) return true;
- if (insert(node->se, points, id)) return true;
+ if (quadtree_insert(node->nw, points, id)) return true;
+ if (quadtree_insert(node->ne, points, id)) return true;
+ if (quadtree_insert(node->sw, points, id)) return true;
+ if (quadtree_insert(node->se, points, id)) return true;
return false;
}
@@ -113,11 +122,11 @@ static int get_points_in_region(struct quadtree_node_t *node,
}
-int get_closest(struct quadtree_node_t *node,
- struct point_t *points,
- struct point_t pt)
+int quadtree_get_closest(struct quadtree_node_t *node,
+ struct point_t *points,
+ struct point_t pt)
{
- if (!contains_point(node->region, pt))
+ if (!quad_contains_point(node->region, pt))
return -1;
if (node->id != -1)
@@ -125,13 +134,13 @@ int get_closest(struct quadtree_node_t *node,
int closest;
if (node->nw != NULL) {
- if ((closest = get_closest(node->nw, points, pt)) != -1)
+ if ((closest = quadtree_get_closest(node->nw, points, pt)) != -1)
return closest;
- if ((closest = get_closest(node->ne, points, pt)) != -1)
+ if ((closest = quadtree_get_closest(node->ne, points, pt)) != -1)
return closest;
- if ((closest = get_closest(node->sw, points, pt)) != -1)
+ if ((closest = quadtree_get_closest(node->sw, points, pt)) != -1)
return closest;
- if ((closest = get_closest(node->se, points, pt)) != -1)
+ if ((closest = quadtree_get_closest(node->se, points, pt)) != -1)
return closest;
/* closest point is in a subregion, but not the *same*
@@ -182,13 +191,13 @@ void draw_quadtree(cairo_t *cr, struct quadtree_node_t *node)
}
-void free_tree(struct quadtree_node_t *node)
+void quadtree_free(struct quadtree_node_t *node)
{
if (node->nw != NULL) {
- free_tree(node->nw);
- free_tree(node->ne);
- free_tree(node->sw);
- free_tree(node->se);
+ quadtree_free(node->nw);
+ quadtree_free(node->ne);
+ quadtree_free(node->sw);
+ quadtree_free(node->se);
}
free(node);