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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Kyros
  • 459
  • 2
  • 5
  • 11
  • 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! – Jean Logeart Jan 23 '12 at 07:53
  • 1
    Traditionally, 0 degrees is to the right, 90 is up, 180 is left, and 270 is down. (I may misunderstand your phrasing.) –  Jun 25 '15 at 02:59

4 Answers4

19

Here is the general formula:

angle = atan2(mouseY - cirleCenterY, mouseX - circleCenterX);
Bill
  • 44,502
  • 24
  • 122
  • 213
Sileria
  • 15,223
  • 4
  • 49
  • 28
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.

Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
minopret
  • 4,726
  • 21
  • 34
  • 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 '12 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 '12 at 17:13
  • Corrected from "fromTop - 360.0" to "360.0 - fromTop". Tested at JavaWIDE. – minopret Jan 24 '12 at 01:40
  • 1
    Did you test this snippet? `atan2` is defined (counterintuitively) as `atan2(y, x)` but you're passing `(x, y)`... – Bill Jul 13 '17 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 '17 at 07:49
0
java.lang.Math.atan2(y-360,x-360);
Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
0

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

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

vlad-ardelean
  • 7,480
  • 15
  • 80
  • 124