5

I have the centerX and centerY value of the circle and radius. And now i have (x1, y1) point that lie on the circle. I want to know the angle of circle for the point.

I tried the below formula to get the angle of (x1, y1). But it is not giving generic solution.

radian = Math.Atan2(y1 - Cy, x1 - Cx);
angle = radian * (180 / Math.PI);

Please refer the screenshot for my requirement.

enter image description here

Anyone please let me suggest what i did wrong.?

Liam
  • 27,717
  • 28
  • 128
  • 190
Bharathi
  • 1,288
  • 2
  • 14
  • 40
  • 1
    I believe this is because atan2 gives answers between -180 and 180 degrees, whereas you want 0 to 360 – meowgoesthedog Aug 11 '17 at 10:28
  • I think this covers the maths here [Finding the angle between two points](https://math.stackexchange.com/questions/1201337/finding-the-angle-between-two-points) – Liam Aug 11 '17 at 10:33

1 Answers1

10

From the MSDN documentation page for Atan2, it returns a result between -180 and 180 degrees (-pi to pi radians). On the other hand, you require 0 to 360. To do this, simply add 360 to the final answer in degrees if it is negative.

radian = Math.Atan2(y1 - Cy, x1 - Cx);
angle = radian * (180 / Math.PI);
if (angle < 0.0) 
   angle += 360.0;
meowgoesthedog
  • 14,670
  • 4
  • 27
  • 40
  • 1
    Since the span is already between *[-180, 180]*, shouldn't the user just add 180 to the final answer if the answer is negative? – Sometowngeek Aug 11 '17 at 10:39
  • 1
    @Sometowngeek No. e.g. when atan2 gives -90 (directly above center), the corresponding result required by OP would be 270 degrees. Adding 180 would give 90 which is incorrect. – meowgoesthedog Aug 11 '17 at 10:41
  • Is there any direct solution to cover the 360 degree..? – Bharathi Aug 11 '17 at 10:46
  • 1
    I removed my second comment because I just realized I need my morning coffee to before I do anything. (My brain is slow at the moment) – Sometowngeek Aug 11 '17 at 10:47
  • I was trying to compare it with [How to get angle of point from center point?](https://stackoverflow.com/questions/22888773/how-to-get-angle-of-point-from-center-point/22888812#22888812) -- This uses python – Sometowngeek Aug 11 '17 at 10:48
  • @Bharathi I don't think there is. Strictly speaking `Atan2` itself is not a "direct" solution as such - it first processes the 4 different cases for the four different quadrants. And do you have a specific reason for wanting a direct solution? – meowgoesthedog Aug 11 '17 at 10:48