...find true angle of a vector or complex number (Arctan)?
Author: Uffe Jakobsen
{
A simple but usefull tiny tip.
This version of Arctan is valid throughout the
interval [-PI/2;3/2*PI] unlike normally only ]-PI/2;PI/2[.
It is of use when you need to convert a given vector from rectangular to
polar format. It is quite handy when you do complex numbers.
Furthermore it allows finding angles of vertical lines or purely imaginery complex numbers.
A similar function exists in the standard libraries of MOSCOW-ML and C/C++, but not in DELPHI math.
}
function ArcTan2(x, y: Double): Double;
var
retur: Double;
begin
Retur := 0.0;
if ABS(x) < 0.000000001 then //Close to zero! We declare it to be zero!
begin //The small margin gives a slight error, but improves reliability.
if y > 0 then
begin
Retur := PI / 2; //90 degrees
end
else
begin
Retur := PI / (-2); //-90 degrees
end;
end
else
begin
if x > 0 then // 1. or 4. quadrant
begin
Retur := ArcTan(Y / X); // Easy stuff. Normal ArcTan is valid for 1. and 4.
end
else // 2. or 3. quadrant
begin
Retur := PI - ArcTan(-Y / X);
end;
end;
ArcTan2 := Retur;
end;