xxxxxxxxxx
92
let quadtree;
function setup() {
createCanvas(800, 800);
translate(0,0);
noFill();
let w = 400;
let h = 400;
// NW // NE // SW // SE
quadtree = [2,0,0,0,
3,0,0,0,3,0,0,0,3,0,0,0,
4,0,0,0,4,0,0,0,4,0,0,0,4,0,0,0,
2,0,0,0,
2,0,0,0]
// # draw quadtree
let cursor = createVector(0,0); // top left of blocks of 4 rectangles
let context = 2;
let qid = -1 ;
let retenue = [];
for(let i = 0 ; i < quadtree.length ; i+=4){
strokeWeight(0.5 + i / 10); // # graphics
stroke(0,10*i,20*i);
let subdiv = quadtree[i] - 1;
let qwidth = w/2**subdiv; // cell width
let qheight = h/2**subdiv; // cell height
if(context == quadtree[i]){ // no subdivision
qid += 1; // we move forward to next quadrant id
}
else{ // there is a subdivision
retenue.push([context, cursor, qid+1]) // put in retenue the rect that we are subdividing
print(retenue);
cursor = getCursor(context, cursor, qid+1) // qid + 1 of old cursor and old context
context = quadtree[i];
qid = 0;
print(cursor)
}
getRect(context, cursor, qid);
if(qid == 3){
// GET ONLY THE LAST OF THE RETENUE
context = retenue[0][0];
cursor = retenue[0][1];
qid = retenue[0][2];
// THEN POP THE LAST ELEMENT OF THE RETENUE
}
}
function getRect(context, cursor, qid){
let rectw = w/2**(context - 1); // context - 1 is the number of subdivisions
let recth = h/2**(context - 1);
let x = 0;
let y = 0;
if(qid == 1 || qid == 3) { x = rectw; }
if(qid > 1) { y = recth; }
let rectx = cursor.x + x;
let recty = cursor.y + y;
rect(rectx,recty,rectw, recth);
}
function getCursor(context, cursor, qid){
let rectw = w/2**(context - 1); // context - 1 is the number of subdivisions
let recth = h/2**(context - 1);
let x = 0;
let y = 0;
if(qid == 1 || qid == 3) { x = rectw; }
if(qid > 1) { y = recth; }
let rectx = cursor.x + x;
let recty = cursor.y + y;
let newCursor = createVector(rectx,recty);
return newCursor;
}
}
function draw() {
//background(220);
}