Reputation: 1786
Interesting trig problem here...
I have a circle in the middle of an Android screen. I need to get the angle from the positive Y axis based upon an X,Y coordinate where the user touched the screen.
All my high school geometry is failing me at the moment. Any help would be appreciated.
Thank you!
Upvotes: 0
Views: 607
Reputation: 11073
If you touch the point x1, y1, and
xc = display.getWidth()/2;
yc = display.getHeight()/2;
Then atan2( y1 - yc, x1 - xc) will give you and answer in radians that corresponds to the angle "a" in the diagram.
The "positive y axis" as you refer to it, corresponds to an angle of 90degree, or pi/2 radians. If what you really want is "an angle from the positive y axis" then you need subtract pi/2 from your angle and take the absolute value (to just get the raw difference in angle between the y axis and you) using this equation:
angleInRadiansAwayFromYAxis = Math.abs(Math.atan2(y1 - yc, x1 - xc) - Math.PI/2);
If you want the absolute angle difference between angle b and a (so, pretending that the horizontal y axis is the 0 angle) You just need to subtract the angles:
angleInRadiansFromYAxis = Math.atan2(y1 - yc, x1 - xc) - Math.PI/2;
and if negative numbers bother you, you can always at Math.PI * 2 to any negative results to get a positive number between 0 and pi*2.
Upvotes: 1