xxxxxxxxxx
138
let rule = [[3,4],[3,4], 0.05, 0.15];
let stopPer = 1;
let cols = 240,rows = 160;
let grid = (() => {
let tem = new Array(cols);
for (let i = 0; i < tem.length; i++) {
tem[i] = new Array(rows);
}
return tem;
})()
let size = 5;
function setup() {
createCanvas(1200,800);
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++){
let coin = random(1);
if (coin < rule[2]) {
grid[i][j] = 1;
} else {
grid[i][j] = 0;
}
}
}
frameRate(15);
}
function draw() {
if (keyIsPressed && key === 's') noLoop();
if (mouseIsPressed && mouseButton === RIGHT) noLoop();
background(255,8);
noFill();
strokeWeight(0.5);
stroke(0);
let alive = []
grid.forEach((col,x) => {
col.forEach((c,y) => {
if (c) {
alive.push({x:x* size + size/2,y:y*size + size/2});
}
});
});
if (alive.length >= stopPer * cols * rows) noLoop();
let aliveDelaunay = d3.Delaunay.from(alive.map(o =>[o.x, o.y]));
let aliveVor = aliveDelaunay.voronoi([-1,-1,width+1,height+1]);
for (let i = 0, n = aliveDelaunay.halfedges.length; i < n; i++) {
let j = aliveDelaunay.halfedges[i];
if (i < j) continue;
let vi = Math.floor(i / 3) * 2;
let vj = Math.floor(j / 3) * 2;
let d = dist(aliveVor.circumcenters[vi], aliveVor.circumcenters[((vi + 1) + aliveVor.circumcenters.length) % aliveVor.circumcenters.length], aliveVor.circumcenters[vj], aliveVor.circumcenters[((vj + 1) + aliveVor.circumcenters.length) % aliveVor.circumcenters.length]);
stroke(map(d,10,400,50,150),map(d,10,400,200,100));
line(aliveVor.circumcenters[vi], aliveVor.circumcenters[((vi + 1) + aliveVor.circumcenters.length) % aliveVor.circumcenters.length], aliveVor.circumcenters[vj], aliveVor.circumcenters[((vj + 1) + aliveVor.circumcenters.length) % aliveVor.circumcenters.length]);
}
updateGrid();
//noLoop();
}
const updateGrid = function () {
let next = (() => {
let tem = new Array(cols);
for (let i = 0; i < tem.length; i++) {
tem[i] = new Array(rows);
}
return tem;
})()
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++){
let pre = grid[i][j];
let sum = count(grid, i, j);
if (pre) sum--;
let nxt;
if (pre) {
if (rule[0].includes(sum)) {
nxt = 1;
} else {
nxt = 0;
}
} else {
if (rule[1].includes(sum)) {
nxt = 1;
} else {
nxt = 0;
}
}
next[i][j] = nxt;
}
}
grid = next;
}
const count = function (grid, i, j) {
let sum = 0;
for (let k = -1; k < 2; k++) {
for (let l = -1; l < 2; l++){
let x = (i + k + cols) % cols;
let y = (j + l + rows) % rows;
sum += grid[x][y];
}
}
return sum;
}
function keyPressed(){
if (keyCode === SHIFT){
noLoop();
}
}
function mouseClicked() {
if (mouseX > width || mouseX < 0 || mouseY > height || mouseY < 0) return;
loop();
background(255);
rule = random([
[[2,3],[3], 0.1, 0.15],
[[3,4],[3,4], 0.05, 0.15],
[[],[2],0.005, 0.01],
[[1,3,5,7],[1,3,5,7], 0.0005, 0.001],
[[1,2,5],[3,6], 0.1, 0.15],
[[2,3],[3,6], 0.1, 0.15],
[[3,4,6,7,8],[3,6,7,8], 0.2, 0.25],
[[2,4,5],[3,6,8], 0.15, 0.2],
[[3,5,6,7,8],[4,6,7,8], 0.4,0.55]
]);
let thre = random(rule[2], rule[3]);
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++){
let coin = random(1);
if (coin < thre) {
grid[i][j] = 1;
} else {
grid[i][j] = 0;
}
}
}
}