简体   繁体   中英

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM