6

For a particle moving about in a Cartesian coordinate system (neglecting the z-axis), how can the angle of travel be computed given the x and y components of the velocity?

Before anyone says this isn't programming related, I am programming this right now, however, I don't know vector math.

For example, suppose the x and y values of the velocity are respectively 5.0 and -1.5, how would I compute the angle?

nbro
  • 15,395
  • 32
  • 113
  • 196
farm ostrich
  • 5,881
  • 14
  • 55
  • 81
  • **This is definitely on-topic.** Just because you don't use vectors at your job doesn't mean basic vector-math isn't *highly* relevant to programming. That being said, this question is a duplicate of [several](http://stackoverflow.com/questions/4428365) [others](http://stackoverflow.com/questions/2276855). – BlueRaja - Danny Pflughoeft May 18 '11 at 16:09

6 Answers6

6

In Javascript, I'd use Math.atan2(1.5, 5.0). To convert to degrees, use Math.atan2(1.5, 5.0)/(Math.PI/180). On Wikipedia: http://en.wikipedia.org/wiki/Atan2

Gray
  • 2,333
  • 1
  • 19
  • 24
  • 1
    I downvoted because you don't give any explanation of why `atan2` should be used to calculate the angle given the x and y components of the velocity. What's the relationship between `atan2` and the velocity? I will remove my downvote if you add these details to the answer. Moreover, the question does not mention Javascript. – nbro Apr 07 '18 at 19:12
4

You need atan2:

For any real arguments x and y not both equal to zero, atan2(y, x) is the angle in radians between the positive x-axis of a plane and the point given by the coordinates (x, y) on it.

nbro
  • 15,395
  • 32
  • 113
  • 196
user541686
  • 205,094
  • 128
  • 528
  • 886
3

The angle in radians from the x-axis is given by:

arctan(vy / vx);  // vx > 0

You also need to handle the case vx < 0.

If you want the bearing versus true north, then you might want:

double bearing = 90 - arctan(vy / vx) * 360 / 2 / M_PI;
nbro
  • 15,395
  • 32
  • 113
  • 196
Keith
  • 6,756
  • 19
  • 23
1

The arc-tangent of the slope will give you what you want.

nbro
  • 15,395
  • 32
  • 113
  • 196
soandos
  • 4,978
  • 13
  • 62
  • 96
1

The angle is the arctangent of y / x. Many languages have a 4-quadrant arctangent function in the math library that takes x and y arguments.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

You have to be careful about what the angles are between. Arctangent, atan(y / x), will give you the angle relative to the positive x-axis, but make sure that's what you need.

nbro
  • 15,395
  • 32
  • 113
  • 196
Adam
  • 16,808
  • 7
  • 52
  • 98