xxxxxxxxxx
201
class Cell {
constructor(x, y, sizeX, sizeY, state) {
this.x = x;
this.y = y;
this.sizeX = sizeX;
this.sizeY = sizeY;
this.state = state;
this.opacity = 0;
this.col = 0;
}
display() {
if (this.state === 0) {
this.col = 0;
} else if (this.state === 1) {
this.col = 255;
}
fill(this.col);
noStroke();
rectMode(CORNER);
rect(this.x, this.y, this.sizeX, this.sizeY);
}
mouseOver() {
if (
mouseX > this.x - this.sizeX * 5 &&
mouseX < this.x + this.sizeX * 5 &&
mouseY > this.y - this.sizeY * 15 &&
mouseY < this.y + this.sizeY * 15
) {
return true;
}
}
mouseOverCircle() {
let d = dist(
mouseX,
mouseY,
this.x + this.sizeX / 2,
this.y + this.sizeY / 2
);
if (d < 100) {
return true;
}
}
}
function make2DArray(cols, rows) {
let arr = new Array(cols);
for (let i = 0; i < arr.length; i++) {
arr[i] = new Array(rows);
}
return arr;
}
let grid;
let cols;
let rows;
let resolution1 = 5;
let resolution2 = 5;
let val = 0;
let cells = [];
let counter = 0;
function setup() {
createCanvas(
round(windowWidth / resolution1) * resolution1,
round(windowHeight / resolution2) * resolution2
);
cols = (round(width / 10) * 10) / resolution1;
rows = (round(height / 10) * 10) / resolution2;
grid = make2DArray(cols, rows);
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j] = new Cell(
i * resolution1,
j * resolution2,
resolution1,
resolution2,
0
);
}
}
// for (let i = 10; i < 15 ; i++) {
// for (let j = 10; j < 20; j++) {
// grid[i][j].state =1
// }
// }
frameRate(24);
}
function mouseDragged() {
if (mouseButton === LEFT) {
counter = 0;
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
if (grid[i][j].mouseOverCircle()) {
grid[i][j].state = 1; //abs(grid[i][j].state-1);
}
}
}
} else if (mouseButton === RIGHT) {
counter = 0;
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
if (grid[i][j].mouseOverCircle()) {
grid[i][j].state = 0; //abs(grid[i][j].state-1);
}
}
}
}
}
function keyPressed() {
if (keyCode === BACKSPACE) {
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j].state = 0; //abs(grid[i][j].state-1);
}
}
}
}
function draw() {
background(0);
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j].display();
if (grid[i][j] === 0) {
grid[i][j].state = abs(grid[i][j].state - 1);
} else {
grid[i][j].state = grid[i][j].state;
}
}
}
if (mouseIsPressed || keyIsPressed) {
counter++;
let next = make2DArray(cols, rows);
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
next[i][j] = new Cell(
i * resolution1,
j * resolution2,
resolution1,
resolution2,
grid[i][j].state
);
}
}
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
let state = grid[i][j].state;
let sum = 0;
let neighbors = countNeighbors(grid, i, j);
if (state == 0 && neighbors == 4) {
next[i][j].state = 1;
} else if (state == 1 && (neighbors < 1 || neighbors > 6)) {
next[i][j].state = 0;
} else {
next[i][j].state = grid[i][j].state;
}
}
}
grid = next;
}
// push();
// // blendMode(EXCLUSION);
// fill(255);
// textSize(40);
// textAlign(LEFT,TOP)
// text('Generation ' + counter,20,20)
// pop();
}
function countNeighbors(grid, x, y) {
let sum = 0;
for (let i = -1; i < 2; i++) {
for (let j = -1; j < 2; j++) {
let col = (x + i + cols) % cols;
let row = (y + j + rows) % rows;
sum += grid[col][row].state;
}
}
sum -= grid[x][y].state;
return sum;
}