有人能指出我在C#中定义的Atan2的一个很好的例子(或者模糊地接近C#的东西),它不使用任何内部数学方法吗?这是在.NET Microframework上,所以没有内部数学库这样的东西。我已经定义了Sin()/ Cos(),但我在使用Atain2时遇到了很多麻烦。
NETMF中有一些散点图数学库,但我发现它们都有缺陷或坏了。其中一个主要的甚至没有正确定义PI!
答案 0 :(得分:3)
实施应该非常直接,从Wikipedia/atan2上的定义开始,然后使用infinite series expansion of arctan on Wikipedia。
只需总结系列中的术语,直到最后一个学期足够小,以适合您的申请 该误差小于最后一个项,因为它是一个严格递减的函数。
修改强>
由于您使用的是.NET Micro框架,因此计算资源可能会很少。根据您所需的精度,您可以考虑预先计算cos,sin,atan2等值的表,并使用最近值的简单查找。另一方面,如果你需要太高的精度,这会浪费一些记忆。
答案 1 :(得分:0)
您是否考虑过/尝试过实施CORDIC算法?它将允许您实现atan函数,从中可以轻松生成atan2函数。
对于CORDIC算法,描述在维基百科上,更好的细节在this paper。另外,我在使用C ++的信号处理(SPUC)项目的sourceforge代码中看到GPL C++ version of CORDIC。它包括数学库,但可以改变以避免这样做。如果你想查看代码,koders上有一个方便的code listing。
如果你不关心速度,给定atan(z)的任何实现返回-pi / 2和pi / 2(或-90和90)之间的值,你可以实现atan2(y,x)返回值容易在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
}
}
答案 2 :(得分:0)
如果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;
答案 3 :(得分:0)
你在用什么板? GHI人员拥有GHI .... System命名空间,它定义了MathEx以及所有缺少的数学函数。
很抱歉没有提供链接,但我正在工作,因此无法在家中访问我的.NET MF代码。
希望有所帮助。
此致