# Calculating difference between bearing values in degrees

Suppose I have 3 geographic points, A, B and C.

I can calculate the bearing of B from A (in degrees) using this SQL snippet:

``````degrees(ST_Azimuth(geogA,geogB)) as brg_a_b
``````

...and the same of C from B:

``````degrees(ST_Azimuth(geogB,geogC)) as brg_b_c
``````

Now, I'm wanting to check if A, B and C are close to being in a straight line, let's assume I want the B -> C bearing to be within say, 2 degrees different to the A -> B bearing to consider them in a straight line.

Azimuth is measured clockwise from north, so North = 0º, East = 90º, South = 180º, West is 270º.

Say the A-B bearing is 1º, how do I calculate if B-C is within, say 2º (either way) - IE between the values of 359º and 3º?

I need to construct a SQL "WHERE" clause which effectively does this: WHERE brg_b_c within 2º of brg_a_b (but copes with the 360-0 crossover)

I'm sure this is really simple but am having a mental block working it out.

Thanks for any pointers (or suggestions of achieving the same thing in a different way ?)

``````WHERE abs(brg_a_b-brg_b_c) <2 OR abs(brg_a_b-brg_b_c) > 358