A javascript implementation of the Mandelbrot set using d3.
Last active
November 21, 2015 00:49
-
-
Save easadler/74c24d8b5da0bf80b918 to your computer and use it in GitHub Desktop.
Mandelbrot Set
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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var canvas = d3.select('body') | |
.append('canvas') | |
.attr('width', width) | |
.attr('height', height) | |
.node().getContext("2d"); | |
canvas.fillStyle = "rgb(0, 0, 0)"; | |
canvas.fillRect(-1, -1, width, height); | |
var color = d3.scale.category20c() | |
var x = d3.scale.linear().domain([0,width]).range([-1.7,1]) | |
var y = d3.scale.linear().domain([0,height]).range([-1,1]) | |
for(var i = 0; i < width; i++){ | |
for( var j = 0; j < height; j++){ | |
var Z = {re: x(i), im: y(j)} | |
C = Z | |
for(var k = 0; k < 120; k++){ | |
re_k = Z.re *Z.re - Z.im * Z.im + C.re; | |
im_k = 2 * Z.re * Z.im + C.im; | |
Z = {re: re_k, im: im_k} | |
if(Math.pow(Z.re,2) + Math.pow(Z.im,2) >= 4){ | |
canvas.fillStyle = color(k) | |
canvas.fillRect(i,j,1,1) | |
break; | |
} | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment