createCanvas(windowWidth, windowHeight);
ellipse(width/2, height/2, 2*maxRadius, 2*maxRadius);
rect(0, height/2, width, height);
if (mouseX > width/2) angle = 90;
angle = atan((width/2-mouseX) / (min(height/2, mouseY)-height/2));
x = min(maxRadius*sin(angle), mouseX-width/2);
x = max(maxRadius*sin(angle), mouseX-width/2);
var y = min(max(0, height/2-mouseY), maxRadius*cos(angle));
text("angle (deg): " + angle, 10, 30);
text("x: " + x, 10, 100);
text("y: " + y, 10, 130);
translate(width/2, height/2);
line(0,0,maxRadius*sin(angle), -maxRadius*cos(angle));
a1 += (a1Target - a1)/20;
a2 += (a2Target - a2)/20;
text("a1: " + a1, 10, 170);
text("a2: " + a2, 10, 200);
text("err: " + posError, 10, 260);
translate(width/2, height/2);
line(0,0,l1*sin(a1), -l1*cos(a1));
translate(l1*sin(a1), -l1*cos(a1));
line(0,0,l2*sin(a1+a2), -l2*cos(a1+a2));
function estimateAngles(x,y){
var r = createVector(x,y);
var minErr = Number.MAX_VALUE;
for(var t1=-90; t1<=90; t1++){
var v1 = createVector(l1*sin(t1), l1*cos(t1));
for(var t2=-180; t2<=180; t2++){
var v2 = createVector(v1.x + l2*sin(t1+t2), v1.y + l2*cos(t1+t2));
var d = p5.Vector.sub(r,v2).mag();