April 25, 2024, 08:18:27 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


I need the function "atan2"

Started by arono, March 13, 2017, 12:18:22 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

arono

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.

billhsln

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
When all else fails, get a bigger hammer.

GWS

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
Tomorrow may be too late ..

fasecero

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$<>""

Brian

Fasecero,
On another tack, how do you find out what functions are available in the C runtimes? The atan2 seemed very elegant
Brian

fasecero

March 15, 2017, 09:52:18 PM #5 Last Edit: March 15, 2017, 10:00:50 PM by fasecero
Hi Brian, you can check in here for CRT math functions

https://msdn.microsoft.com/en-us/library/y0ybw9fy.aspx

Brian

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