Skip to content

Instantly share code, notes, and snippets.

@pkerpedjiev
Created November 5, 2018 16:26
Show Gist options
  • Save pkerpedjiev/567cb2d2879d66f1e862d4d28c33f418 to your computer and use it in GitHub Desktop.
Save pkerpedjiev/567cb2d2879d66f1e862d4d28c33f418 to your computer and use it in GitHub Desktop.
AWS Lambda Mandelbrot Tiles Code
function tileset_info() {
return {
'min_pos': [-2.5, -2.5],
'max_pos': [2.5, 2.5],
'bins_per_dimension': 256,
'max_width': 5,
'max_zoom': 50,
'mirror_tiles': 'false',
};
}
function tile_bounds(tsinfo, z,x,y, width=1, height=1) {
const min_pos = tsinfo['min_pos'];
const max_pos = tsinfo['max_pos'];
const max_width = Math.max(max_pos[0] - min_pos[0],
max_pos[1] - min_pos[1]);
const tile_width = max_width / 2 ** z;
const from_x = min_pos[0] + x * tile_width;
const to_x = min_pos[0] + (x+width) * tile_width;
const from_y = min_pos[1] + y * tile_width;
const to_y = min_pos[1] + (y+height) * tile_width;
return [from_x, from_y, to_x, to_y];
}
function mandelbrot(from_x, from_y, to_x, to_y, grid_size, maxiter) {
const output = new Float32Array(grid_size * grid_size);
let step_x = (to_x - from_x) / grid_size;
let step_y = (to_y - from_y) / grid_size;
let creal = from_x;
let cimag = from_y;
for (let i = 0; i < grid_size; i++) {
cimag = from_y;
for (let j = 0; j < grid_size; j++) {
let nreal = 0;
let real = 0;
let imag = 0;
let n = 0;
for (n = 0; n < maxiter; n++) {
nreal = real*real - imag*imag + creal;
imag = 2 * real*imag + cimag;
real = nreal;
if (real * real + imag * imag > 4.0) {
break;
}
}
output[j * grid_size + i] = n;
cimag += step_y;
}
creal += step_x;
}
return output;
}
function mandelbrot_tile(z,x,y) {
const tsinfo = tileset_info();
const [from_x, from_y, to_x, to_y] = tile_bounds(tsinfo, z,x,y);
const tile_size = tsinfo['bins_per_dimension'] || 256;
const matrix = mandelbrot(from_x, from_y, to_x, to_y, 100, 500);
//console.log('matrix:', matrix);
return matrix;
}
/**
* Go through and generate data for each tile
*
* @param {string} tile_id Something like x.0.0.0 (identifier.z.x.y)
*
* @returns {obj} The data to be sent back {'x.0.0.0': {'dense': 'sdfkjdsfs'}}
*/
function process_tile(tile_id) {
const tile_parts = tile_id.split('.');
const l = tile_parts.length;
const y = parseInt(tile_parts[l-1]);
const x = parseInt(tile_parts[l-2]);
const z = parseInt(tile_parts[l-3]);
const data = mandelbrot_tile(z, x, y);
const base64 = Buffer(data.buffer).toString('base64');
console.log('base64', base64);
return {
'dense': base64,
'dtype': 'float32'
}
}
exports.handler = async (event) => {
// TODO implement
mandelbrot_tile(0,0,0);
const tiles = event.multiValueQueryStringParameters.d;
const ret = {};
for (let tile_id of tiles) {
ret[tile_id] = process_tile(tile_id);
}
const response = {
statusCode: 200,
body: JSON.stringify(ret),
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
};
return response;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment