13

If I have an image 720, 720 that looks like this..

enter image description here

How do I work out the angle of the touched x,y given that the center x and y are 360, 360 A lot of calculations I see for this assume the origin is 0,0 (which is top left) so I get incorrect results. I am assuming 0 is always to the top and not rotated.

2
  • What exactly did you find so far? If your problem is only about the coordinates of the center, we will be happy to help you! Jan 23, 2012 at 7:53
  • 1
    Traditionally, 0 degrees is to the right, 90 is up, 180 is left, and 270 is down. (I may misunderstand your phrasing.)
    – user1881400
    Jun 25, 2015 at 2:59

4 Answers 4

19

Here is the general formula:

angle = atan2(mouseY - cirleCenterY, mouseX - circleCenterX);
0
13

May be clearer this way:

(Math.toDegrees( Math.atan2(fromLeft - 360.0, 360.0 - fromTop) ) + 360.0) % 360.0

Adding a 360 degree turn and applying the modulo operator gives you the positive angle, which atan2 does not.

5
  • This seems the closest to any solution suggested here. I misunderstood in my previous comment, if I substitute x touch coordinate for fromLeft and y for fromTop I get a positive angle back at least. Clicking at around 3 o'clock does give around 90 degrees as expected but if I click at 6 0'clock I get close to 360 degrees which wasn't expected, 9 o'clock reports the correct 270 degrees. It looks like this is pretty close but still some problem with 12 and 6 o'clock in that calculation
    – Kyros
    Jan 23, 2012 at 16:57
  • If I click at 12 0'clock I get 180 and 6 o'clock I get 0 so these are somehow reversed.
    – Kyros
    Jan 23, 2012 at 17:13
  • Corrected from "fromTop - 360.0" to "360.0 - fromTop". Tested at JavaWIDE.
    – minopret
    Jan 24, 2012 at 1:40
  • 1
    Did you test this snippet? atan2 is defined (counterintuitively) as atan2(y, x) but you're passing (x, y)...
    – Bill
    Jul 13, 2017 at 12:33
  • @Bill Thanks. I agree now that it's worthwhile to state clearly the coordinate axes that are used in this question. They are not oriented as usual for x-y coordinates in mathematics. They are oriented as usual for Java2D, Postscript, and so on. That is why I named my parameters fromLeft and fromTop. In regard to testing: Yes, I think you'll agree now that the point 720 from left, 360 from top, is at 90 degrees; the point 360 from left, 720 from top, is at 180 degrees.
    – minopret
    Oct 18, 2017 at 7:49
0
java.lang.Math.atan2(y-360,x-360);
0
0

the screen coordinates don't go the way of the trigonometric ones.

use java.lang.Math.atan2(-(y-360),x-360);

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.