Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Basic Misc > Re: ARCTANXY Fu...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 1435 of 1466
Post > Topic >>

Re: ARCTANXY Function

by david.williams@[EMAIL PROTECTED] (David Williams) Mar 6, 2008 at 12:41 PM

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




 1 Posts in Topic:
Re: ARCTANXY Function
david.williams@[EMAIL PRO  2008-03-06 12:41:59 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed May 14 19:11:29 CDT 2008.