fractalkitty looking for math? fractalkitty.com

cellular automata

GOL QR

#grid#image#animation#accumulates#dom#cellular-automata

cellsnCellsinputimgLoadedneedsResethandleFiletempImgreInitializedrawCellsdrawLineGridpopulateGridcheckNeighborsrunGOL

let qr;
let d;
let cells;
let nCells;
let input;
let img;
let imgLoaded = false;
let needsReset = false;
function preload() {
  qr = loadImage("/assets/qr.png");
}
function handleFile(file) {
  if (file.type === "image") {
    let tempImg = createImg(file.data, "");
    tempImg.hide();
    loadImage(tempImg.elt.src, function (newImage) {
      console.log("Successfully loaded new image");
      qr = newImage;
      cells = [];
      needsReset = true;
    });
  } else {
    console.log("Not an image file!");
  }
}

function reInitialize() {
  background(255);
  cells = [];

  cnt = 2;
  col1 = [0, 0, 0, 0];
  noStroke();
  while (col1[0] < 200) {
    x = cnt;
    y = 5;
    col1 = qr.get(qr.width / (width / x), qr.height / (height / y));
    cnt++;
  }
  console.log(cnt);

  d = (cnt - 2) / 7;
  nCells = int(width / d) + 1;

  populateGrid();
  drawLineGrid();
  drawCells();
}

function setup() {
  cnv = createCanvas(400, 400);

  frameRate(2);
  reInitialize();
}

function draw() {
  if (needsReset) {
    reInitialize();
    needsReset = false;
  }
  runGOL();
  drawCells();
}

function drawCells() {
  for (let i = 0; i < nCells; i++) {
    for (let j = 0; j < nCells; j++) {
      x = i * d;
      y = j * d;
      if (cells[i][j].alive === 0) {
        fill(200, 255, 255, 50);
      } else {
        fill(0, 0, 0, 50);
      }

      rect(x, y, d, d);
    }
  }
}

function drawLineGrid() {
  d = (cnt - 2) / 7;
  strokeWeight(1);
  stroke(200, 100, 100);
  for (let i = 0; i < width; i += d) {
    line(i, 0, i, height);
    line(0, i, width, i);
  }
}

function populateGrid() {
  for (let i = 0; i < nCells; i++) {
    cells[i] = [];
    for (let j = 0; j < nCells; j++) {
      x = i * d + d;
      y = j * d + d;
      col = qr.get(qr.width / (width / x), qr.height / (height / y));

      if (col[0] > 100) {
        a = 0;
      } else {
        a = 1;
      }

      cells[i][j] = {
        alive: a,
        neighborsAlive: 2
      };
    }
  }
}

function checkNeighbors() {
  for (let i = 0; i < nCells; i++) {
    for (let j = 0; j < nCells; j++) {
      if (j === 0) {
        if (i === 0) {
          cells[i][j].neighborsAlive =
            cells[i][j + 1].alive + cells[i + 1][j].alive;
        } else if (i < nCells - 1) {
          cells[i][j].neighborsAlive =
            cells[i][j + 1].alive +
            cells[i + 1][j].alive +
            cells[i - 1][j].alive;
        } else if (i === nCells - 1) {
          cells[i][j].neighborsAlive =
            cells[i][j + 1].alive + cells[i - 1][j].alive;
        }
      } else if (j < nCells - 1) {
        if (i === 0) {
          cells[i][j].neighborsAlive =
            cells[i][j + 1].alive +
            cells[i][j - 1].alive +
            cells[i + 1][j].alive;
        } else if (i < nCells - 1) {
          cells[i][j].neighborsAlive =
            cells[i][j + 1].alive +
            cells[i][j - 1].alive +
            cells[i + 1][j].alive +
            cells[i - 1][j].alive;
        } else if (i === nCells - 1) {
          cells[i][j].neighborsAlive =
            cells[i][j + 1].alive +
            cells[i][j - 1].alive +
            cells[i - 1][j].alive;
        }
      } else if (j === nCells - 1) {
        if (i === 0) {
          cells[i][j].neighborsAlive =
            cells[i][j - 1].alive + cells[i + 1][j].alive;
        } else if (i < nCells - 1) {
          cells[i][j].neighborsAlive =
            cells[i][j - 1].alive +
            cells[i + 1][j].alive +
            cells[i - 1][j].alive;
        } else if (i === nCells - 1) {
          cells[i][j].neighborsAlive =
            cells[i][j - 1].alive + cells[i - 1][j].alive;
        }
      }
    }
  }
}

function runGOL() {
  checkNeighbors();
  for (let i = 0; i < nCells; i++) {
    for (let j = 0; j < nCells; j++) {
      if (
        (cells[i][j].neighborsAlive < 2 || cells[i][j].neighborsAlive > 3) &&
        cells[i][j].alive === 1
      ) {
        cells[i][j].alive = 0;
      } else if (
        cells[i][j].neighborsAlive === 3 ||
        (cells[i][j].neighborsAlive === 2 && cells[i][j].alive === 1)
      ) {
        cells[i][j].alive = 1;
      }
    }
  }
}