# ArcTan2 Function ?

1. Jan 15, 2008

### Sky Scripter

Anyone have the Source for ArcTan2 ( x, y: Extended);
I Can have it in most any programming language, if you can show me how it works.. then that would be great too.

And just so you know im trying to calculate the Degrees in a line using ArcTan2;

for example:

Degrees := (ArcTan2(MiddleX - X, MiddleY - Y));

MiddleX := Center of the Circle (X Axis);
MiddleY := Center of the Circle (Y Axis);

X := Being the Point i want to calculate (X Axis)
Y := Being the Point i want to calculate (Y Axis)

Thanks :D

Last edited: Jan 16, 2008
2. Jan 16, 2008

### chaoseverlasting

I dont know what extended means, but if you want to program the arctan(x) function, you might want to use the taylor series of the function. Its the summation of $$\frac{x^n}{n!}f^n(x)$$, where $$f^n(x)$$ is the nth derivative of the function youre finding the series of.

If thats too much of a hassle, just look up the series online. In your case, replace x by y/x (as $$tan\theta=\frac{y}{x}$$ which gives $$\theta=tan^{-1}\frac{y}{x}$$.

3. Jan 16, 2008

### Sky Scripter

nvm found source :)

thanks though...

Code (Text):

function ArcTan(x: Extended): Extended;
var
i: Integer;
begin
if x > 1.0 then
begin
Result := Pi / 2 - ArcTan(1 / x);
end else
begin
for i := 0 to 200 do
begin
Result := Result + ((Pow((-1), i) * Pow(x, (2 * i + 1))) / (2 * i + 1))
end;
end;
while Result > 360 do Result := Result - 360;
while Result < 0 do result := result + 360;
end;

function ArcTan2(y, x: Extended): Extended;
var
A1: extended;
begin
A1 := Pi / 180;

if (y = 0.0) and (x < 0.0) then Result := A1 * 180;
if (y = 0.0) and (x > 0.0) then Result := 0.0;
if (x = 0.0) and (y < 0.0) then Result := A1 * 270;
if (x = 0.0) and (y > 0.0) then Result := A1 * 90;
if (Result = 0.0) and (x <> 0.0) then if (Abs(y) / Abs(x) = 1.0) then
begin
if (y > 0.0) and (x > 0.0) then Result := A1 * 45;
if (y > 0.0) and (x < 0.0) then Result := A1 * 135.0;
if (y < 0.0) and (x < 0.0) then Result := A1 * 225.0;
if (y < 0.0) and (x > 0.0) then Result := A1 * 315.0;
end;
if Result = 0 then
begin
Result := ArcTan(Abs(-y) / Abs(x));
if (y > 0.0) and (x < 0.0) then Result := Result + A1 * 90.0;
if (y < 0.0) and (x < 0.0) then Result := Result + A1 * 180.0;
if (y < 0.0) and (x > 0.0) then Result := Result + A1 * 270.0;
end;
end;