xxxxxxxxxx
151
let sponge = [];
let z = 0.5;
let level = 0;
let scaling = false;
let scalingPoint = 1;
function setup(){
createCanvas(1200,1200);
let b = new Box(0,0, 1200);
sponge.push([b]);
}
function nextRound(){
let next = [];
let m = 0;
for(let i = 0; i < sponge.length; i++){
next[i*3] = [];
next[i*3+1] = [];
next[i*3+2] = [];
for(let j = 0; j < sponge[i].length; j++){
let newBoxes = sponge[i][j].generate();
for(let k = 0; k < newBoxes.length; k++){
for(let l = 0; l < newBoxes[k].length; l++) {
next[i*3+ k].push(newBoxes[k][l]);
}
}
}
}
sponge = next;
}
function draw(){
if(scaling){
scalingPoint *= 1.01;
if(scalingPoint >= 2.0){
sponge.slice(0, 300);
}
if(scalingPoint >= 3.0){
scalingPoint = 1.0;
scaling = false;
sponge = sponge.slice(0, 81);
for(let i = 0; i < sponge.length; i++){
sponge[i] = sponge[i].slice(0, 81);
for(let j = 0; j < sponge[i].length; j++){
sponge[i][j].x *= 3.0;
sponge[i][j].y *= 3.0;
sponge[i][j].r *= 3.0;
}
}
}
}else{
scalingPoint = 1.01;
nextRound();
level++;
if(level > 4){
scaling = true;
level = 4;
}
}
scale(scalingPoint);
background(51);
stroke(255);
fill(255);
translate(sponge[0].x, sponge[0].y);
for(let i = 0; i < sponge.length; i++){
for(let j = 0; j < sponge[i].length; j++){
sponge[i][j].show();
}
}
}
function Box (x, y, r, space) {
this.x = x;
this.y = y;
this.r = r;
this.space = space || false;
this.c = 255;
this.show = function(){
if(this.space && this.c > 0){
this.c-=2;
}
noStroke();
fill(this.c);
square(this.x,this.y, this.r);
}
this.generate = function() {
let boxes = [];
for(let i = 0; i < 3; i++){
for(let j = 0; j < 3; j++){
let sum = Math.abs(x) + Math.abs(y);
let newR = this.r/3;
let b = new Box(this.x + i*newR,this.y + j*newR, newR);
if((i == 1 && j == 1)){
b.space = true;
}
if(!boxes[i]){
boxes[i] = [];
}
if(this.space == true){
b.space = true;
}
b.c = this.c;
boxes[i].push(b);
}
}
return boxes;
}
}