title: How do you find relative bearing between two points?
body: <pre><code>double computeHeading(double latitude1, double longitude1, double latitude2, double longitude2)
  {
      double degToRad = PI / 180.0;
      double phi1 = latitude1*degToRad;
      double phi2 = latitude2*degToRad;
      double lam1 = longitude1*degToRad;
      double lam2 = longitude2*degToRad;
  
      double x,y;
      x = cos(phi2) * sin(lam2-lam1);
      printf("X is %lf\n", x);
      y = cos(phi1) * sin(phi2) - sin(phi1) * cos(phi2) * cos(lam2-lam1);
      printf("Y is %lf\n", y);
      return atan2(x,y)*180/PI;
  }
  </code></pre>
  
  <p>I am using the above function to determine the true bearing from North between two geographic coordinates.</p>
  
  <p>I'm currently developing a small navigation widget which uses GPS data from Android sensors. The widget has an arrow facing towards a point away from the device's current location. The arrow's direction changes with the device's current location and azimuth to always face the distant point.</p>
  
  <p>Here is a scenario:</p>
  
  <p>I'm  at a location, facing north, and another location has a bearing of 300 degrees(somewhat northwest of me). If I face towards south, without moving, my relative bearing to the distant location should be 120 degrees.</p>
  
  <p>How can I find the relative bearing with accounting for the facing direction (azimuth)?</p>