Created
March 5, 2014 16:57
-
-
Save anonymous/9371353 to your computer and use it in GitHub Desktop.
space-filling fractal, a modification of Sierpinski curve
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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