xxxxxxxxxx
100
let grid;
let nextGrid;
let dA = 2.9;
let dB = 1.5;
let feed = 0.096;
let kill = 0.862;
function setup() {
createCanvas(400, 400);
pixelDensity(1);
grid = [];
nextGrid = [];
for (let x = 0; x < width; x++) {
grid[x] = [];
nextGrid[x] = [];
for (let y = 0; y < height; y++) {
grid[x][y] = {
a:0.5,
b: noise(x * 0.2, y * 0.1) > 0.2 ? 0 : 4 // initialize using Perlin noise
};
nextGrid[x][y] = {a: 1, b: 0};
}
}
}
function draw() {
background(220);
for (let x = 1; x < width - 1; x++) {
for (let y = 1; y < height - 1; y++) {
let a = grid[x][y].a;
let b = grid[x][y].b;
nextGrid[x][y].a = a +
(dA * laplaceA(x, y)) -
(a * b * b) +
(feed * (1 - a));
nextGrid[x][y].b = b +
(dB * laplaceB(x, y)) +
(a * b * b) -
((kill + feed) * b);
nextGrid[x][y].a = constrain(nextGrid[x][y].a, 0, 1);
nextGrid[x][y].b = constrain(nextGrid[x][y].b, 0, 1);
}
}
loadPixels();
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
let pix = (x + y * width) * 4;
let a = nextGrid[x][y].a;
let b = nextGrid[x][y].b;
let c = floor((a - b) * 255);
let n = noise(x * 0.1, y * 0.1) * 25; // Perlin noise influence
pixels[pix + 0] = n + c % 255;
pixels[pix + 1] = n;
pixels[pix + 2] = c;
pixels[pix + 3] = 255;
}
}
updatePixels();
swap();
}
function swap() {
let temp = grid;
grid = nextGrid;
nextGrid = temp;
}
function laplaceA(x, y) {
let sumA = 0;
sumA += grid[x][y].a * -1;
sumA += grid[x - 1][y].a * 0.2;
sumA += grid[x + 1][y].a * 0.2;
sumA += grid[x][y - 1].a * 0.2;
sumA += grid[x][y + 1].a * 0.5;
sumA += grid[x - 1][y - 1].a * 0.05;
sumA += grid[x + 1][y - 1].a * 0.09;
sumA += grid[x + 1][y + 1].a * 0.05;
sumA += grid[x - 1][y + 1].a * 0.05;
return sumA;
}
function laplaceB(x, y) {
let sumB = 0;
sumB += grid[x][y].b * -1;
sumB += grid[x - 1][y].b * 0.2;
sumB += grid[x + 1][y].b * 0.4;
sumB += grid[x][y - 1].b * 0.7;
sumB += grid[x][y + 1].b * 0.2;
sumB += grid[x - 1][y - 1].b * 0.05;
sumB += grid[x + 1][y - 1].b * 0.05;
sumB += grid[x + 1][y + 1].b * 0.05;
sumB += grid[x - 1][y + 1].b * 0.05;
return sumB;
}