ATAN2 / Arctan2 in Pascal
ATAN2(Y, X) or ArcTan2(Y, X) is an exceedingly useful function, not
provided in Pascal. It gives the anti-clockwise angle between the X-
axis and the line (0,0), (X,Y), in radians, as -pi < theta <= pi.
For ATAN2 / Arctan2 in Pascal, one can use
function atan2(y : extended ; x : extended) : extended ; assembler ;
asm fld [y] ; fld [x] ; fpatan end ;
( see in <URL:http://www.merlyn.demon.co.uk/programs/fpatan2.pas> )
For those who need to get ArcTan2 from arctan, without using assembler
or inline, consider the following tested VBscript :
function AT2(byval Y, X) '' new 2003-10-29
if Y<>0 then
AT2 = 2.0 * Atn( Y / ( X + Sqr(X*X+Y*Y) ) )
else
if X<0 then AT2 = Pi else AT2 = 0
end if
end function
which gives results agreeing with its predecessor to about one in 10^16.
The main expression probably represents a well-known trigonometrical
equation, but was in fact derived directly from a geometrical diagram
and the definition of tan as opposite/adjacent.
An untested Pascal translation is :
function AT2(const Y, X : extended) : extended ;
begin
if Y<>0.0
then AT2 := 2.0 * arctan( Y / ( X + Sqr(X*X+Y*Y) ) )
else if X<0.0 then AT2 := Pi else AT2 := 0.0 ;
end {AT2} ;
These clearly only work if X-squared is in range; I believe that this is
a case where Y<>0.0 is a good test. Note : the arctan expression does
give the right result when Y=0 provided that X > 0 (X=0?).
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
|