
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation. Please see the index.html
// for more details.

function EyeMovement(radius)
{
	function cartesianToPolar(x ,y)
	{
		return {
			r: Math.sqrt(x * x + y * y),
			phi: Math.atan2(y, x)
		};
	}

	function polarToCartesian(r, phi)
	{
		return {
			x: r * Math.cos(phi),
			y: r * Math.sin(phi)
		};
	}

	this.calculate = function(face, eye, event)
	{
		if (typeof event == "undefined")
		{
			return { x: 0, y: 0 };
		}

		mouseX = event.clientX - face.offsetX - eye.offsetX;
		mouseY = event.clientY - face.offsetY - eye.offsetY;

		var polar = cartesianToPolar(mouseX, mouseY);

		if (polar.r > radius)
		{
			polar.r = radius;
		}

		var cartesian = polarToCartesian(polar.r, polar.phi);

		return {
			x: Math.round(cartesian.x),
			y: Math.round(cartesian.y)
		};
	}
}


