diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-08-25 22:54:10 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-08-25 22:57:33 +0100 |
commit | 0a5d5fc87cc452e9c82ff8ef91662686f9ffe283 (patch) | |
tree | 559ce98af7f10aa03578e577bc7579cdb02f00eb | |
parent | 21c03264e65042db3b5856fba8a6cd1099438f96 (diff) | |
download | abelian-sandpile-0a5d5fc87cc452e9c82ff8ef91662686f9ffe283.tar.gz abelian-sandpile-0a5d5fc87cc452e9c82ff8ef91662686f9ffe283.tar.bz2 abelian-sandpile-0a5d5fc87cc452e9c82ff8ef91662686f9ffe283.zip |
~modified step,~state data is now unsigned 64 bit integer
Stopped adding a grain of sand per step.
Collapsing should work properly for any amount of sand grains,
regardless of whether it's 4 or 4000. Previous code fixed it at 4
always, and how it distributed was so as well. Now we distribute as
much sand as we can.
This comes from the idea that, instead of incrementing while
rendering, let's just set some n pixels in the centre and see how the
model distributes them.
-rw-r--r-- | lib.h | 3 | ||||
-rw-r--r-- | main.c | 11 |
2 files changed, 7 insertions, 7 deletions
@@ -2,12 +2,13 @@ #define LIB_H #include <stdbool.h> +#include <stdint.h> #include <stdlib.h> typedef struct State { // Sandpiles - unsigned char *data; + uint64_t *data; size_t dwidth; size_t window_len; @@ -17,12 +17,11 @@ void step(state_t *state) { - state->data[((state->dwidth / 2) * state->dwidth) + (state->dwidth / 2)]++; for (size_t i = 0; i < state->dwidth; ++i) for (size_t j = 0; j < state->dwidth; ++j) if (state->data[(i * state->dwidth) + j] >= 4) { - unsigned char *references[] = { + uint64_t *references[] = { (j == 0) ? NULL : &state->data[((i)*state->dwidth) + j - 1], (i == state->dwidth - 1) ? NULL @@ -33,8 +32,8 @@ void step(state_t *state) (i == 0) ? NULL : &state->data[((i - 1) * state->dwidth) + j]}; for (size_t k = 0; k < 4; ++k) if (references[k]) - *references[k] += 1; - state->data[(i * state->dwidth) + j] = 0; + *references[k] += state->data[(i * state->dwidth) + j] / 4; + state->data[(i * state->dwidth) + j] %= 4; } } @@ -97,8 +96,8 @@ int main(void) for (size_t i = 0; i < state.dwidth; ++i) for (size_t j = 0; j < state.dwidth; ++j) { - Color c = {0}; - unsigned char sandpile = state.data[(i * state.dwidth) + j]; + Color c = {0}; + uint64_t sandpile = state.data[(i * state.dwidth) + j]; if (sandpile == 0) c = BLACK; else if (sandpile == 1) |