I have no idea how you got that code. It's wrong in most of the ways it could be wrong; the most correct thing about it is that it has some cases and throws Atn(y / x) in there.... Anyway, I transcribed the definition given from the Wikipedia article I linked into VB6 code. It might well be the same in .NET, though I don't use .NET. I checked it against input distributed across the unit circle and got correct results.
To be honest, I think you should be able to make this routine from the Wikipedia article. I was curious and searched for the routine briefly and found some promising hits in a few seconds as well, so you probably didn't search for it. Honestly, I wrote the above code for the convenience of people who read this thread in the future since I recently ran into an issue when someone could have saved me a fair bit of hassle by posting some code.Code:Private Const PI = 3.14159265 Private Function atan2(y As Single, x As Single) As Single If x > 0 Then atan2 = Atn(y / x) ElseIf x < 0 Then If y >= 0 Then atan2 = PI + Atn(y / x) Else 'y < 0 atan2 = -PI + Atn(y / x) End If Else 'x = 0 If y > 0 Then atan2 = PI / 2 ElseIf y < 0 Then atan2 = -PI / 2 Else 'y = 0 MsgBox "Whoops. You missed an edge case." atan2 = 0 End If End If 'Make the routine slightly nicer; keep angles of 0 to 2 PI outputting in that interval If atan2 < 0 Then atan2 = atan2 + 2 * PI End Function




Reply With Quote