fractalkitty looking for math? fractalkitty.com

Mathober 2024

consistency

#random#grid#color#text#interactive#animation#math#cellular-automata#mathober

bgColgColnGridplayersgSpacefillRectcheckAlivedrawGrid


const bgCol = "#447abd";
const gCol = "#a2c2e8";
const nGrid = 20;
let players;
let gSpace;

function setup() {
  w = max(400, min(windowWidth, windowHeight) * 0.9);
  describe(" ");
  players = [];
  createCanvas(w, w);
  rectMode(CENTER);
  describe("the game of life on a blueprint design");
  nV = 3;
  mul = [0.89, 0.92, 0.93, 0.935, 0.935, 0.94, 0.945];
  r = (w * 0.82) / 2;
  d = w;
  angleMode(DEGREES);
  textSize(16);
  frameRate(3);
  gSpace = (w / nGrid) * 0.95;

  for (let i = 0; i < nGrid - 1; i++) {
    players[i] = [];
    for (let j = 0; j < nGrid - 1; j++) {
      if (i === j) {
        b = 1;
      } else {
        b = 0;
      }
      players[i][j] = {
        x: -width / 2 + gSpace * 1.5 + i * gSpace,
        y: -width / 2 + gSpace * 1.5 + j * gSpace,
        alive: random([0, 1, 1]),
        n: 0
      };
    }
  }
}

function draw() {
  background(bgCol);
  translate(w / 2, w / 2);

  t = frameCount / 3;
  noStroke();
  checkAlive();
  for (let i = 0; i < nGrid - 1; i++) {
    for (let j = 0; j < nGrid - 1; j++) {
      if (
        players[i][j].n === 1 &&
        (players[i][j].alive === 1 || players[i][j].alive === 3)
      ) {
        fillRect(players[i][j].x, players[i][j].y, 1);
      } else if (players[i][j].alive === 1) {
        fillRect(players[i][j].x, players[i][j].y, -1);
      }
    }
  }

  noStroke();
  fill(gCol);
  text("consistency", -w / 2 + 10, w / 2 - 5);
  drawGrid();
}
function fillRect(x, y, s) {
  push();
  translate(x, y);
  stroke(gCol);
  strokeWeight(2);
  if (s === 1) {
    scale(1, 1);
  } else {
    scale(1, -1);
  }
  for (let i = 0; i <= 6; i++) {
    line(
      -gSpace / 2 + (gSpace / 6) * i,
      -gSpace / 2,
      -gSpace / 2,
      -gSpace / 2 + (gSpace / 6) * i
    );
    line(
      gSpace / 2 - (gSpace / 6) * i,
      gSpace / 2,
      gSpace / 2,
      gSpace / 2 - (gSpace / 6) * i
    );
  }
  pop();
}
function checkAlive() {
  s = nGrid - 2;

  players[0][0].n = players[0][1].alive + players[1][0].alive;
  players[s][0].n = players[s][1].alive + players[s - 1][0].alive;
  players[s][s].n = players[s][s - 1].alive + players[s - 1][s].alive;
  players[0][s].n = players[0][s - 1].alive + players[s - 1][s].alive;
  for (let j = 1; j < s; j++) {
    players[0][j].n =
      players[0][j - 1].alive + players[0][j + 1].alive + players[1][j].alive;
    players[s][j].n =
      players[s][j - 1].alive +
      players[s][j + 1].alive +
      players[s - 1][j].alive;
    players[j][0].n =
      players[j - 1][0].alive + players[j + 1][0].alive + players[j][1].alive;
    players[j][s].n =
      players[j - 1][s].alive +
      players[j + 1][s].alive +
      players[j][s - 1].alive;
  }
  for (let i = 2; i < s - 1; i++) {
    for (let j = 2; j < s - 1; j++) {
      players[i][j].n =
        players[i][j + 1].alive +
        players[i][j - 1].alive +
        players[i - 1][j].alive +
        players[i + 1][j].alive;
    }
  }
  for (let i = 0; i <= s; i++) {
    for (let j = 0; j <= s - 1; j++) {
      if (players[i][j].alive === 1) {
        if (players[i][j].n < 2) {
          players[i][j].alive = 0;
        } else if (players[i][j].n === 2 || players[i][j].n === 3) {
          players[i][j].alive = 1;
        } else if (players[i][j].n > 3) {
          players[i][j].alive = 0;
        }
      } else {
        if (players[i][j].n === 3) {
          players[i][j].alive = 1;
        }
      }
    }
  }
}
function drawGrid() {
  gSpace = (w / nGrid) * 0.95;
  stroke(gCol);
  strokeWeight(0.3);
  for (let i = 1; i <= nGrid; i++) {
    for (let j = 1; j <= nGrid; j++) {
      line(
        -w / 2 + gSpace * i,
        -w / 2 + gSpace,
        -w / 2 + gSpace * i,
        w / 2 - gSpace
      );
      line(
        -w / 2 + gSpace,
        -w / 2 + gSpace * i,
        w / 2 - gSpace,
        -w / 2 + gSpace * i
      );
    }
  }
  noFill();
  strokeWeight(3);
  rect(0, 0, w - 2 * gSpace);
}

function mousePressed() {
  setup();
  draw();
}
function windowResized() {
  setup();
  draw();
}