On 04/03/2008 7:33 PM, DAVID WILLIAMS wrote to All:
-> FUNCTION Ang(X, Y)
-> SELECT CASE SGN(X)
-> CASE 1: A = ATN(Y / X)
-> CASE -1: A = ATN(Y / X) + PI
-> CASE ELSE: A = SGN(Y) * PI / 2
-> END SELECT
-> IF A > PI THEN A = A - 2 * PI
-> Ang = A
-> END FUNCTION
->
-> The only real difference between this and the version I posted this
-> morning is the IF line near the end. It subtracts 2*PI, i.e. a complete
-> rotation, from the angle if it is greater than PI. The result is that
-> the FUNCTION in the above form outputs angles in the range -PI to PI.
Here's a neater way to do the same thing:
FUNCTION Ang(X, Y)
SELECT CASE SGN(X)
CASE1: Ang = ATN(Y / X)
CASE -1: Ang = ATN(Y / X) + PI * SGN(SGN(Y) +.5)
CASE ELSE: Ang = SGN(Y) * PI / 2
END SELECT
END FUNCTION
This will produce an output in the range -PI < Ang <= PI. This may (or may
not) be what is done by the function in another language that you're
trying
to emulate.
The CASE -1 line adds PI to the ATN if Y is positive *or zero*, or
subtracts PI if Y is negative. This puts the output into the range I
described above.
The case where Y = 0 may be important. If the input is (-1, 0), for
example, should the output be PI or -PI? The code above makes it PI.
However, if you need it to be -PI, just change the "+ .5" in the CASE -1
line to "- .5".
The important thing to realize is that, to make the BASIC function do
exactly what is done by the one you want to emulate, you must first
investigate *exactly* what the non-BASIC one does. You must find out what
range it uses for its outputs, including the "end points" (PI and -PI in
the case above). Only when you know exactly what you want to replicate
will
you be able to choose an exact equivalent in BASIC.
dow


|