Skip to content

Instantly share code, notes, and snippets.

@nikomatsakis
Created September 13, 2012 21:16
Show Gist options
  • Save nikomatsakis/3717738 to your computer and use it in GitHub Desktop.
Save nikomatsakis/3717738 to your computer and use it in GitHub Desktop.
// Adapted from
//
// https://github.com/RiverTrail/RiverTrail/blob/master/examples/mandelbrot/mandelbrot.js
//
// which in turn is adapted from a WebCL implementation available at
//
// http://www.ibiblio.org/e-notes/webcl/mandelbrot.html
var nc = 30, maxCol = nc*3, cr,cg,cb;
// initialises the color map for translating Mandelbrot iterations
// into nice colors
function computeColorMap() {
var st = 255/nc;
cr = new Array(maxCol); cg = new Array(maxCol); cb = new Array(maxCol);
for (var i = 0; i < nc; i++){
var d = Math.floor(st*i);
cr[i] = 255 - d; cr[i+nc] = 0; cr[i+2*nc] = d;
cg[i] = d; cg[i+nc] = 255 - d; cg[i+2*nc] = 0;
cb[i] = 0; cb[i+nc] = d; cb[i+2*nc] = 255 - d;
}
cr[maxCol] = cg[maxCol] = cb[maxCol] = 0;
}
// this is the actual mandelbrot computation, ported to JavaScript
// from the WebCL / OpenCL example at
// http://www.ibiblio.org/e-notes/webcl/mandelbrot.html
function computeSetByRow(y) {
// "use strict";
// var result = [];
for (var x = 0; x < cols; x++) {
// var Cr = (x - 256) / scale + 0.407476;
// var Ci = (y - 256) / scale + 0.234204;
// var I = 0, R = 0, I2 = 0, R2 = 0;
// var n = 0;
// while ((R2+I2 < 2.0) && (n < 512)) {
// I = (R+R)*I+Ci;
// R = R2-I2+Cr;
// R2 = R*R;
// I2 = I*I;
// n++;
// }
// result[x] = n;
}
// return result;
return 0;
}
var scale = 10000*300;
var rows = 512;
var cols = 512;
computeColorMap();
var mandelbrot0 = new ParallelArray([rows], function(r) { return r; });
var mandelbrot1 = mandelbrot0.map(computeSetByRow);
// var mandelbrot2 = new ParallelArray([512,512], scale);
// writeResult(canvas, mandelbrot);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment