IonicWind Software

IWBasic => General Questions => Topic started by: arono on March 13, 2017, 12:18:22 PM

Title: I need the function "atan2"
Post by: arono on March 13, 2017, 12:18:22 PM
Hi,

I need this function to calculate collisions with circles.  I have the x,y point of the collision between a ball and the circumference of a circle.  I also know the center x,y of the circle and its radius.  In order to determine what angle the ball will take after it collides, I need to know the angle of that collision point to the center of the circle. 

In order to calculate this, i need function atan2.  Unfortunately it isn't in the Microsoft SDK that was installed with IWBasic.  Is there another library I can access?

Thanks.
Title: Re: I need the function "atan2"
Post by: billhsln on March 13, 2017, 01:07:59 PM
I found this equation that is supposed to replace ATAN2.

angle = (y/x) - ((1/3)*(y/x))*((1/3)*(y/x))*((1/3)*(y/x)) + ((1/5)*(y/x))*((1/5)*(y/x))*((1/5)*(y/x))*((1/5)*(y/x))*((1/5)*(y/x))

Maybe it will work for you?

Bill
Title: Re: I need the function "atan2"
Post by: GWS on March 13, 2017, 03:37:39 PM
Hi Arono,

I find it's often useful to write your own function.  Once declared in your program, it becomes part of the language ..  :)

Here's one for the atan2(y,x) function ..


' Test Routine for atan2(y,x) function ..

openconsole
cls

def pi,coeff_1,coeff_2,abs_y,angle,r,x,y:double

declare atan2(y:double,x:double)

autodefine "off"
setprecision 10

pi = 4 * atan(1)

coeff_1 = pi / 4
coeff_2 = 3 * coeff_1

' example for atan2(1,1) .. angles in radians
x = 1
y = 1

atan2(y,x)

print angle
' should give answer pi/4 = 0.7853981634


do:until inkey$<>""
closeconsole
end


sub atan2(y,x)

abs_y = abs(y)

if (x >= 0)
r = (x - abs_y) / (x + abs_y)
angle = coeff_1 - coeff_1 * r
else
r = (x + abs_y) / (abs_y - x)
angle = coeff_2 - coeff_1 * r
endif

if (y < 0) then angle = -angle

return



It seems to be working OK, but I haven't tested it fully ....

Best wishes, :)

Graham
Title: Re: I need the function "atan2"
Post by: fasecero on March 13, 2017, 05:40:54 PM
Another alternative, if you are willing to use the C runtime library

/*
Compile as a CONSOLE target
*/

DECLARE CDECL EXTERN _atan2(double y, double x), double ' The C runtime uses CDECL EXTERN and not CDECL IMPORT

PRINT _atan2(4, 8)
PRINT:PRINT "Press any key to close"
DO:UNTIL INKEY$<>""
Title: Re: I need the function "atan2"
Post by: Brian on March 15, 2017, 03:15:02 AM
Fasecero,
On another tack, how do you find out what functions are available in the C runtimes? The atan2 seemed very elegant
Brian
Title: Re: I need the function "atan2"
Post by: fasecero on March 15, 2017, 09:52:18 PM
Hi Brian, you can check in here for CRT math functions

https://msdn.microsoft.com/en-us/library/y0ybw9fy.aspx (https://msdn.microsoft.com/en-us/library/y0ybw9fy.aspx)
Title: Re: I need the function "atan2"
Post by: Brian on March 16, 2017, 03:21:49 AM
Wow, there's a heck of a lot of stuff in there when you dig down. There must be something I can use! Thanks a lot,

Brian