Chong Lip Phang
Chong Lip Phang

Reputation: 9279

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

Upvotes: 0

Views: 788

Answers (2)

dmuir
dmuir

Reputation: 4431

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

D = remainder( B-A, 360.0)

Upvotes: 1

MBo
MBo

Reputation: 80287

 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

Upvotes: 3

Related Questions