Some P5 sketches I made.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.5 KiB

2 years ago
const height = 512;
const width = 512;
const gridWidth = 64;
const gridHeight = 64;
const tileWidth = width/gridWidth;
const tileHeight = height/gridHeight;
let colorMap =
[[0,0,0],
[0,0,255],
[0,255,0],
[255,0,0],
[0,255,255],
[255,0,255],
[255,255,0],
[255,255,255]];
let sandpiles = Array(gridHeight);
let oldpiles = new Array(sandpiles.length);
function setup() {
createCanvas(width, height);
for(let i = 0; i < gridHeight; i++){
sandpiles[i] = Array(gridWidth);
for(let j = 0; j < gridWidth; j++)
sandpiles[i][j] = 0;
}
sandpiles[gridHeight/2][gridWidth/2] = 10000;
//sandpiles[gridHeight/2][gridWidth/2+1] = 4;
for(let i = 0; i < gridHeight; i++)
oldpiles[i] = new Array(sandpiles[i].legnth);
// for(let k = 0; k< 10000; k++)
// update_sandpiles_in();
noStroke();
frameRate(5);
}
function update_sandpiles() {
for(let i = 0; i < gridHeight; i++)
for(let j = 0; j < gridWidth; j++)
oldpiles[i][j] = sandpiles[i][j];
for(let i = 0; i < gridHeight; i++)
for(let j = 0; j < gridWidth; j++){
if (oldpiles[i][j] > 4){
sandpiles[i][j] -= 4;
if(i > 0)
sandpiles[i-1][j]++;
if(j > 0)
sandpiles[i][j-1]++;
if(i < gridHeight-1)
sandpiles[i+1][j]++;
if(j < gridWidth-1)
sandpiles[i][j+1]++;
}
}
}
function update_sandpiles_in() {
for(let k = 0; k < 2; k++)
for(let i = 0; i < gridHeight; i++){
for(let j = (i+k)%2; j < gridWidth; j+= 2){
//sandpiles[i][j] = 3+k;
if (sandpiles[i][j] > 4){
sandpiles[i][j] -= 4;
if(i > 0)
sandpiles[i-1][j]++;
if(j > 0)
sandpiles[i][j-1]++;
if(i < gridHeight-1)
sandpiles[i+1][j]++;
if(j < gridWidth-1)
sandpiles[i][j+1]++;
}
}
}
}
function draw() {
update_sandpiles_in();
// put drawing code her
background(0);
for(let i = 0; i < gridHeight; i++)
for(let j = 0; j < gridWidth; j++){
let color = colorMap[constrain(sandpiles[i][j],0,7)];
fill(color[0],color[1], color[2]);
rect( j*tileWidth, i*tileHeight,tileWidth,tileWidth);
}
//noLoop();
}