xxxxxxxxxx
94
let buf1;
let buf2;
function drawShape(shp) {
for (let reg of shp.regions) {
beginShape();
for (let p of reg) {
vertex(p[0], p[1]);
}
endShape(CLOSE);
}
}
function drawGrid(levs, cx, cy, dx, dy, bds, cang, sang, b, db) {
function samp(s, t) {
return [cx + dx * s - dy * t, cy + dx * t + dy * s];
}
let ndx = cang * (dx / 3) - sang * (dy / 3);
let ndy = sang * (dx / 3) + cang * (dy / 3);
for (let y = 0; y < 5; ++y) {
for (let x = 0; x < 5; ++x) {
const pgon = {
regions: [
[
samp(x - 2.5, y - 2.5),
samp(x - 2.5, y - 1.5),
samp(x - 1.5, y - 1.5),
samp(x - 1.5, y - 2.5),
],
],
inverted: false,
};
const ipgon = PolyBool.intersect(pgon, bds);
if (ipgon.regions.length > 0) {
const sp = samp(x - 2, y - 2);
const m = 2*((x+y+10)%2) - 1;
const g = b + m*db;
if (levs > 0) {
drawGrid(levs - 1, sp[0], sp[1], ndx, ndy, ipgon, cang, sang, g, db/3 );
}
strokeWeight(mag(dx,dy)/20.00001);
if( levs == 0 ) {
fill( g );
} else {
noFill();
}
drawShape(ipgon);
}
}
}
}
function setup() {
createCanvas(512,512);
noFill();
}
function draw() {
background( 255 );
const all = {
regions: [
[
[5, 5],
[width-5, 5],
[width-5, height-5],
[5, height-5],
],
],
inverted: false,
};
const ang = map( frameCount%100, 0, 100, 0, HALF_PI );
const c = cos(ang);
const s = sin(ang);
drawGrid(2, width / 2, height / 2, width / 3, 0, all, c, s, 160, 60);
strokeWeight(5);
drawShape(all);
}
function keyPressed()
{
if( key == 'g' ) {
loop();
saveGif("output", 100, { units: "frames", delay: 0 });
}
}