ATan2 Function

Anything QL Software or Programming Related.
User avatar
dinox
ROM Dongle
Posts: 28
Joined: Tue Dec 20, 2016 4:59 pm

ATan2 Function

Post 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


User avatar
dilwyn
Mr QL
Posts: 2753
Joined: Wed Dec 01, 2010 10:39 pm

Re: ATan2 Function

Post 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.


User avatar
dinox
ROM Dongle
Posts: 28
Joined: Tue Dec 20, 2016 4:59 pm

Re: ATan2 Function

Post 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!


User avatar
janbredenbeek
Super Gold Card
Posts: 630
Joined: Wed Jan 21, 2015 4:54 pm
Location: Hilversum, The Netherlands

Re: ATan2 Function

Post 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.


User avatar
ql_freak
Gold Card
Posts: 354
Joined: Sun Jan 18, 2015 1:29 am

Re: ATan2 Function

Post by ql_freak »

Thank You Jan!


http://peter-sulzer.bplaced.net
GERMAN! QL-Download page also available in English: GETLINE$() function, UNIX-like "ls" command, improved DIY-Toolkit function EDLINE$ - All with source. AND a good Python 3 Tutorial (German) for Win/UNIX :-)
User avatar
dinox
ROM Dongle
Posts: 28
Joined: Tue Dec 20, 2016 4:59 pm

Re: ATan2 Function

Post 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


stevepoole
Super Gold Card
Posts: 712
Joined: Mon Nov 24, 2014 2:03 pm

Re: ATan2 Function

Post 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.


stevepoole
Super Gold Card
Posts: 712
Joined: Mon Nov 24, 2014 2:03 pm

Re: ATan2 Function

Post 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


stevepoole
Super Gold Card
Posts: 712
Joined: Mon Nov 24, 2014 2:03 pm

Re: ATan2 Function

Post 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


User avatar
dinox
ROM Dongle
Posts: 28
Joined: Tue Dec 20, 2016 4:59 pm

Re: ATan2 Function

Post by dinox »

Cheers, that looks neater than my version.
Last edited by dinox on Sun Mar 12, 2017 9:07 pm, edited 2 times in total.


Post Reply