[FIXED] Find the Angle between two Bearings

Issue

Given two bearing, how do I find the smallest angle between them?

So for example if 1 heading is 340 degrees and the second is 10 degrees the smallest angle will be 30 degrees.

I’ve attached a picture to show what I mean. I’ve tried subtracting one from the other but that didn’t work because of the wrap around effect of a circle. I’ve also tried using negative degrees (180 – 359 being -180 to 0) but that got messed up when trying to calculate the angle between positive and negative number.

I’m sure there must be an easier way that having lots of if statements.

Thank for your help.
Adam

BTW. This is a navigation question so the radius of the circle is unknown.

Finding the angle between two headings

Solution

float getDifference(float a1, float a2) {
    return Math.min((a1-a2)<0?a1-a2+360:a1-a2, (a2-a1)<0?a2-a1+360:a2-a1)
}

Answered By – Rob Watts

Answer Checked By – Robin (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published