Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdlib.h>
- #include <stdio.h>
- #define RADIUS 6371.0088
- int main(int argc, char**argv)
- {
- if (argc < 3) {
- printf("Usage:\n\t%s [coordinate 1] [coordinate 2] ...\n", argv[0]);
- return 1;
- }
- double *lat = calloc(sizeof(double), argc - 1);
- double *lon = calloc(sizeof(double), argc - 1);
- int i;
- for (i = 1; i < argc; ++i) {
- sscanf(argv[i], "%lf,%lf", &lat[i-1], &lon[i-1]);
- if (lat[i-1] < -90 || lat[i-1] > 90 || lon[i-1] < -180 || lon[i-1] > 180 ) {
- printf("Coordinate out of range.\n");
- return 1;
- }
- lat[i-1] *= M_PI / 180;
- lon[i-1] *= M_PI / 180;
- }
- double latdiff, londiff, a, b, c, total;
- for (i = 2; i < argc; ++i ) {
- latdiff = lat[i-1] - lat[i-2];
- londiff = lon[i-1] - lon[i-2];
- a = sin(latdiff / 2);
- b = sin(londiff / 2);
- a *= a;
- a += cos(lat[i-1]) * cos(lat[i-2]) * b * b;
- c = 2 * atan2(sqrt(a), sqrt(1-a));
- total += c * RADIUS;
- }
- printf("Distance: %lf km\n", total);
- free(lat);
- free(lon);
- return 0;
- }
Add Comment
Please, Sign In to add comment