Created
July 10, 2018 20:34
-
-
Save carlcorder/e0fee149300321b60d2900776279862f to your computer and use it in GitHub Desktop.
Convert RLE to regl.framebuffer data
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
let decode = (rle) => rle.replace(/[\n\s]/g, '') | |
.replace(/(\d+)(\w)/g, (m, n, c) => new Array(parseInt(n) + 1).join(c)) | |
let decoder = (rle) => { | |
let rld = []; | |
rle.replace('!', '').split('$').forEach(s => rld.push(decode(s))) | |
return rld | |
} | |
// reg, green, blue, alpha | |
let rgbAlpha = (rld) => { // rld: run-length decoded | |
return rld.map(s => s.split('').map(c => ({ | |
'b': 0, | |
'o': 255 | |
} [c])).map(x => [x, x, x, x])) | |
.map(a => [].concat.apply([], a)) | |
} | |
let initialize = (rle, p = START_POSITION) => { // p: starting position | |
let rld = decoder(rle) | |
let rgba = rgbAlpha(rld) | |
let initialConditions = (Array(RADIUS * RADIUS * 4)).fill(0) | |
for (let i = 0; i < rgba.length; i++) { | |
for (let j = 0; j < rgba[i].length; j++) { | |
initialConditions[p - i * 4 * RADIUS + j] = rgba[i][j] | |
} | |
} | |
return initialConditions | |
} | |
const INITIAL_CONDITIONS = initialize(SIR_ROBIN) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment