C#中的Atan2(或类似的lanaguge)

有人能指出我在C#中定义的Atan2的一个很好的例子(或者模糊地接近C#的东西),它不使用任何内部数学方法吗? 这是在.NET Microframework上,因此没有内部数学库这样的东西。 我已经定义了Sin()/ Cos(),但我在使用Atain2时遇到了很多麻烦。

NETMF中有一些散点图数学库,但我发现它们都有缺陷或坏了。 其中一个主要的甚至没有正确定义PI!

从Wikipedia / atan2上的定义开始,然后在维基百科上使用arctan的无限级数展开,实现应该非常简单。

只需总结系列中的术语,直到最后一个术语足够小,适合您的应用程序。
该误差小于最后一个项,因为它是一个严格递减的函数。

编辑:
由于您使用的是.NET Micro框架,因此计算资源可能会很少。 根据您所需的精度,您可以考虑预先计算cos,sin,atan2等值的表,并使用最近值的简单查找。 另一方面,如果你需要太高的精度,这会浪费一些记忆。

您是否考虑/尝试实施CORDIC算法? 它将允许您实现atan函数,从中可以轻松生成atan2函数。

对于CORDIC算法,描述在维基百科上,本文中有更好的细节。 另外,我在使用C ++的信号处理(SPUC)项目的sourceforge代码中看到了一个GPL C ++版本的CORDIC 。 它包括数学库,但可以改变以避免这样做。 如果您想查看代码,可以在koders上找到方便的代码。

如果你不关心速度,给定atan(z)的任何实现返回-pi / 2和pi / 2(或-90和90)之间的值,你可以实现atan2(y,x)返回0到0之间的值2pi(或360)很容易。 这是一个伪代码示例:

atan2(y,x){ if (x < 0){ return (atan(y/x)+3*pi/2); // subst 270 for 3*pi/2 if degrees }else{ return (atan(y/x)+pi/2); // subst 90 for pi/2 if degrees } } 

如果ArcTan已经实现,这是具有定点数的Pascal实现:

 function Fix64ArcTan2(const y, x: fix64): fix64; // based on http://en.wikipedia.org/wiki/Atan2 // Variation of the arctangent function. For any real arguments x and y not both // equal to zero, arctan2(x,y) is the angle in radians between the positive x-axis // of a plane and the point given by the coordinates (x,y) on it. var result: fix64; begin if x = 0.0 then if y = 0.0 then result := 0.0; // ArcTan2(0,0) is undefined, but I had to return something !!! elsif y > 0.0 then result := FIX_PIHALF; else // y < 0.0 result := -FIX_PIHALF; endif; else result := Fix64ArcTan(Fix64Div(y,x)); if x < 0.0 then if Y < 0.0 then result := result - FIX_PI; else // y >= 0.0 result := result + FIX_PI; endif; endif; if result > FIX_PI then result := result - FIX_PITWO; endif; endif; return(result); end; 

你在用什么板? GHI人员拥有GHI …. System命名空间,它定义了MathEx以及所有缺少的Math函数。

很抱歉没有提供链接,但我正在工作,所以无法在家中访问我的.NET MF代码。

希望有所帮助。

问候