问题描述
首先,我的数学不强!但我计算某点的角度从原点(0,0)到(1,-1)。
这角度为225°或5π/ 4。
- 在5π/ 4 = 3.9269908169872415480783042290994
- Math.Atan2(-1,-1)= -2.3561944901923448
也许我的 Math.Atan2
的理解是不正确,但我认为这是为了回报给出了一圈,其原产地是两个点的角度(0度, 0)。我认为这里发生的是,从 ATAN2
是翻转的结果,因为它是在X轴的负方...有人可以确认并提供适当的办法做到这一点?
这是我的测试,它失败:
Assert.AreEqual((5 * Math.PI)/ 4,Math.Atan2(-1,-1));
这是我的 PointD
类,它包含点X和Y:
公共双ToRadians()
{
双弧度= Math.Atan2(this.Y,this.X);
返回弧度;
}
你的假设是正确的。 ATAN2
给出的结果范围在-pi到+ PI。如果你想要一个数字范围为0到二皮音乐,只是如果加上二皮其结果是小于0
注意:断言对浮点值相等像这通常不是个好主意......
*为方便日后参考,这类细节通常是明确的文档中给出。例如:http://msdn.microsoft.com/en-us/library/system.math.atan2.aspx.
First, my math is not strong! But I was calculating the angle of a point 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 it was meant to return the degree of an angle given two points on a circle whose origin is (0, 0). What I assume is happening here is that the result from Atan2
is "flipped" since it is on the negative side of the X axis...can someone confirm and provide the proper way to do this?
This is my test, which fails:
Assert.AreEqual((5 * Math.PI) / 4, Math.Atan2(-1, -1));
And here's my PointD
class which holds points X and Y:
public double ToRadians()
{
double radians = Math.Atan2(this.Y, this.X);
return radians;
}
Your assumption is correct. Atan2
gives a result in the range -pi to +pi. If you want a number in the range 0 to 2pi, just add 2pi if the result is less than 0.
Note: Asserting for equality on floating-point values like this is usually not a good idea...
这篇关于Math.Atan2产生奇怪的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!