xxxxxxxxxx
75
let cell = 40;
let bs = [];
let res;
const i16b = 0.00392156862; // result of dividing 1 by 255
function setup() {
createCanvas(400, 400);
px = createVector(width / cell, height / cell);
background(220, 100, 0);
loop();
frameRate(30);
}
function draw() {
let x = 0;
while (x < width) {
let y = 0;
while (y < height) {
let index = y * cell + x;
if (frameCount < 2) {
bs[index] = {
value: floor(random(1) * 2) * 255,
x: x,
y: y
}
} else {
checkNeighbors(index);
}
fill(bs[index].value);
rect(x, y, px.x, px.y);
y += px.y;
// console.log(x,y);
}
x += px.x;
}
}
function checkNeighbors(index) {
let top = index - cell * px.y;
let topLeft = top - px.x;
let topRight = top + px.x;
let left = index - px.x;
let right = index + px.x;
let bottom = index + cell * px.y;
let bottomLeft = bottom - px.x;
let bottomRight = bottom + px.x;
if (bottom < bs.length) {
if (bs[index].value < bs[bottom].value) {
let temp = bs[bottom].value;
bs[bottom].value = bs[index].value;
bs[index].value = temp;
} else if (bs[bottom].value >= bs[index].value || (bs[left] && [left].value && bs[left].value <= bs[index].value) || (bs[right] && [right].value && bs[right].value < bs.index.value)) {
let lwt = 0;
let rwt = 0;
if (left > 0 && bs[left].value < bs[index].value) {
lwt = (bs[left].value + bs[index].value) * 0.25;
}
if (right < width && bs[right].value < bs[index].value) {
rwt = (bs[right].value + bs[index].value) * 0.25;
}
let startingValue = bs[index].value;
let shiftLeft = bs[left].value - lwt;
let shiftRight = bs[right].value - rwt;
bs[index].value = startingValue * 0.5;
if(lwt > 0 && shiftLeft + bs[left].value < 255)
bs[left].value += shiftLeft;
if(rwt > 0 && shiftRight + bs[right].value < 255)
bs[right].value += shiftRight;
}
}
}