PDA

View Full Version : Arctangent inconsistency



RobbeK
04-04-2014, 17:07
...

in the math module the atan2 outputs degrees, the other fucntions radians ...
(the atn the normal radians)

noticed while working on something ..

,------------------------------- see attachment

Uses "math" , "console"


PrintL "atn(1) ="+Str$(Atn (1))
PrintL "atan2(1,1) ="+Str$(ATAN2(1,1))
PrintL "arccos(0) ="+Str$(ArcCos(0))

WaitKey

,--------------------------------

best Rob

peter
04-04-2014, 17:19
Yes, should have: 0.7853981633974483

peter
04-04-2014, 18:00
Hi Robbek,

I wrote an Atan2 function for you.


Uses "math", "ui"
#INCLUDE "abc.inc"

OpenWindow 320,240
SetHandleDC hdc, hwnd

Function Atan(y,x As Double) As Double
If x=0 And y=0 Then Return -1
Local a2 As Double
If y > 0 Then
If x >= y Then
a2 = Atn(y / x)
ElseIf x <= -y Then
a2 = Atn(y / x) + Pi
Else
a2 = Pi / 2 - Atn(x / y)
End If
Else
If x >= -y Then
a2 = Atn(y / x)
ElseIf x <= y Then
a2 = Atn(y / x) - Pi
Else
a2 = -Atn(x / y) - Pi / 2
End If
End If
Return a2
End Function

DrawString 10,10,Str$(Atan(1,1)),0
Canvas_Redraw
Canvas_WaitKey
Canvas_Window End hwnd

RobbeK
04-04-2014, 18:06
Thanks Peter,

... DegtoRad(atan2(y/x)) also works , but it took a few minutes to find out what was going on in my code ;-)
( atn (x) is an incomplete function of course , it can't make the difference between -- , ++ and -+ , +- )

best Rob