/**
 * Created by kelvinzhang on 6/26/17.
 */

function SensorDemo(id, sensorNoise)
{
	const scale = 50;
	this.view = new View(document.getElementById(id), scale);
	this.ctx = this.view.canvas.getContext('2d');
	this.robotSize = 0.2;
	this.nBuckets = 100;

	//create an 0 filled array of size this.nBuckets
	this.buckets = Array.apply(null, new Array(this.nBuckets)).map(Number.prototype.valueOf,0);
	this.maxCount = 0;

	//Demo parameters
	// this.sensorRadius = sensorRadius;
	this.sensorNoise = sensorNoise;

	this.actualDistance = this.view.width/2;

	this.draw();
}

SensorDemo.prototype.sample = function(n)
{
	this.ctx.strokeStyle = 'green';
	for(var i = 0; i < n; i ++)
	{
		var reading = this.actualDistance + gaussian()*this.sensorNoise;
		// this.draw();
		// this.ctx.strokeLine(reading, 0, reading, this.view.height);
		// if(reading > this.sensorRadius)reading = this.sensorRadius;
		var index = round(reading/this.view.width * this.nBuckets);
		this.buckets[index]++;
		this.maxCount = max(this.maxCount, this.buckets[index]);
	}
	this.draw();
};

SensorDemo.prototype.draw = function()
{
	const ctx = this.ctx;

	clearCanvas(this.view.canvas);

	//Draw the actual distance
	ctx.strokeStyle = 'red';
	ctx.strokeLine(this.actualDistance, 0, this.actualDistance, this.view.height);

	//Draw sensor readings
	ctx.strokeStyle = 'grey';
	var x = 0;
	const step =  1.0/this.buckets.length * this.view.width;
	for(var i = 0; i < this.buckets.length; i ++, x+=step)
	{
		var count = this.buckets[i];
		ctx.strokeLine(x+step/2, 0, x+step/2, count*1.0/this.maxCount*this.view.height);
	}
};
@@ -614,73 +614,301 @@
 %0A%7B%0A%09
+if(
 this.
-ctx.strokeStyle = 'green';%0A%09for(var i = 0; i %3C n; i ++)%0A%09%7B%0A%09
+readingsLeft %3E 0)%0A%09%09return;%0A%09this.readingsLeft = n;%0A%09const self = this;%0A%09this.intervalId = window.setInterval(function()%7Bself.takeSingleReading();%7D, 100);%0A%7D;%0A%0ASensorDemo.prototype.takeSingleReading = function()%0A%7B%0A%09if(this.readingsLeft-- %3C= 0)%0A%09%09window.clearInterval(this.intervalId);%0A%0A
 %09var
@@ -970,20 +970,16 @@
 Noise;%0A%09
-%09// 
 this.dra
@@ -984,20 +984,75 @@
 raw();%0A%09
-%09// 
+this.ctx.lineWidth *= 3;%0A%09this.ctx.strokeStyle = 'green';%0A%09
 this.ctx
@@ -1104,16 +1104,41 @@
 ight);%0A%09
+this.ctx.lineWidth /= 3;%0A
 %09// if(r
@@ -1193,17 +1193,16 @@
 Radius;%0A
-%09
 %09var ind
@@ -1254,17 +1254,16 @@
 ckets);%0A
-%09
 %09this.bu
@@ -1278,17 +1278,16 @@
 dex%5D++;%0A
-%09
 %09this.ma
@@ -1340,25 +1340,8 @@
 %5D);%0A
-%09%7D%0A%09this.draw();%0A
 %7D;%0A%0A

add animation to single sensor reading