[Math] How to convert a line angle to a navigational-bearing scale (i.e., with range of [0,360] with “North” = 0 deg)

algebra-precalculusanglegeometry

I have two points, (x1,y1) and (x2,y2), that I'd like to draw a line between. I know I can figure out the angle of that line using Sin(th)=opp/Hyp:

theta = arcsin((y1-y2)/sqrt((y1-y2)^2 + (x1-x2)^2))) *180/pi

or by using arctangent and the slope:

theta = arctan((y2-y1)/(x2-x1)) * 180/pi

However, I want to convert that angle to be on the scale of [0,360].
Basically, I want my angle to be on a compass scale in which "North" is 0 deg, "East" is 90 deg, "South" is 180 deg, and "West" is 270 deg .

Thanks!!

Best Answer

Here is a way to get a direction angle for your line, where $0\le \theta<180°$, $0°$ means straight up (due north), and $90°$ means to the right (due east). This is the standard for bearings in navigation. Let me know if you mean something else: your comments have not been clear.

$$\theta = \begin{cases} 90°-\dfrac{180°}{\pi}\cdot\tan^{-1}\left(\dfrac{y_2-y_1}{x_2-x_1}\right), & \text{if }x_1\ne x_2 \\[2ex] 0°, & \text{if }x_1=x_2 \end{cases} $$

Here's the explanation:

  • The internal fraction $\dfrac{y_2-y_1}{x_2-x_1}$ is the slope of the line
  • The arctangent of that slope is the direction angle of the line, in standard trigonometric form (measured in radians, $0$ is to the right, positive angles are counterclockwise).
  • Multiplying that radians angle by $\dfrac{180°}{\pi}$ converts it to degrees.
  • Subtracting that degree angle from $90°$ changes the orientation to match that of bearings in navigation.
  • That calculation fails for a vertical line, since the $x$-coordinates are equal and the slope is undefined. My formula makes that a special case: vertical lines have bearing $0°$.

There is one problem with that formula: if your two given points are identical, there is no well-defined line through them so no well-defined angle, but my formula gives an answer of $0°$. A slight modification can easily take care of that special case.


You ask about the angle of the line determined by the points $(-0.019,0.406)$ and $(-0.287,-0.353)$. Here is the calculation from my formula:

Angle calculation in TI-Nspire CX calculator

And here is what the angle looks like on a graph:

Angle calculation in Geogebra graph

You see that the two agree. I hope the graph shows you more clearly exactly which angle my formula gives.

As for your different answers: I can't speak about your "nav bearings scale" since I don't know what that is. Check my graph to make sure we are talking about the same angle.

My formula gives values $0°<\theta<90°$ for lines with positive slope and values $90°<\theta<180°$ for lines with negative slope. However, the answer does depend on which point is point 1 and which is point 2. If you do want a formula that distinguishes between them, and also gives angles up to $360°$, here is an alternate formula that uses the atan2 function.

$$\theta = 90°-\dfrac{180°}{\pi}\cdot\operatorname{atan2}\left(x_2-x_1,y_2-y_1\right)$$

This gives an undefined value if the two points are identical. Is this what you want? (Be careful, some systems that have the atan2 function swap the $x$ and $y$ parameters.)