Skip to content

Instantly share code, notes, and snippets.

@uhho
Created June 27, 2014 04:57
Show Gist options
  • Save uhho/dddd61edc0fdfa1c28e6 to your computer and use it in GitHub Desktop.
Save uhho/dddd61edc0fdfa1c28e6 to your computer and use it in GitHub Desktop.
Two-dimensional Gaussian function in JavaScript
/**
* Two-dimensional Gaussian function
*
* @param {number} amplitude
* @param {number} x0
* @param {number} y0
* @param {number} sigmaX
* @param {number} sigmaY
* @returns {Function}
*/
function makeGaussian(amplitude, x0, y0, sigmaX, sigmaY) {
return function(amplitude, x0, y0, sigmaX, sigmaY, x, y) {
var exponent = -(
( Math.pow(x - x0, 2) / (2 * Math.pow(sigmaX, 2)))
+ ( Math.pow(y - y0, 2) / (2 * Math.pow(sigmaY, 2)))
);
return amplitude * Math.pow(Math.E, exponent);
}.bind(null, amplitude, x0, y0, sigmaX, sigmaY);
}
// USAGE
var gaussian = makeGaussian(300, 200, 200, 50, 50);
console.log(gaussian(100,100)); // ~5
console.log(gaussian(200, 200)); // 300
@RepComm
Copy link

RepComm commented Jun 2, 2020

I love it! I'm using it an a modular kernel filter system to implement gaussian blur.
Here's my contribution for better intellisense:

/**@author https://gist.github.com/uhho
 * @gist https://gist.github.com/uhho/dddd61edc0fdfa1c28e6
 * Two-dimensional Gaussian function
 * @param {number} amplitude
 * @param {number} x0
 * @param {number} y0
 * @param {number} sigmaX
 * @param {number} sigmaY
 * @returns {callback}
 * @callback callback
 * @param {number} u
 * @param {number} v
 * @returns {number}
 */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment