2025-05-04 13:00:58 +00:00
|
|
|
|
/*
|
|
|
|
|
Duration: 52 seconds.0:52
|
|
|
|
|
Hilbert curve at its sixth iteration
|
|
|
|
|
|
|
|
|
|
Alphabet : A, B
|
|
|
|
|
Constants : F + −
|
|
|
|
|
Axiom : A
|
|
|
|
|
Production rules:
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
console.clear();
|
|
|
|
|
|
|
|
|
|
const rules = {
|
|
|
|
|
"A": "+BF-AFA-FB+",
|
|
|
|
|
"B": "-AF+BFB+FA-",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let steps = 5;
|
|
|
|
|
|
|
|
|
|
let s = "A";
|
|
|
|
|
for(let i=0;i<steps;i++) {
|
|
|
|
|
let ns = "";
|
|
|
|
|
for(let c of s) {
|
|
|
|
|
ns += rules[c] || c;
|
|
|
|
|
}
|
|
|
|
|
s = ns;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let [x,y] = [0,0];
|
|
|
|
|
const m = 2**(-steps);
|
|
|
|
|
let dir = [m,0];
|
|
|
|
|
let d = 'M 0 0 ';
|
|
|
|
|
const width = 1.5 * 2**-(steps+1);
|
|
|
|
|
|
|
|
|
|
const dots = [];
|
|
|
|
|
|
|
|
|
|
for(let c of s) {
|
|
|
|
|
const [dx,dy] = dir;
|
|
|
|
|
switch(c) {
|
|
|
|
|
case '+':
|
|
|
|
|
dir = [-dy,dx];
|
|
|
|
|
break;
|
|
|
|
|
case '-':
|
|
|
|
|
dir = [dy,-dx];
|
|
|
|
|
break;
|
|
|
|
|
case 'F':
|
|
|
|
|
x += dx;
|
|
|
|
|
y += dy;
|
|
|
|
|
d += `l ${dx} ${dy}`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const length = 2**steps - 1;
|
|
|
|
|
|
|
|
|
|
document.getElementById('curve').setAttribute('stroke-width',width);
|
|
|
|
|
document.getElementById('curve').setAttribute('d',d);
|
|
|
|
|
document.getElementById('dots').setAttribute('stroke-width',width*0.4);
|
|
|
|
|
document.getElementById('dots').setAttribute('d',d);
|
|
|
|
|
document.getElementById('dots').setAttribute('stroke-dasharray',`0.0000001 ${2*m}`);
|
|
|
|
|
|
|
|
|
|
function easeInOutSine(x) {
|
2025-05-04 13:04:57 +00:00
|
|
|
|
return -(Math.cos(Math.PI * x) - 1) / 2;
|
2025-05-04 13:00:58 +00:00
|
|
|
|
}
|
2025-05-04 13:04:57 +00:00
|
|
|
|
|
2025-05-04 13:00:58 +00:00
|
|
|
|
function frame() {
|
|
|
|
|
const period = 1000;
|
|
|
|
|
const t = (new Date()-0)/period % 1000;
|
2025-05-04 13:04:57 +00:00
|
|
|
|
document.getElementById('dots').setAttribute('stroke-dashoffset',(t)*m);
|
2025-05-04 13:00:58 +00:00
|
|
|
|
requestAnimationFrame(frame);
|
|
|
|
|
}
|
|
|
|
|
frame();
|