简体   繁体   中英

Making a simple Javascript version of a gaussian distribution

I am trying to draw a series of rectangles (or lines) that follow the height of a gaussian curve. I will need to tweak the distribution manually, because I want to accomplish the very specific curve attached - where it goes up to a spike and back down - reference image here Or is there an easier equation to just start small, get large, then small again?

Based on the gaussian curve function I have tried this:

var a = 10;
var b = 10;
var x = 20;
var c = 10;

for (var j=0; j< 15; j++){
r.rect(100+j*12, 10, 6, 10*j*((a * Math.E) - (Math.pow(j-b),2))/(Math.pow(c,2)))
.fill(0)
}

if the r.rect looks weird, it's from the rune.js library.

Your math is incorrect. Here is what you wrote in standard mathematical notation:

在此处输入图片说明

I'm not familiar with rune.js, however, here's a simple implementation of a Gaussian curve in pure JS.

 var a = 100; var b = 25; var c = 10; var cv = document.getElementById('c'); var ctx = cv.getContext("2d"); for (var j=0; j< 150; j++){ var y = a/Math.pow(Math.E, (Math.pow(jb, 2))/(2*c*c)); ctx.rect(j*10, cv.height-y, 10, y); ctx.stroke(); } 
 <canvas id='c' height='200' width='500'></canvas> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM