Reaction-Diffusion (2)
About the code
Mitosis
for (int i = 0; i < 10; i++)
{
int sze = 10 + (rand() % 10);
int px = sze + (rand() % (SCREEN_WIDTH - sze * 2));
int py = sze + (rand() % (SCREEN_HEIGHT - sze * 2));
for (int y = 0; y < sze; ++y)
for (int x = 0; x < sze; ++x)
table[0][1][px + x][py + y] = 1.0;
}
Now let's try these coefficients:
double f = 0.038;
double k = 0.065;
Download source code
Download executable for Windows
We can see small spots duplicating themselves until they fill the whole screen.
Worms
double f = 0.046;
double k = 0.065;
Download source code
Download executable for Windows
Worms are slowly stretching, trying to fill as much space as they can while avoiding each others.
Clovers
double f = 0.042;
double k = 0.061;
Download source code
Download executable for Windows
This one looks a bit like the programs of the previous article.
Bubbles
double f = 0.062;
double k = 0.061;
Download source code
Download executable for Windows
Red bubbles appear in a spreading lighter blue ocean.
Rings
double f = 0.062;
double k = 0.063;
Download source code
Download executable for Windows
Circles grow, slowly getting more like clovers. When they can't get more complex, worms spread from their ends
Unstable
for (int i = 0; i < 15; i++)
{
[...]
for (int y = 0; y < sze; ++y)
for (int x = 0; x < sze; ++x)
table[0][1][px + x][py + y] = 0.6;
}
Now we will use a low value for f
double f = 0.01;
double k = 0.045;
Download source code
Download executable for Windows
This one is interesting. Circles will grows quickly. Reappearing cyclically.
Mapping the parameters
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 800
We will initialize the tables as a checkerboard of A and B.
// initialize the table
for (int y = 0; y < SCREEN_HEIGHT+2; ++y)
for (int x = 0; x < SCREEN_WIDTH+2; ++x)
for (int t = 0; t < 2; ++t)
{
int check = ((x / 10) % 2) ^ ((y / 10) % 2);
table[t][0][y][x] = check;
table[t][1][y][x] = 1 - check;
}
The f and k will be computed inside the loops of update():
for (int y = 1; y < SCREEN_HEIGHT+1; ++y)
for (int x = 1; x < SCREEN_WIDTH+1; ++x)
{
double f = 0.01 + 0.09 * (SCREEN_HEIGHT - y) / (SCREEN_HEIGHT - 1);
double k = 0.02 + 0.06 * (x - 1) / (SCREEN_WIDTH - 1);
double ca = 1.0 - f - 1.8 * DA;
double cb = 1.0 - f - 1.8 * DB - k;
Due to the the great range of values f and k can take, the concentrations could get bigger than 1.
for (int t = 0; t < 2; ++t)
{
if (table[1-curT][t][y][x] < 0.0)
table[1-curT][t][y][x] = 0.0;
if (table[1-curT][t][y][x] > 1.0)
table[1-curT][t][y][x] = 1.0;
}
Finally at the beginning of update we will fill the leftmost column with B and the rightmost with A to avoid black
for (int t = 0; t < 2; ++t)
{
for (int y = 0; y < SCREEN_HEIGHT + 2; ++y)
{
table[curT][t][y][SCREEN_WIDTH - 1] = (t == 0 ? 1.0 : 0.0);
table[curT][t][y][0] = (t == 0 ? 0.0 : 1);
table[1-curT][t][y][SCREEN_WIDTH - 1] = (t == 0 ? 1.0 : 0.0);
table[1-curT][t][y][0] = (t == 0 ? 0.0 : 1);
}
}
Download source code
Download executable for Windows
And after a few seconds we can see that: