/**
 * Created by kelvinzhang on 5/29/17.
 */


var canvas;
var bgCanvas;

var robotX;
var robotY;

var senseRadius = 150;

var nLasers = 36;

var robotDir = 0;
var dirOffset = 0;

var sensorNoise = 0.1;

//Results of laser scan
var z = new Array(nLasers);

var sensorModel;

function update()
{
    //Only scan if location changed
    scan(robotX, robotY, robotDir, senseRadius, map, z);
    var checkbox = document.getElementById('shouldColor');
    checkbox.checked = false;
	clearColor();
}

function drawLaserLines(ctx)
{
    //Angle between each laser, in radians
    var rad = Math.PI*2/nLasers;

    //index of corresponding entry in z
    var i = (dirOffset - nLasers/4);
    i = (i+nLasers+nLasers);

    for(var index = 0; index <= nLasers/2; index ++, i++)
    {
        //dirOffset is the direction the user's mouse
        //is point at, -nLasers/4 to offset it by 90 degrees,
        //so that laser scan is centered at where the user
        //points to

        //get i stay in bound
        i %= nLasers;

        var dir = i * rad;
        var laserLen = senseRadius;

        //Grey color for a miss
        if(z[i] >= senseRadius)
        {
            ctx.strokeStyle = 'grey';
        }else
        {
            //distance reported by the laser sensor
            var dist = z[i] + gaussian()*sensorNoise;
            laserLen = min(laserLen, dist);

            //red for a hit
            ctx.strokeStyle = 'red';
            ctx.fillStyle = 'red';
            ctx.beginPath();
            ctx.circle(robotX + cos(dir)*dist, robotY + sin(dir)*dist, 5);
            ctx.fill();
        }
        ctx.strokeLine(robotX, robotY, robotX + cos(dir)*laserLen, robotY + sin(dir)*laserLen)
    }
}

function drawRobot(ctx)
{
    ctx.strokeStyle = 'black';
    ctx.drawRobot(robotX, robotY, robotDir, 20);
}

function parameterChanged(event)
{
    var target = event.target || event.srcElement;

    if(target.id === 'sensorRadius')
    {
        senseRadius = Number(target.value) / 0.02;
    }
    else if(target.id === 'sensorNoise')
    {
        sensorNoise = Number(target.value);
        sensorModel.a1 = sensorNoise;
    }
    else if(target.id === 'nLasers')
    {
        nLasers = Number(target.value) * 2;
        z = new Array(nLasers);
    }
}

function trackRobotDir(event)
{
    var coor = getClickLoc(event);
    var x = coor.x;
    var y = coor.y;

    robotDir = atan2(y-robotY, x-robotX);
    dirOffset = round(robotDir/Math.PI/2 * nLasers);
    clearCanvas(canvas);
    canvas.getContext('2d').drawRobot(robotX, robotY, robotDir, 20);
}

function mouseDown(event)
{
	var coor = getClickLoc(event);
	var x = coor.x;
	var y = coor.y;

	//Do nothing if it is not a left button event
	if(event.altKey)
    {
    	clearCanvas(canvas);
		var ctx = canvas.getContext('2d');
		drawRobot(ctx);
		drawLaserLines(ctx);
		ctx.strokeStyle = 'rgba(0, 0, 255, 0.5)';
		ctx.drawRobot(x, y, robotDir, 5);
		var probability = sensorModel.probability(z, new RobotState(x, y, robotDir));
		document.getElementById('probability').innerHTML = "Probability: " + probability;
		return;
	}

    robotX = x;
    robotY = y;
    clearCanvas(canvas);
    drawRobot(canvas.getContext('2d'));
    bgCanvas.onmousemove = trackRobotDir;
    bgCanvas.onmouseout = mouseUp;
	bgCanvas.onmouseup = mouseUp;
}

function mouseUp()
{
    bgCanvas.onmousemove = undefined;
    bgCanvas.onmouseout = undefined;
    bgCanvas.onmouseup = undefined;
    update();
    drawLaserLines(canvas.getContext('2d'));
}

function init()
{
    canvas = document.getElementById('canvas');
    bgCanvas = document.getElementById('background');

    //Draw the map
    var map = getMapForCanvas(canvas);
    bgCanvas.getContext('2d').drawMap(map);
    robotX = floor(random()*canvas.width);
    robotY = floor(random()*canvas.height);
    robotDir = random()*TWO_PI;
    dirOffset = round(robotDir/Math.PI/2 * nLasers);

    update();
    drawRobot(canvas.getContext('2d'));
    drawLaserLines(canvas.getContext('2d'));

	sensorModel = new BeamModel(getSensorNoise(), getSensorRadius(), map,
		canvas.width, canvas.height);

    Particle.size = 5;

    //Listen to mouse click events
    bgCanvas.onmousedown = mouseDown;
    senseRadius = getSensorRadius();
    sensorNoise = getSensorNoise();
}

function getSensorNoise()
{
	return getValue('sensorNoise');
}

function getSensorRadius()
{
	return getValue('sensorRadius')/0.02;
}

function toggleColoring(event)
{
	var target = event.target || event.srcElement;
	if(target.checked)
	{
		colorMap();
	}else
	{
		clearColor();
	}
}

function colorMap()
{
	var ctx = bgCanvas.getContext('2d');
	var resolution = getValue('colorRes');
	var probs = new Array(Math.ceil(bgCanvas.height/resolution));
	var sum = 0;
	var max = 0;
	for(var i = 0; i < probs.length; i ++)
	{
		probs[i] = new Array(Math.ceil(bgCanvas.width/resolution));
		for(var j = 0; j < probs[i].length; j ++)
		{
			var p = sensorModel.probability(z, new RobotState(j*resolution + resolution/2, i*resolution + resolution/2, robotDir));
			console.assert(p >= 0 && p < 1);
			sum += p;
			max = Math.max(p, max);
			probs[i][j] = p;
		}
	}

	max /= sum;

	for(var i = 0; i < probs.length; i ++)
	{
		for(var j = 0; j < probs[i].length; j ++)
		{
			var p = probs[i][j] / max;
			console.log(p);
			ctx.fillStyle = 'rgba(' + round(p*255) + ', 0, ' + (255-round(p*255)) + ', 0.5)';
			ctx.fillRect(j*resolution, i*resolution, resolution, resolution);
		}
	}
}

function clearColor()
{
	clearCanvas(bgCanvas);
	bgCanvas.getContext('2d').drawMap(map);
}
@@ -5129,16 +5129,19 @@
 %09%7D%0A%09%7D%0A%0A%09
+// 
 max /= s

comment redundant code