P.cleanOutput = function () {
let iterations = 0;
if (this.dirtyScene) this.cleanScene();
if (!this.dirtyScene) {
const {
dataArrays,
diffusionRateA,
diffusionRateB,
feedRate,
killRate,
currentSource,
drawEvery,
maxGenerations,
currentGeneration,
width,
height,
rowAbove,
rowBelow,
colLeft,
colRight,
} = this;
let sourceA, destA, sourceB, destB;
if (!maxGenerations || currentGeneration < maxGenerations) {
let generation = currentGeneration,
sourceFlag = currentSource;
while (iterations < drawEvery) {
if (sourceFlag) [destA, sourceA, destB, sourceB] = dataArrays;
else [sourceA, destA, sourceB, destB] = dataArrays;
const killPlusFeed = killRate + feedRate;
let index,
rowAboveIndex, rowHere, rowBelowIndex,
colLeftIndex, colRightIndex,
topLeft, top, topRight,
left, right,
bottomLeft, bottom, bottomRight,
a, b, bb, reaction,
lapA, lapB,
da, db;
for (let row = 0; row < height; row++) {
rowAboveIndex = rowAbove[row];
rowHere = row * width;
rowBelowIndex = rowBelow[row];
for (let col = 0; col < width; col++) {
index = rowHere + col;
colLeftIndex = colLeft[col];
colRightIndex = colRight[col];
topLeft = rowAboveIndex + colLeftIndex;
top = rowAboveIndex + col;
topRight = rowAboveIndex + colRightIndex;
left = rowHere + colLeftIndex;
right = rowHere + colRightIndex;
bottomLeft = rowBelowIndex + colLeftIndex;
bottom = rowBelowIndex + col;
bottomRight = rowBelowIndex + colRightIndex;
a = sourceA[index];
b = sourceB[index];
bb = b * b;
reaction = a * bb;
lapA =
(sourceA[index] * -1) +
(sourceA[topLeft] * 0.05) +
(sourceA[topRight] * 0.05) +
(sourceA[bottomLeft] * 0.05) +
(sourceA[bottomRight] * 0.05) +
(sourceA[top] * 0.2) +
(sourceA[right] * 0.2) +
(sourceA[left] * 0.2) +
(sourceA[bottom] * 0.2);
lapB =
(sourceB[index] * -1) +
(sourceB[topLeft] * 0.05) +
(sourceB[topRight] * 0.05) +
(sourceB[bottomLeft] * 0.05) +
(sourceB[bottomRight] * 0.05) +
(sourceB[top] * 0.2) +
(sourceB[right] * 0.2) +
(sourceB[left] * 0.2) +
(sourceB[bottom] * 0.2);
da = a + (diffusionRateA * lapA) - reaction + (feedRate * (1 - a));
db = b + (diffusionRateB * lapB) + reaction - (killPlusFeed * b);
destA[index] = da < 0 ? 0 : da > 1 ? 1 : da;
destB[index] = db < 0 ? 0 : db > 1 ? 1 : db;
}
}
sourceFlag = sourceFlag ? 0 : 1;
generation++;
iterations++;
if (maxGenerations && generation >= maxGenerations) break;
}
this.currentSource = sourceFlag;
this.currentGeneration = generation;
if (sourceFlag) {
this.outputA = dataArrays[0];
this.outputB = dataArrays[2];
}
else {
this.outputA = dataArrays[1];
this.outputB = dataArrays[3];
}
this.paintCanvas();
}
else if (this.dirtyOutput) this.paintCanvas();
}
};