Choosing the right from asin solutions
I am getting the magnitude of 2 vectors that represent the speed of a vehicle in North-South and East-West direction. North is considered positive South negative , East positive and West negative. Obviously they are perpendicular to each other. I can calculate the magnitude of the addition 开发者_JAVA百科of the two vectors using the pythagorean theorem but when it comes to the angle there is a problem. I use Math.asin
but there are two solutions for a given sine. Two angles for example 45 and 135 have the same sine. How can i point to asin method which solution I would like since i know from the begining whether the vehicle is heading north-east or south-east?
You may want to take the arctangent using Math.Atan2(y, x)
rather than the arcsine to obtain the direction. Atan2
returns:
- arctan(y/x) if x!= 0 and y != 0 (always in (-pi, pi])
- pi if y=0 and x < 0
- 0 if y=0 and x >= 0
- pi/2 if x=0 and y > 0
- -pi/2 if x=0 and y < 0
(The boundary conditions are defined in the MSDN documentation)
For example, if x=-1 and y=1, Atan(-1,1) would be 3pi/4 (135 degrees) rather than 45deg.
精彩评论