JSFiddle - React, Tailwind, and code Playground

by jcubed111

JavaScript

/* Randomize array in-place using Durstenfeld shuffle algorithm */
function shuffleArray(array) {
    for (var i = array.length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}


function guass(x) {
	// std = 3
	return Math.exp(-(x**2 / 18));
}


function rand(r) {
	return (Math.random() - 0.5) * 2 * r;
}



let samples = Array(10000).fill(0)
	.map(i => [rand(10), rand(10)])
    .map(([x, y]) => [x, y, guass(x) * guass(y)])
    .filter(([x, y, v]) => v > 0.05);
    
shuffleArray(samples);

samples = samples.slice(0, 50);
samples.push([0, 0, 1.0]);
samples.sort(([,,av], [,,bv]) => bv - av);
let sum = samples.map(([x, y, v]) => v).reduce((a, b) => a + b);
let prog = samples
    .map(([x, y, v]) => `\n+ texture(renderSampler, samplePos + vec2(${x.toFixed(5)}, ${y.toFixed(5)}) / imageSize) * ${(v/sum).toFixed(5)}`)
    .join('');
console.log(prog);