Guest User

Untitled

a guest
Jul 16th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. #define RADIUS 6371.0088
  6.  
  7. int main(int argc, char**argv)
  8. {
  9. if (argc < 3) {
  10. printf("Usage:\n\t%s [coordinate 1] [coordinate 2] ...\n", argv[0]);
  11. return 1;
  12. }
  13.  
  14. double *lat = calloc(sizeof(double), argc - 1);
  15. double *lon = calloc(sizeof(double), argc - 1);
  16. int i;
  17. for (i = 1; i < argc; ++i) {
  18. sscanf(argv[i], "%lf,%lf", &lat[i-1], &lon[i-1]);
  19. if (lat[i-1] < -90 || lat[i-1] > 90 || lon[i-1] < -180 || lon[i-1] > 180 ) {
  20. printf("Coordinate out of range.\n");
  21. return 1;
  22. }
  23.  
  24. lat[i-1] *= M_PI / 180;
  25. lon[i-1] *= M_PI / 180;
  26. }
  27.  
  28. double latdiff, londiff, a, b, c, total;
  29. for (i = 2; i < argc; ++i ) {
  30. latdiff = lat[i-1] - lat[i-2];
  31. londiff = lon[i-1] - lon[i-2];
  32.  
  33. a = sin(latdiff / 2);
  34. b = sin(londiff / 2);
  35. a *= a;
  36. a += cos(lat[i-1]) * cos(lat[i-2]) * b * b;
  37. c = 2 * atan2(sqrt(a), sqrt(1-a));
  38.  
  39. total += c * RADIUS;
  40. }
  41.  
  42. printf("Distance: %lf km\n", total);
  43.  
  44. free(lat);
  45. free(lon);
  46.  
  47. return 0;
  48. }
Add Comment
Please, Sign In to add comment