wad wrong with this code?
Code:
#include <stdio.h>
#include <math.h>
#define PI 3.141593
//// macro (used in earlier tutorial), suitable for one liner function
#define deg_to_rad(deg) (deg * (PI/180.0))
//// function prototypes
double angle(double x1, double y1, double z1, double x2, double y2, double z2);
//// prototype of function to find the great circle distance between two points
double gc_dist(double, double, double, double);
int main(void) {
double lat1, long1, lat2, long2;
printf("Enter latitude north and longitude west ");
printf("for location 1 : ");
scanf("%lf %lf", &lat1, &long1);
printf("Enter latitude north and longitude west ");
printf("for location 2 : ");
scanf("%lf %lf", &lat2, &long2);
printf("Great Circle Distance: %.0f km\n", gc_dist(lat1, long1, lat2, long2));
}
double angle(double x1, double y1, double z1, double x2, double y2, double z2) {
double dot, dist1, dist2;
dot = x1 * x2 + y1 * y2 + z1 * z2; // find the dot product of the two vectors
dist1 = sqrt(x1 * x1 + y1 * y1 + z1 * z1); // find length of each vector
dist2 = sqrt(x2 * x2 + y2 * y2 + z2 * z2);
return acos(dot / (dist1 * dist2));
}
double gc_dist(double lat1, double long1, double lat2, double long2) {
double rho = 6371, phi, theta, gamma, dot, dist1, dist2, x1,y1,z1,x2,y2,z2;
phi = deg_to_rad(90 - lat1); // convert to spherical coordinates
theta = deg_to_rad(360 - long1);
x1 = rho * sin(phi) * cos(theta); // find rectangular coordinates for p1
y1 = rho * sin(phi) * sin(theta);
z1 = rho * cos(phi);
phi = deg_to_rad(90 - lat2); // repeat for p2
theta = deg_to_rad(360 - long2);
x2 = rho * sin(phi) * cos(theta);
y2 = rho * sin(phi) * sin(theta);
z2 = rho * cos(phi);
return angle(x1, y1, z1, x2, y2, z2) * rho;
}
given
Enter latitude north and longitude west for location 1 : 36.12 86.67
Enter latitude north and longitude west for location 2 : 33.94 118.40
Great Circle Distance: 2886 km
but i got 3497km...
did i specify the wrong macro ?
or i return the wrong value ?
no difference in the result ... is still not 2886
Code:
double angle(double x1, double y1, double z1, double x2, double y2, double z2) {
double dot, dist1, dist2;
dot = (x1 * x2) + (y1 * y2) + (z1 * z2); // find the dot product of the two vectors
dist1 = sqrt((x1 * x1) + (y1 * y1) + (z1 * z1)); // find length of each vector
dist2 = sqrt((x2 * x2) + (y2 * y2) + (z2 * z2));
}