xxxxxxxxxx
151
let s = 400;
function srect(x1,y1,x2,y2) {
rect(s*x1,s*y1,s*x2,s*y2);
}
function fwmin(xmax,xmin,nrectmax,nrect) {
return ( 1/3.6/sqrt(nrectmax/(nrect+1)) );
}
function fwmax(xmax,xmin,nrectmax,nrect) {
return ( (xmax - xmin) * sqrt((1+5)/(nrectmax-nrect+10)) );
}
function setup() {
createCanvas(400, 400);
background(220);
rectMode(CORNERS);
let nrectmax = 12;
let nrect = 1;
//List of available spaces
let free_rectsx = [[0,1]];
let free_rectsy = [[0,1]];
let free_rectsa = [1];
//List of empty indices on the array
//after deletion of a free rect
let empty_indices = [];
//List of rects
//list of rects (later tree)
let rectsx = [];
let rectsy = [];
let ir;
for( ir = 0; ir < 3*nrectmax ; ir++ ) {
nrect = 0;
for( let l = 0; l < rectsx.length; l++ ) {
if( !rectsx[l] ) {
nrect++;
}
}
if (nrect >= nrectmax) {
break;
}
//Region-based sampling: we choose to
//build in a random free rectangle
//Almost equal volume sampling: we chose to
//build a room in a region according
//to its area^2 (does not account multi-reg)
let i = floor(random(0,free_rectsx.length));
let count = 0;
while ( !free_rectsx[i] ) {
i = floor(random(0,free_rectsx.length));
count++;
if (count == 10) {
break;
}
}
if ( free_rectsx[i] == null) { break; }
let xmin = free_rectsx[i][0];
let xmax = free_rectsx[i][1];
let ymin = free_rectsy[i][0];
let ymax = free_rectsy[i][1];
let wmin = fwmin(xmax,xmin,nrectmax,nrect);
let wmax = fwmax(xmax,xmin,nrectmax,nrect);
let hmin = fwmin(ymax,ymin,nrectmax,nrect);
let hmax = fwmax(ymax,ymin,nrectmax,nrect);
if (wmax < 0 || wmin > wmax) {
continue;
}
if (hmax < 0 || hmin > hmax) {
continue;
}
let w = random(wmin,wmax);
let h = random(hmin,hmax);
let x1 = xmin + random(0,wmax-w);
let y1 = ymin + random(0,hmax-h);
let x2 = x1 + w;
let y2 = y1 + h;
rectsx.push([x1,x2]);
rectsy.push([y1,y2]);
srect(x1,y1,x2,y2);
// Create (up to 8) new free
// spaces by subdivision
let new_rectsx = [];
let new_rectsy = [];
let new_rectsa = [];
let rx = [xmin,x1,x2,xmax];
let ry = [ymin,y1,y2,ymax];
for (let ix = 0; ix < 3; ix++) {
for (let iy = 0; iy < 3; iy++) {
let rx1, rx2, ry1, ry2;
rx1 = rx[ix];
rx2 = rx[ix+1];
ry1 = ry[iy];
ry2 = ry[iy+1];
let wx = (rx2-rx1);
let wy = (ry2-ry1);
let a = wx*wy;
if((ix == 1) && (iy == 1)) {
continue;
}
if ( wx < fwmax(rx2,rx2,nrectmax,nrect) ) {
continue;
}
if ( wy < fwmax(ry2,ry2,nrectmax,nrect) ) {
continue;
}
if (a < (1/32*nrect)) {
continue;
}
fill(230);
srect(rx1,ry1,rx2,ry2);
fill(255);
new_rectsx.push([rx1,rx2]);
new_rectsy.push([ry1,ry2]);
new_rectsa.push(a);
}
}
//TODO: why null free_rects persist?
//and eil doesn't stay near 0?
console.log("ir:", ir);
for( let l = 0; l < free_rectsx.length; l++ ) {
console.log("frx:", free_rectsx[l][0], free_rectsx[l][1]);
}
console.log("frl:", free_rectsx.length);
free_rectsx[i] = null;
free_rectsy[i] = null;
free_rectsa[i] = null;
empty_indices.push(i);
console.log("eil:",empty_indices.length);
for( let l = 0; l < empty_indices.length; l++ ) {
console.log("ei:", empty_indices[l]);
}
for (let inr = 0; inr < new_rectsx.length; inr++) {
let ifr = free_rectsx.length;
if( empty_indices.length > 0 ) {
ifr = empty_indices.pop();
}
free_rectsx[ifr] = new_rectsx[inr];
free_rectsy[ifr] = new_rectsy[inr];
free_rectsa[ifr] = new_rectsa[inr];
}
}
console.log("ir:",ir);
//return;
}
function draw() {
}