xxxxxxxxxx
81
let W = 700;
let H = 700;
let cellSize = 20;
let liveCell = 1;
let deadCell = 0;
let grid = [];
function setup() {
createCanvas(W, H);
}
function Grid() {
// vertical lines
stroke(0);
let cellState = deadCell
for (let x = 0; x <= W; x = x + cellSize) {
for (let y = 0; y <= H; y = y + cellSize){
line(x,0,x,H);line(0,y,W,y)
grid.push([x,y,cellState])
}
}
}
function converToGrid() {
//Convert Mouse Coords To Grid Coords
let mx = mouseX;
let my = mouseY;
let gridX = Math.floor(mx / 20);
let gridY = Math.floor(my / 20);
return { x: gridX, y: gridY };
}
function drawCell(){
for (let i= 0; i<grid.length;i++){
if (grid[i][2]==liveCell){rect(i[0],i[1],cellSize,cellSize)}
}
}
function mouseClicked(){
coords = converToGrid()
grid[coords.y
}
function distance(x,y,x1,y1){
let d = Math.sqrt((x-x1)**2+(y-y1)**2)
return d
}
function nOfNeighbors(){
for (let i=0;i<grid.length;i++){
let n = 0
for (let j=0;j<grid.length;j++){
if (Equals(grid[i],grid[j])){continue}
let d = distance(grid[i][0],grid[i][1],grid[j][0],grid[j][1])
if (d<=cellSize){n+=1}
}grid[i].push(n);
}
return n;
}
function Equals(a1, a2) {
let diff = 0;
if (a1.length === a2.length) {
for (let i = 0; i < a1.length; i++) {
let c = a1[i] - a2[i];
diff += c;
}
}
return diff === 0;
}
function draw() {
background(220);
Grid();
}