Title says it all, I'm looking for the formula to get the bearing from one point to another on a number plane. I have found examples of this for lat/lon around the earth but that's not exactly what i need. Thanks
Bearing from one point to another on 2D number plane
1 Answers
Exactly what is a "bearing" to you? Compass bearings are typically quoted with 0° meaning due north and increasing in the clockwise direction, whereas in mathematics in general the convention is to consider 0° to be due right, and increasing counterclockwise.
For a compass bearing, the conventional answer is $\tan^{-1}\left(\frac{E_2-E_1}{N_2-N_2}\right)$, where $(E_1,N_1)$ and $(E_2,N_2)$ are the (easting,northing) coordinates of your two points. There are various points to keep in mind, though:
Your arctangent function probably gives values in radians, which you'll have to convert to degrees yourself.
If the northings are equal, you'll end up dividing by zero. This needs to be handled as a special case, answering 90° or 270° as appropriate.
The formula also cannot distinguish between a direction and its direct opposite, so you need to add 180° if $N_2
. Directions in the northwest quadrants will end up as negative numbers between $-$90° and 0°; you'll want to add 360° to those to make them look like ordinary compass bearings.
If you're looking to create a computer implementation, see if your programming language has an atan2
function -- it will take care of points 2 and 3 for you automatically. Typically it should be called as atan2(e2-e1,n2-n1)
, but I've heard rumors of languages that need you to give the arguments in the opposite order.