xxxxxxxxxx
205
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 cell;
let cells = [];
let states = [];
let counter = 0;
let t = 0;
function setup() {
createCanvas(
Math.round(windowWidth / resolution1) * resolution1,
Math.round(windowHeight / resolution2) * resolution2
);
cols = (Math.round(width / 10) * 10) / resolution1;
rows = (Math.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] = 0;
}
}
states.push(grid);
cells = make2DArray(cols, rows);
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
cells[i][j] = new Cell(
i * resolution1,
j * resolution2,
resolution1,
resolution2,
val
);
}
}
//frameRate(24);
}
function mouseDragged() {
if (mouseButton === LEFT) {
counter = 0;
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
if (cells[i][j].mouseOverCircle()) {
states[t][i][j] = 1;
}
}
}
} else if (mouseButton === RIGHT) {
counter = 0;
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
if (cells[i][j].mouseOverCircle()) {
states[t][i][j] = 0;
}
}
}
}
}
function keyPressed() {
if (keyCode === BACKSPACE) {
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
states[t][i][j]= 0;
}
}
}
}
function draw() {
background(255);
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
cells[i][j].display();
if (states[t][i][j] == 1) {
cells[i][j].state = 1;
} else if (states[t][i][j] == 0) {
cells[i][j].state = 0;
}
}
}
if (mouseIsPressed ) {
let next = make2DArray(cols, rows);
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
let state = states[t][i][j];
let sum = 0;
let neighbors = countNeighbors(grid, i, j);
if (state == 0 && neighbors ==4) {
next[i][j] = 1;
} else if (state == 1 && (neighbors < 1 || neighbors > 6)) {
next[i][j] = 0;
} else {
next[i][j] = state;
}
}
}
t++;
if (t == states.length) {
states.push(next);
}
} else if (keyIsPressed && keyCode === LEFT_ARROW) {
t = max(t - 1, 1);
states.splice(max(states.length - 1, 2), 1);
}
//print(t,states.length)
}
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 += states[t][col][row];
}
}
sum -= states[t][x][y];
return sum;
}
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 = 255;
this.time = 0;
}
display() {
if (this.state === 0) {
this.col = 255;
} else if (this.state === 1) {
this.col = 0;
}
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 < 60) {
return true;
}
}
// decay(){
// if(this.state === 1){
// this.time++;
// if(this.time>20){
// this.state = 0;
// } else if (this.state === 0){
// this.time = 0;
// }
// }
// }
}