Finding the angle of a vector

Blitz3D Forums/Blitz3D Programming/Finding the angle of a vector

Rico(Posted 2011) [#1]
I want to find at what angle (0-306 degress) a vector is aligned. So for example i might have a vector made up of an x-component of 2 and a y y-component of -1.5.

I only know a little about trigonometry, but i figured i could use Tan. However this gives me angles which seem to change depending on which quadrant the vector is in, and i am not sure how to convert this into 0-360 degrees.

Please could somone tell me the best way of doing this. Thank you :)


JBR(Posted 2011) [#2]
atan2


Rico(Posted 2011) [#3]
Thanks JBR, it still seems to work in 2 halfs of a circle. but i found this seems to work if i do this little fix on the result. It gives an angle in proper 0 to 360 form. where 0 degrees is directly to the right

ans=ans=ATan2(y,x)
If ans<0 Then ans=(180-Abs(ans))+180



JBR(Posted 2011) [#4]
I may be wrong, but this is what I'd do

ans = ATan2(y,x)

ans = (ans + 360) mod 360

ATan2 returns between -180 to +180

Jim