Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > ATAN2 / Arctan2 in Pascal
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 27th March 01:38
dr john stockton
External User
 
Posts: 1
Default 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.
  Reply With Quote


  sponsored links


2 1st April 02:44
olivier avenel
External User
 
Posts: 1
Default ATAN2 / Arctan2 in Pascal



Dr John Stockton a écrit:


I'm puzzled %-).
ArcTan2(Y,X) is simply ArcTan(Y/X).

O.A.
  Reply With Quote
3 1st April 02:44
dr john stockton
External User
 
Posts: 1
Default ATAN2 / Arctan2 in Pascal


JRS: In article <01c3bb07$d7cb5ca0$6815a684@spec104>, seen in
news:comp.lang.pascal.borland, Olivier Avenel <oavenel@cea.fr> posted at
Fri, 5 Dec 2003 08:14:28 :-


As stated, the range of ArcTan2 is -pi < theta <= +pi
However, the range of ArcTan is -pi/2 < theta <= +pi/2

If X -> -X, Y -> -Y, then ArcTan does not change but ArcTan2 changes by
pi.

--
© 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.
  Reply With Quote
4 1st April 02:44
bob schor
External User
 
Posts: 1
Default ATAN2 / Arctan2 in Pascal


I coded the following function --

FUNCTION atan2 (yval, xval : real) : real;

{ returns arctan (y/x), angle in (-pi, pi], handles 0 values }

BEGIN { atan2 }
IF zero (yval) THEN BEGIN
IF xval >= 0
THEN atan2 := 0
ELSE atan2 := radians (180)
END
ELSE
IF zero (xval) THEN BEGIN
IF yval >= 0
THEN atan2 := radians (90)
ELSE atan2 := radians (-90) END ELSE
IF xval >= 0
THEN atan2 := arctan (yval / xval) ELSE
IF yval >= 0
THEN atan2 := arctan (yval / xval) + radians (180)
ELSE atan2 := arctan (yval / xval) - radians (180)
END;

This uses a "helper function" I call "radians" which converts degrees
to radians. Note that radians (90) is pi/2, and radians (180) is pi.

It also uses a helper function I call "zero", which tests if an
argument is "near zero". One version of this function was
zero := abs(arg) < epsilon, where epsilon was defined as 1.0E-6.

Bob Schor
Pascal Enthusiast
  Reply With Quote
5 1st April 02:45
dr john stockton
External User
 
Posts: 1
Default ATAN2 / Arctan2 in Pascal


JRS: In article <1u85tvk0toaa2jhck3fhvpp131e1sihgnp@4ax.com>, seen in
news:comp.lang.pascal.borland, Bob Schor <bschor@pitt.edu> posted at
Sat, 6 Dec 2003 22:56:15 :-


That is very much the sort of convoluted code that I wanted to avoid;
mine has many fewer routes, and is thus easier to test.

Your code has serious problems if the definition of zero, e.g. epsilon,
is or becomes inappropriate to the numbers supplied. A chemist who has
been contentedly calculating crystal geometry using the above and
Angstrom units may get severe error if he upgrades to SI and uses data
in metres or in nanometres * 10^-9.

In fact, Atan2 should have no dependence on the scale of (X & Y).

In this newsgroup, the type "real" needs a health warning.

If it is really necessary to take special moves for small numbers, I
would seek to go to a linear approximation rather than to a constant
value.

My first thought was to form whichever will be the smaller of X/Y and
Y/X, and to proceed from there. That ensures smooth behaviour across
the principal axes - but it became ***bersome.

--
© 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.
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666