Skip to content

Instantly share code, notes, and snippets.

@randompast
Last active March 21, 2017 03:21
Show Gist options
  • Save randompast/266276b1001ab6d3f42703ae4a335333 to your computer and use it in GitHub Desktop.
Save randompast/266276b1001ab6d3f42703ae4a335333 to your computer and use it in GitHub Desktop.
Had to do mandelbrot after some youtubes
//Inspiration via Mathologer: https://www.youtube.com/watch?v=leFep9yt3JY&t=12m53s
//Excitement via Two Minute Papers: https://www.youtube.com/watch?v=HvHZXPd0Bjs&list=PLujxSBD-JXgnqDD1n-V30pKtp6Q886x7e&index=93
document.title = "Mandlebrotch"
var fit = require('canvas-fit')
var canvas = document.body.appendChild(document.createElement('canvas'))
window.addEventListener('resize', fit(canvas), false)
var ctx = canvas.getContext('2d')
//z^2 + c
var f = function(z, c){ return [ z[0]*z[0] - z[1]*z[1] + c[0], 2*z[0]*z[1] + c[1] ] }
//|z|_2
var h = function(z){return Math.sqrt(z[0]*z[0] + z[1]*z[1])}
//extra fun/weird norm
//var h = function(z){return Math.sqrt(z[0]*z[0], z[1]*z[1])}
//iteration > 2 ? -> iteration
var g = function(c, n){
var z = f([0,0], c)
for(var i = 0; i < n; i++){
z = f(z, c)
if(h(z) > 2) return i
}
return i
}
var col = function(c, n){
//pure color
// return "hsl("+g(c,n)*10+",100%,50%)"
//playing with lightness/darkenss
return "hsl("+g(c,n)*10+",100%,"+(100-g(c,n)*8)+"%)"
}
//sizing m -> 526px
var m = 0.0076
for(var zx = -2; zx < 2; zx+=m){
for(var zy = -2; zy < 2; zy+=m){
ctx.fillStyle = col([zx, zy], 25)
ctx.fillRect((zx+2)/m,(zy+2)/m,1,1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment