Page 1 of 2

ATan2 Function

Posted: Mon Jan 23, 2017 2:02 pm
by dinox
Hi all,

I've been using a function called ATan (Arc Tangent) in my programming but this introduces a problem when a zero is used since it crashes. Also it only works in 90 degree sectors (i.e. returns the same four results for 360 degrees). It looks like I should be using a function called ATan2 which doesn't have this problem. However, Superbasic doesn't have this function.

Has anyone written a ATan2 function (in Superbasic) or do I need to write my own?

All the best.

Stephen

Re: ATan2 Function

Posted: Mon Jan 23, 2017 2:45 pm
by dilwyn
There is a Maths Package at http://www.dilwyn.me.uk/tk/Maths_Package.zip whcih includes an ATN and ATN2.

Never used it, so don't know if it does what you want, sorry.

Assuming this is the same maths package as described in the SuperBASIC Reference Guide:
ATN(x) - This functions is the same as the original QL ROM variant of ATAN. ATN has been implemented to make porting programs written in other BASIC dialects easier.

ATN2(x,y) - calculates ATAN(x/y) but expands the results from 0...PI to -PI...PI which allows you to convert cartesian and polar co-ordinates in both directions without loss of information.

Re: ATan2 Function

Posted: Mon Jan 23, 2017 3:03 pm
by dinox
dilwyn wrote:There is a Maths Package at http://www.dilwyn.me.uk/tk/Maths_Package.zip whcih includes an ATN and ATN2.

Never used it, so don't know if it does what you want, sorry.

Assuming this is the same maths package as described in the SuperBASIC Reference Guide:
ATN(x) - This functions is the same as the original QL ROM variant of ATAN. ATN has been implemented to make porting programs written in other BASIC dialects easier.

ATN2(x,y) - calculates ATAN(x/y) but expands the results from 0...PI to -PI...PI which allows you to convert cartesian and polar co-ordinates in both directions without loss of information.
That certainly looks like it. Cheers!

Re: ATan2 Function

Posted: Mon Jan 23, 2017 11:14 pm
by janbredenbeek
dinox wrote:
dilwyn wrote:There is a Maths Package at http://www.dilwyn.me.uk/tk/Maths_Package.zip whcih includes an ATN and ATN2.

Never used it, so don't know if it does what you want, sorry.

Assuming this is the same maths package as described in the SuperBASIC Reference Guide:
ATN(x) - This functions is the same as the original QL ROM variant of ATAN. ATN has been implemented to make porting programs written in other BASIC dialects easier.

ATN2(x,y) - calculates ATAN(x/y) but expands the results from 0...PI to -PI...PI which allows you to convert cartesian and polar co-ordinates in both directions without loss of information.
That certainly looks like it. Cheers!
Minerva and SMSQ's ATAN function has the same functionality when provided with 2 arguments. Quote from manual:
ATAN will provide a 4 quadrant result by taking two parameters. If x is greater than 0, ATAN
(x,y) give the same results as ATAN (y/x). Otherwise it returns values in the other quadrants
(>PI/2 and <-PI/2).
Jan.

Re: ATan2 Function

Posted: Tue Jan 24, 2017 12:40 am
by ql_freak
Thank You Jan!

Re: ATan2 Function

Posted: Mon Mar 06, 2017 11:59 am
by dinox
Thanks for all the help.

I've now also written a short function to simulate the ATan2 Function in SuperBasic which might be of use to others.

29380 DEFine FuNction ATan2 (y,x)
29390 LOCal answer
29400 IF x<>0 THEN arctan=ATAN(y/x)
29410 IF x>0 THEN answer=arctan
29420 IF y>=0 AND x<0 THEN answer= PI + arctan
29430 IF y<0 AND x<0 THEN answer=arctan - PI
29440 IF y>0 AND x=0 THEN answer=PI/2
29450 IF y<0 AND x=0 THEN answer=PI/-2
29460 RETurn answer
29470 END DEFine

Re: ATan2 Function

Posted: Mon Mar 06, 2017 4:49 pm
by stevepoole
Hi,

If you still get any problems with '0' values, I can provide my ATAN_(y,x) function.
I have been using this since 1985 in 3D animation programs and never had any problems with it.
Just say if you need it...

Regards,
Steve Poole.

Re: ATan2 Function

Posted: Tue Mar 07, 2017 12:08 am
by stevepoole
Hi,
Here is a demo in superbasic :

100 REMark ATAN__bas by Steve Poole v6mar17
110 CLS : FOR x = -1 to 1 : print : for y = -1 to 1 : print DEG(ATAN_(y,x))
120 :
130 DEFine FuNction ATAN_(y,x)
140 IF x>0 : RETurn ATAN(y/x)
150 IF x=0 : RETurn PI * ((y>0) - (y<0)) /2
160 IF y=0 : RETurn PI
170 RETurn ATAN(y/x) + PI * ((y>0) - (y<0))
180 END DEFine

Re: ATan2 Function

Posted: Tue Mar 07, 2017 12:18 pm
by stevepoole
Hi,
After over 30 years of use of the previous routine, this more recent version of the rad_angle=ATAN_(y,x) function runs some 10% faster...

100 DEFine FuNction ATAN_(y,x)
110 IF x>0 : RETurn ATAN(y/x)
120 if x<0 : IF y<0 : RETurn ATAN(y/x) - PI : ELSE RETurn ATAN(y/x) + PI
130 IF y>0 : RETurn PI / 2
140 IF y<0 : RETurn PI / -2 : ELSE RETurn 0
150 END DEFine

Re: ATan2 Function

Posted: Sat Mar 11, 2017 9:21 pm
by dinox
Cheers, that looks neater than my version.