Atan2 in C# (or similar lanaguge)

不羁岁月 提交于 2019-12-02 01:24:46

An implementation should be quite straight forward starting with the definition on Wikipedia/atan2 and then using the infinite series expansion of arctan on Wikipedia.

Just sum up terms in the series until the last term is small enough for your application.
The error is less then the last term since it is an strictly decreasing function.

Edit:
Since you´re on the .NET Micro framework you might be low on computational resources. Depending on your required precision you might consider precalculating a table of the cos, sin, atan2, etc values and just use a simple lookup of the closest value. On the other hand this wastes some memory if you need too high precision.

Have you considered/tried implementing the CORDIC algorithm? It will allow you to implement the atan function, from which it is easy to generate the atan2 function.

For the CORDIC algorithm, a description is at Wikipedia and better details are in this paper. Also, I see a GPL C++ version of CORDIC at the sourceforge code of the Signal Processing using C++ (SPUC) project. It includes the math library, but could be altered to avoid doing so. There is a convenient code listing on koders if you want to see the code.

If you are not concerned with speed, given any implementation of atan(z) that returns values between -pi/2 and pi/2 (or -90 and 90), you can implement atan2(y,x) returning values between 0 and 2pi (or 360) easily. Here is example pseudocode:

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
    }
}

This is Pascal implementation with fixed point numbers if ArcTan is already implemented:

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;

What board are you using. The GHI guys have the GHI....System namespace which defines MathEx with all the missing Math functions.

Sorry for not supplying links, but I'm at work so don't have access to my .NET MF Code at home.

Hope that helps.

Regards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!