title: Bearing using two sets of coordinates (Latitude and Longitude) in C
body: <p>I have began working on an autonomous rc helicopter in c. I need help finding a way to calculate the bearing acuratley. I am using two sets of coordinates (latitude and longitude), one is the current location and the other is the destination point. I have converted the latitude and longitude into decimal format so.... </p>
  
  <pre><code>40°58'19.86"N = 40.972183
  74°14'52.74"W = 74.247983
  </code></pre>
  
  <p>Can anyone show me code in c to find the bearing or a formula i can use? </p>
  
  <p>i have looked at: <a href="http://www.movable-type.co.uk/scripts/latlong.html" rel="nofollow">http://www.movable-type.co.uk/scripts/latlong.html</a> and when i transfer it to c, the results make no sense. </p>
  
  <p>This is what i have tried:</p>
  
  <pre><code> double x = Sin(Longitude2 - Longitude1) * Cos(Latitude2);
   double y = Cos(Latitude1) * Sin(Latitude2) - Sin(Latitude1) * Cos(Latitude2) * Cos(Longitude2 - Longitude1);
   double heading = (Atan2(x, y) % 2 * 3.14159265) (180/3.14159265);
  </code></pre>