algorithmmathgeometrygeobearing

How to compute minimum rotation between two bearings


Given two bearings between 0 degree and 360 degrees, A and B, what is the most efficient way to compute the minimum rotation required for A to reach B? The rotation can be clockwise (positive) or anti-clockwise (negative). The minimum rotation should be in whichever direction which gives the smaller absolute degrees.

For example,

minRotation(30,20) yields -10.

minRotation(350,20) yields 30.

How do we formulate the function minRotation(A,B)?


Solution

  •  D = B - A
     while D < -180   // use "if" operator when angles are normalized to 0..360 range
        D = D + 360
     while D > 180
        D = D - 360