Skip to content

Instantly share code, notes, and snippets.

@m1el
Forked from anonymous/fractal.js
Created March 5, 2014 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m1el/9374161 to your computer and use it in GitHub Desktop.
Save m1el/9374161 to your computer and use it in GitHub Desktop.
// http://nolandc.com/sandbox/fractals/?x=25&y=1&l=8&d=6&sa=80&a=-45&s=F++F++F++F++&r=F,F+D---F++F,D,+F+D---F+D
// space-filling fractal, a modification of Sierpinski curve
function d2xy(d, s) {
var r = {x: 0, y: 0}, p = {x: 0, y: 0};
for (s = s-1; s >= 0; s--) {
p.x *= 2;
p.y *= 2;
var pow = 1 << (s*2);
rot(r, (d & pow*3) >> s*2);
if(s==1){
d = (d + 2) % pow;
} else if(s > 1) {
d = (d + pow / 2 + pow / 8) % pow;
}
p.x += r.x;
p.y += r.y;
}
return p;
}
function rot(p, q) {
q = q % 4;
if (q & 1) {
var tmp = p.x;
p.x = p.y;
p.y = 1 - tmp;
}
if (q & 2) {
p.x = 1 - p.x;
p.y = 1 - p.y;
}
}
var a = [],
pow = 6,
s = Math.pow(2, pow),
scale = 10;
for (var i = 0; i < s * s; i++) {
var p = d2xy(i, pow);
a.push(p.x *scale + ' ' + p.y * scale);
}
document.body.innerHTML = '<svg height=' + s*scale + ' width=' + s*scale + '>' +
'<polygon stroke=black fill=none points="' + a.join(' ') + '"></svg>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment