ATan2 Function
ATan2 Function
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
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
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:
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.
--
All things QL - https://dilwyn.qlforum.co.uk/index.html
All things QL - https://dilwyn.qlforum.co.uk/index.html
Re: ATan2 Function
That certainly looks like it. Cheers!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.
- janbredenbeek
- Super Gold Card
- Posts: 632
- Joined: Wed Jan 21, 2015 4:54 pm
- Location: Hilversum, The Netherlands
Re: ATan2 Function
Minerva and SMSQ's ATAN function has the same functionality when provided with 2 arguments. Quote from manual:dinox wrote:That certainly looks like it. Cheers!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.
Jan.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).
Re: ATan2 Function
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
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
Re: ATan2 Function
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
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
-
- Super Gold Card
- Posts: 714
- Joined: Mon Nov 24, 2014 2:03 pm
Re: ATan2 Function
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.
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.
-
- Super Gold Card
- Posts: 714
- Joined: Mon Nov 24, 2014 2:03 pm
Re: ATan2 Function
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
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
-
- Super Gold Card
- Posts: 714
- Joined: Mon Nov 24, 2014 2:03 pm
Re: ATan2 Function
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
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
Cheers, that looks neater than my version.
Last edited by dinox on Sun Mar 12, 2017 9:07 pm, edited 2 times in total.