Skip to content

Instantly share code, notes, and snippets.

@mfm24 mfm24/mandelbrot_server.js Secret
Last active Aug 29, 2015

Embed
What would you like to do?
Mandelbrot server in node.js
// mfm 2014-06-07 how fast is mandelbrot in node.js?
// NB this is no multithreaded, not sure how we'd share
// cache across processes, one way might be to have a single
// thread serving pages, and the others serving images??
// for the moment we'll keep single threaded
var PNG = require('node-png').PNG,
fs = require('fs'),
http = require('http');
function make_mandel(w, h, iters, sx, ex, sy, ey)
{
var p = new PNG({
filterType: -1,
width: w,
height: h
})
var dx = (ex-sx) / p.width
var dy = (ey-sy) / p.height
console.log(dx, dy)
var fy=sy;
for(var j=0; j<p.height; j++) {
var fx=sx;
for (var i=0; i<p.width; i++) {
var re=fx, im=fy
var v = 0;
for( ; v<iters && (re * re + im * im) < 4; v++) {
var ore = re
re = fx + re * re - im * im;
im = fy + 2 * ore * im;
}
v = iters-v // if we reach max iters this is always 0
var idx = (j*p.width + i) << 2
p.data[idx] = v
p.data[idx+1] = v
p.data[idx+2] = v
p.data[idx+3] = 0xFF
fx += dx
}
fy += dy
}
return p
}
var cache = {}
http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'image/png'});
var ret = cache[req.url]
if (!ret)
{
var parts = req.url.split("/"),
sy = parseFloat(parts[1]),
sx = parseFloat(parts[2]),
ey = parseFloat(parts[3]),
ex = parseFloat(parts[4]),
max_iter = parseFloat(parts[5]);
console.log("Requesting "+sx+", "+ex+", "+sy+", "+ey+", "+max_iter)
var p = make_mandel(256, 256, max_iter, sx, ex, sy, ey);
var bufs=[]
p.pack().on('data', function(data) {
bufs.push(data)
}).on('end', function(data) {
if(data)
bufs.push(data)
ret = cache[req.url] = Buffer.concat(bufs)
res.end(ret)
})
}
else
res.end(ret)
}).listen(8080, '127.0.0.1');
console.log('Server running at http://127.0.0.1:8080/');
//var p = make_mandel(256, 256, 64, -1.0, 1.0, -1.0, 1.0)
//p.pack().pipe(fs.createWriteStream('out.png'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.