title: Calculate bearing between two locations (lat, long)
body: <p>I'm trying to develop my own augmented reality engine.</p>
  
  <p>Searching on internet, I've found this useful <a href="http://www.invasivecode.com/blog/archives/1435" rel="noreferrer">tutorial</a>. Reading it I see that the important thing is bearing between user location, point location and north.</p>
  
  <p>The following picture is from that tutorial.</p>
  
  <p><img src="https://i.stack.imgur.com/rKr0w.jpg" alt="enter image description here"></p>
  
  <p>Following it, I wrote an Objective-C method to obtain beta:</p>
  
  <pre><code>+ (float) calculateBetaFrom:(CLLocationCoordinate2D)user to:(CLLocationCoordinate2D)destination
  {
      double beta = 0;
      double a, b = 0;
  
      a = destination.latitude - user.latitude;
      b = destination.longitude - user.longitude;
  
      beta = atan2(a, b) * 180.0 / M_PI;
      if (beta &lt; 0.0)
          beta += 360.0;
      else if (beta &gt; 360.0)
          beta -= 360;
  
      return beta;
  }
  </code></pre>
  
  <p>But, when I try it, it doesn't work very well.</p>
  
  <p>So, I checked iPhone AR Toolkit, to see how it works (I've been working with this toolkit, but it is so big for me).</p>
  
  <p>And, in <a href="https://github.com/nielswh/iPhone-AR-Toolkit/blob/master/ARKit/ARGeoCoordinate.m" rel="noreferrer">ARGeoCoordinate.m</a> there is another implementation of how to obtain beta:</p>
  
  <pre><code>- (float)angleFromCoordinate:(CLLocationCoordinate2D)first toCoordinate:(CLLocationCoordinate2D)second {
  
      float longitudinalDifference    = second.longitude - first.longitude;
      float latitudinalDifference     = second.latitude  - first.latitude;
      float possibleAzimuth           = (M_PI * .5f) - atan(latitudinalDifference / longitudinalDifference);
  
      if (longitudinalDifference &gt; 0) 
          return possibleAzimuth;
      else if (longitudinalDifference &lt; 0) 
          return possibleAzimuth + M_PI;
      else if (latitudinalDifference &lt; 0) 
          return M_PI;
  
      return 0.0f;
  }
  </code></pre>
  
  <p>It uses this formula:</p>
  
  <pre><code>float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference / longitudinalDifference);
  </code></pre>
  
  <p>Why is (M_PI * .5f) in this formula? I don't understand it.</p>
  
  <p>And continue searching, I've found another <a href="http://www.movable-type.co.uk/scripts/latlong.html" rel="noreferrer">page</a> talking about how to calculate distance and bearing of 2 locations. In this page there is another implementation:</p>
  
  <pre><code>/**
   * Returns the (initial) bearing from this point to the supplied point, in degrees
   *   see http://williams.best.vwh.net/avform.htm#Crs
   *
   * @param   {LatLon} point: Latitude/longitude of destination point
   * @returns {Number} Initial bearing in degrees from North
   */
  LatLon.prototype.bearingTo = function(point) {
    var lat1 = this._lat.toRad(), lat2 = point._lat.toRad();
    var dLon = (point._lon-this._lon).toRad();
  
    var y = Math.sin(dLon) * Math.cos(lat2);
    var x = Math.cos(lat1)*Math.sin(lat2) -
            Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
    var brng = Math.atan2(y, x);
  
    return (brng.toDeg()+360) % 360;
  }
  </code></pre>
  
  <p>Which one is the right one?</p>