Q:

Intersection of Lines Given LatLong + Bearing

I'm trying to port some javascript to php from http://www.movable-type.co.uk/scripts/latlong.html. I get it to run with out error but it gives me a different value. I tried to double check it all and finally just did it again but with the same result. I'm not sure if i didn't translate a function incorrectly somewhere or what.
The javascript...
/**
 * Returns the point of intersection of two paths defined by point and bearing
 *
 *   see http://williams.best.vwh.net/avform.htm#Intersection
 *
 * @param   {LatLon} p1: First point
 * @param   {Number} brng1: Initial bearing from first point
 * @param   {LatLon} p2: Second point
 * @param   {Number} brng2: Initial bearing from second point
 * @returns {LatLon} Destination point (null if no unique intersection defined)
 */
LatLon.intersection = function(p1, brng1, p2, brng2) {
  brng1 = typeof brng1 == 'number' ? brng1 : typeof brng1 == 'string' && trim(brng1)!='' ? +brng1 : NaN;
  brng2 = typeof brng2 == 'number' ? brng2 : typeof brng2 == 'string' && trim(brng2)!='' ? +brng2 : NaN;
  lat1 = p1._lat.toRad(), lon1 = p1._lon.toRad();
  lat2 = p2._lat.toRad(), lon2 = p2._lon.toRad();
  brng13 = brng1.toRad(), brng23 = brng2.toRad();
  dLat = lat2-lat1, dLon = lon2-lon1;

  dist12 = 2*Math.asin( Math.sqrt( Math.sin(dLat/2)*Math.sin(dLat/2) + 
    Math.cos(lat1)*Math.cos(lat2)*Math.sin(dLon/2)*Math.sin(dLon/2) ) );
  if (dist12 == 0) return null;

  // initial/final bearings between points
  brngA = Math.acos( ( Math.sin(lat2) - Math.sin(lat1)*Math.cos(dist12) ) / 
    ( Math.sin(dist12)*Math.cos(lat1) ) );
  if (isNaN(brngA)) brngA = 0;  // protect against rounding
  brngB = Math.acos( ( Math.sin(lat1) - Math.sin(lat2)*Math.cos(dist12) ) / 
    ( Math.sin(dist12)*Math.cos(lat2) ) );

  if (Math.sin(lon2-lon1) > 0) {
    brng12 = brngA;
    brng21 = 2*Math.PI - brngB;
  } else {
    brng12 = 2*Math.PI - brngA;
    brng21 = brngB;
  }

  alpha1 = (brng13 - brng12 + Math.PI) % (2*Math.PI) - Math.PI;  // angle 2-1-3
  alpha2 = (brng21 - brng23 + Math.PI) % (2*Math.PI) - Math.PI;  // angle 1-2-3

  if (Math.sin(alpha1)==0 && Math.sin(alpha2)==0) return null;  // infinite intersections
  if (Math.sin(alpha1)*Math.sin(alpha2) < 0) return null;       // ambiguous intersection

  //alpha1 = Math.abs(alpha1);
  //alpha2 = Math.abs(alpha2);
  // ... Ed Williams takes abs of alpha1/alpha2, but seems to break calculation?

  alpha3 = Math.acos( -Math.cos(alpha1)*Math.cos(alpha2) + 
                       Math.sin(alpha1)*Math.sin(alpha2)*Math.cos(dist12) );
  dist13 = Math.atan2( Math.sin(dist12)*Math.sin(alpha1)*Math.sin(alpha2), 
                       Math.cos(alpha2)+Math.cos(alpha1)*Math.cos(alpha3) )
  lat3 = Math.asin( Math.sin(lat1)*Math.cos(dist13) + 
                    Math.cos(lat1)*Math.sin(dist13)*Math.cos(brng13) );
  dLon13 = Math.atan2( Math.sin(brng13)*Math.sin(dist13)*Math.cos(lat1), 
                       Math.cos(dist13)-Math.sin(lat1)*Math.sin(lat3) );
  lon3 = lon1+dLon13;
  lon3 = (lon3+3*Math.PI) % (2*Math.PI) - Math.PI;  // normalise to -180..+180Âº

  return new LatLon(lat3.toDeg(), lon3.toDeg());
}

And my php...
<?php

function intersect($p1_lat, $p1_lon, $brng1, $p2_lat, $p2_lon, $brng2) {
    $lat1 = deg2rad($p1_lat);
    $lon1 = deg2rad($p1_lon);
    $lat2 = deg2rad($p2_lat);
    $lon2 = deg2rad($p2_lon);
    $brng13 = deg2rad($brng1);
    $brng23 = deg2rad($brng2);
    $dLat = $lat2 - $lat1;
    $dLon = $lon2 - $lon1;

    $dist12 = 2 * asin(sqrt(sin($dLat / 2) * sin($dLat / 2) +
                            cos($lat1) * cos($lat2) * sin($dLon / 2) * sin($dLon / 2)));
    if ($dist12 == 0) {
        return false;
    }

    // initial/final bearings between points
    $brngA = acos(( sin($lat2) - sin($lat1) * cos($dist12) ) /
            ( sin($dist12) * cos($lat1) ));
    if (is_nan($brngA)) {
        $brngA = 0;  // protect against rounding
    }
    $brngB = acos(( sin($lat1) - sin($lat2) * cos($dist12) ) /
            ( sin($dist12) * cos($lat2) ));

    if (sin($lon2 - $lon1) > 0) {
        $brng12 = $brngA;
        $brng21 = 2 * pi() - $brngB;
    } else {
        $brng12 = 2 * pi() - $brngA;
        $brng21 = $brngB;
    }

    $alpha1 = ($brng13 - $brng12 + pi()) % (2 * pi()) - pi();  // angle 2-1-3
    $alpha2 = ($brng21 - $brng23 + pi()) % (2 * pi()) - pi();  // angle 1-2-3

    if (sin($alpha1) == 0 && sin($alpha2) == 0) {
        return false;  // infinite intersections
    }
    if (sin($alpha1) * sin($alpha2) < 0) {
        return false;       // ambiguous intersection
    }

    $alpha3 = acos(-cos($alpha1) * cos($alpha2) +
            sin($alpha1) * sin($alpha2) * cos($dist12));
    $dist13 = atan2( sin($dist12)*sin($alpha1)*sin($alpha2),cos($alpha2)+cos($alpha1)*cos($alpha3) );
    $lat3 = asin(sin($lat1) * cos($dist13) +
            cos($lat1) * sin($dist13) * cos($brng13));
    $dLon13 = atan2(sin($brng13) * sin($dist13) * cos($lat1), cos($dist13) - sin($lat1) * sin($lat3));
    $lon3 = $lon1 + $dLon13;
    $lon3 = ($lon3 + 3 * pi()) % (2 * pi()) - pi();  // normalise to -180..+180Âº

    return array(rad2deg($lat3),rad2deg($lon3));
}

print_r (intersect(34.8403183513,-111.8159478164,148,34.8403254442,-111.8158955968,195));
?>

A:

I was trying to port another function from the same library and the issue I had was that I needed to use fmod() instead of the regular % modulus operator.

