xxxxxxxxxx
110
let grid;
let next;
let dA = 1;
let dB = 0.5;
let feed = 0.055;
let kill = 0.062;
function setup() {
createCanvas(700, 700);
pixelDensity(1);
grid = [];
next = [];
for (let x = 0; x < width; x++) {
grid[x] = [];
next[x] = [];
for (let y = 0; y < height; y++) {
grid[x][y] = {
a: 1,
b: 0
};
next[x][y] = {
a: 1,
b: 0
};
}
}
for (let i = 300; i < 330; i++) {
for (let j = 300; j < 330; j++) {
grid[i][j].b = 1;
}
}
for (let m = 0; m < 1000; m++) {
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;
next[x][y].a = a + (dA * laplaceA(x, y) - a * b * b + feed * (1 - a));
next[x][y].b = b + (dB * laplaceB(x, y) + a * b * b - (kill + feed) * b);
next[x][y].a = constrain(next[x][y].a, 0, 1);
next[x][y].b = constrain(next[x][y].b, 0, 1);
}
}
grid = next;
}
}
function draw() {
background(51);
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;
next[x][y].a = a + (dA * laplaceA(x, y) - a * b * b + feed * (1 - a));
next[x][y].b = b + (dB * laplaceB(x, y) + a * b * b - (kill + feed) * b);
next[x][y].a = constrain(next[x][y].a, 0, 1);
next[x][y].b = constrain(next[x][y].b, 0, 1);
}
}
grid = next;
loadPixels();
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
let pix = (x + y * width) * 4;
pixels[pix + 0] = floor(grid[x][y].a * 255);
pixels[pix + 1] = 0;
pixels[pix + 2] = floor(grid[x][y].b * 255);
pixels[pix + 3] = 255;
}
}
updatePixels();
}
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.2;
sumA += grid[x - 1][y - 1].a * 0.05;
sumA += grid[x + 1][y - 1].a * 0.05;
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.2;
sumB += grid[x][y + 1].b * 0.2;
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;
}