Skip to content

Instantly share code, notes, and snippets.

@607011
Last active July 2, 2019 13:10
Show Gist options
  • Save 607011/6991350 to your computer and use it in GitHub Desktop.
Save 607011/6991350 to your computer and use it in GitHub Desktop.
One iteration of the Belousov-Zhabotinsky reaction:// cell: pointer to first element of 2D array (source)cell_new: pointer to first element of 2D array (destination)// width: width of arrays// height: height of arrays// k1: influence of excited cells in neighborhood// k2: influence of activated cells in neighborhood// g: speed at which waves tra…
void BZR_iterate(int *cell, int *cell_new,
int width, int height,
int k1, int k2, int g, int n) {
int x, y, xx, yy;
int c, S, excited, active;
int v;
int c_new;
for (y = 0; y < height; ++y) {
for (x = 0; x < width; ++x) {
c = cell[x + y * (width+2)];
S = c;
excited = 0;
active = 0;
for (yy = -1; yy <= 1; ++yy) {
for (xx = -1; xx <= 1; ++xx) {
v = cell[(x + xx) + (y + yy) * (width+2)];
S += v;
if (v == n)
++active;
else if (v != 0)
++excited;
}
}
if (c == 0)
c_new = excited / k1 + active / k2;
else if (c == n)
c_new = 0;
else
c_new = S / (excited + active + 1) + g;
if (c_new > n)
c_new = n;
cell_new[x + y * (width+2)] = c_new;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment