Math.Atan2产生奇怪的结果?奇怪、结果、Math

2023-09-07 13:13:01 作者:凉薄少年

首先,我的数学不强!但我计算某点的角度从原点(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));
 
如图是一农田长期使用一种农药后害虫群体密度变化曲线.下列叙述.不符合达尔文进化观点的是 A.随着农药的使用.害虫群体的抗药性逐渐增强B.农药使害虫产生变异C.害虫抗药性的形成是农药对害虫定向选择的结

这是我的 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...

* For future reference, this sort of detail is usually given explicitly in the documentation. For example: http://msdn.microsoft.com/en-us/library/system.math.atan2.aspx.