Braço com 2 revolute joints

Algoritmo de Inverse Kinematics para um braço robótico com 2 revolute joints

$s1 = this.arm.seg01;
$s2 = this.arm.seg01.seg02;

pit = function($x,$y) {
	h = Math.pow($x,2) + Math.pow($y,2);
	h = Math.pow(h,0.5);
	return h
}

$m=150
$n=150

onMouseMove = function() {
	$e = arm._xmouse<0;
	$f = 1-2*$e;
	$g = $e*Math.PI;
	$h = pit(arm._xmouse,arm._ymouse);

	$x = arm._xmouse*Math.min($m+$n,$h)/$h;
	$y = arm._ymouse*Math.min($m+$n,$h)/$h;

	$a = $y/$x;
	$b = ($x*$x+$y*$y)/(2*$m*$n)-($m/$n+$n/$m)/2;
	$c = $n*Math.sin(Math.acos($b))/($m+$n*$b);

	$r2 = Math.acos($b);
	$r1 = -Math.atan($c)+Math.atan($a)-$g;

	$s1._rotation = $r1/Math.PI*180;
	$s2._rotation = $r2/Math.PI*180;

	trace([$s1._rotation,$s2._rotation]);
}