summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2014-08-04 12:29:43 -0400
committerTavian Barnes <tavianator@tavianator.com>2014-08-04 12:29:43 -0400
commita04e2f811d094f6641db4197388c267dff889b5c (patch)
tree3acc7120152d49c204ec02f5cba017d145f9c048 /main.c
parent2fb8418db686e1bf7d41d091054a5d01f0e37324 (diff)
downloadkd-forest-a04e2f811d094f6641db4197388c267dff889b5c.tar.xz
Uniform neighbor selection.
Diffstat (limited to 'main.c')
-rw-r--r--main.c39
1 files changed, 17 insertions, 22 deletions
diff --git a/main.c b/main.c
index c2b6c55..cf131c5 100644
--- a/main.c
+++ b/main.c
@@ -365,34 +365,29 @@ try_neighbor(const state_t *state, kd_node_t *node, int dx, int dy)
return node + (int)state->width*dy + dx;
}
-// Star pattern:
-// 6 1 4
-// 3 7
-// 0 5 2
-static int neighbor_order[][2] = {
- { -1, -1 },
- { 0, +1 },
- { +1, -1 },
- { -1, 0 },
- { +1, +1 },
- { 0, -1 },
- { -1, +1 },
- { +1, 0 },
-};
-
static kd_node_t *
next_neighbor(const state_t *state, kd_node_t *node)
{
- unsigned int first = rand_in(8);
- for (unsigned int i = first; i < first + 8; ++i) {
- int *delta = neighbor_order[i%8];
- kd_node_t *neighbor = try_neighbor(state, node, delta[0], delta[1]);
- if (neighbor && !neighbor->added) {
- return neighbor;
+ kd_node_t *neighbors[8];
+ unsigned int size = 0;
+ for (int dy = -1; dy <= 1; ++dy) {
+ for (int dx = -1; dx <= 1; ++dx) {
+ if (dx == 0 && dy == 0) {
+ continue;
+ }
+
+ kd_node_t *neighbor = try_neighbor(state, node, dx, dy);
+ if (neighbor && !neighbor->added) {
+ neighbors[size++] = neighbor;
+ }
}
}
- return NULL;
+ if (size == 0) {
+ return NULL;
+ }
+
+ return neighbors[rand_in(size)];
}
static void