Math.Atan2 gives a strange result?

Firstly, my math is not strong! But I calculated the point angle from the origin (0, 0) to (-1, -1).

This angle is 225 Β° or 5Ο€ / 4.

  • 5Ο€ / 4 = 3.9269908169872415480783042290994
  • Math.Atan2 (-1, -1) = -2.3561944901923448

Perhaps my understanding of Math.Atan2 is incorrect, but I thought that he should have returned the degree of the angle given by two points on the circle, the beginning of which (0, 0). I assume that what happens here is that the result from Atan2 β€œflipped” because it is on the negative side of the X axis ... can someone confirm and provide an appropriate way to do this?

This is my test that fails:

 Assert.AreEqual((5 * Math.PI) / 4, Math.Atan2(-1, -1)); 

And here is my PointD class, which contains points X and Y:

 public double ToRadians() { double radians = Math.Atan2(this.Y, this.X); return radians; } 
+4
source share
1 answer

Your guess is correct. Atan2 gives a result in the range from -pi to + pi. * If you need a number between 0 and 2pi, just add 2pi if the result is less than 0.


Note: A statement for equality with floating point values ​​like this is usually not a good idea ...


* For future reference, this information is detailed in the documentation. For example: http://msdn.microsoft.com/en-us/library/system.math.atan2.aspx .

+13
source

All Articles