1

I'm not the best in Maths, but for what I am doing now I need to calculate the angle of the vector which is shown as arrow in the picture below:

alt text

I have a point A and a point B in a 2D plane. I need to calculate the following:

  • The angle in which the arrow must be rotated in order to point to B
starblue
  • 55,348
  • 14
  • 97
  • 151
lamas
  • 4,528
  • 7
  • 38
  • 43

2 Answers2

3

atan2(yB-yA, xB-xA), assuming your library has atan2. Otherwise you need to use atan, which will return the correct answer if B is to the right of A, and will be 180 degrees off otherwise. Also note that the return value is in radians, you can convert radians to degrees by multiplying by 180/pi if necessary.

Wikipedia has a detailed explanation of the geometry.

mtrw
  • 34,200
  • 7
  • 63
  • 71
1

arctan((A.y - B.y) / (A.x - B.x)) and note the special case where A.x = B.x

Ivo
  • 3,481
  • 1
  • 25
  • 29