finding angle on mouse click (trigonometry)
in
Programming Questions
•
2 years ago
Hi,
I'm trying to solve this question:
I would find the exact angle on mouse click in order to generate an ellipse that moves in a circle,
I wrote this code based on the idea suggested in "Processing" by Reas and Fry (pg 123).
It works "quarter a way", because of if I click on the top-left quarter of the screen
the ellipse starts moving in circle counter-clockwise,
clicking in the bottom-right it moves in circle clockwise,
clicking on the top-right the ellipse moves in ellipse clockwise,
clicking on the bottom-left it moves in ellipse counter-clockwise.
I can't figure out what's the problem, I guess i miss some trigonometry knowledges... :(
I would appreciate if someone could point me in the right way.
Here's the code:
I'm trying to solve this question:
I would find the exact angle on mouse click in order to generate an ellipse that moves in a circle,
I wrote this code based on the idea suggested in "Processing" by Reas and Fry (pg 123).
It works "quarter a way", because of if I click on the top-left quarter of the screen
the ellipse starts moving in circle counter-clockwise,
clicking in the bottom-right it moves in circle clockwise,
clicking on the top-right the ellipse moves in ellipse clockwise,
clicking on the bottom-left it moves in ellipse counter-clockwise.
I can't figure out what's the problem, I guess i miss some trigonometry knowledges... :(
I would appreciate if someone could point me in the right way.
Here's the code:
- /* from Processing by Reas and Fry (pg 123)
float a = HALF_PI;
float c = cos(a);
float ac = acos(c);
// Prints "3.1415927 : -1.0 : 3.1415927"
println(a + " : " + c + " : " + ac);*/
float a= 100, b=100, raggio=30;
void setup() {
size(200,200);
smooth();
}
void draw() {
background(120);
stroke(0);
line(width/2,0,width/2,height);
line(0,height/2,width,height/2);
noStroke();
fill(0);
ellipse(cos(a)*raggio+width/2,sin(b)*raggio+height/2,10,10);
a=a+PI/100.0;
b=b+PI/100.0;
}
void mousePressed() {
raggio=dist(mouseX,mouseY,width/2,height/2);
float xT=((mouseX-width/2)/dist(width/2,height/2,mouseX,mouseY));
float yT=((mouseY-height/2)/dist(width/2,height/2,mouseX,mouseY));
a=acos(xT);
b=asin(yT);
println("the value of a is :"+a+" the value of b is "+b);
}
1