How to compute minimum rotation between two bearings

696 views Asked by At

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)?

2

There are 2 answers

2
MBo On BEST ANSWER
 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
3
dmuir On

If your language has an equivalent of the C math library remainder() function than

D = remainder( B-A, 360.0)