Perhaps it's just the 31 degree heat, but I cannot fucking work this out for the life of me.

Situation is that I have two sets of two latitude/longitude co-ordinates, each representing a journey from A to B (as the crow flies). What I want to do, is find out if those two journeys are headed in roughly the same direction (with a configurable tolerance, at the moment it should "match" if route 2 is within 45 degrees either side of route 1)

So I've got some PHP code calculating what I believe is called the "forward azimuth" for each:

**Code:**

function get_forward_azimuth( $lat1, $lng1, $lat2, $lng2 ) {

$delta_lng = deg2rad( $lng2 - $lng1 );

$lat1 = deg2rad( $lat1 );

$lat2 = deg2rad( $lat2 );

$y = sin( $delta_lng ) * cos( $lat2 );

$x = cos( $lat1 ) * sin( $lat2 ) - sin( $lat1 ) * cos( $lat2 ) * cos( $delta_lng );

$az = round( rad2deg( atan2( $y, $x ) ) );

return $az;

}

This returns a figure between -180 and 180, in these directions:

**Code:**

0

-90 # 90

-180/180

And I'm then converting these directions to a Whole Circle Bearing, between 0 and 359, with this code:

**Code:**

function get_wcb( $az ) {

return ( $az + 360 ) % 360;

}

Which gives directions like this:

**Code:**

359/0

270 # 90

180

And this is where I'm hitting a brick wall. At the moment I'm now doing this:

**Code:**

$delta = abs( $wcb1 - $wcb2 )

if ( $delta <= $tolerance * 2 ) return true;

And this works perfectly (I think) except for anything that crosses that boundary at 359/0, where the maths doesn't stack up.

Feel like I'm missing something super-obvious, but have no idea what.