Posted By

jatkins on 10/02/09


Tagged

coordinates formula arduino haversine geographical earth


Versions (?)

ARDUINO: Distance between two points


 / Published in: Processing
 

uses the haversine formula

  1. float london[2] = {51.507222,-0.1275};
  2. float nyc[2] = {40.757267,-73.985839};
  3.  
  4. float radius_of_earth = 6378.1; // km
  5.  
  6. void setup() {
  7. Serial.begin(9600);
  8. }
  9.  
  10. double distance_in_km(float start_lat, float start_long, float end_lat, float end_long) {
  11. // / 180 / PI converts degrees to radians
  12. start_lat/= 180 / PI;
  13. start_long/= 180 / PI;
  14. end_lat/= 180 / PI;
  15. end_long/= 180 / PI;
  16. // haversine formula - based on implementation at http://www.movable-type.co.uk/scripts/latlong.html
  17. float a = pow(sin((end_lat-start_lat)/2), 2) + cos(start_lat) * cos(end_lat) * pow(sin((end_long-start_long)/2), 2);
  18. float answer = radius_of_earth * 2 * atan2(sqrt(a), sqrt(1-a));
  19. return double(answer);
  20. }
  21.  
  22. void loop() {
  23. Serial.print("Distance between Times Square, NYC and London: ");
  24. Serial.print(distance_in_km(london[0], london[1], nyc[0], nyc[1]));
  25. Serial.println(" km");
  26. delay(1000);
  27. }

Report this snippet  

You need to login to post a comment.