#include #include #include #include struct place { double ladeg, lamin; double lodeg, lomin; char *city, *country; } Place[] = { /* Eight places which form roughly a cube */ -12, -30, -96, -50, "Cocos (Keeling) Islands", "", 12, 24, 83, 40, "Punta Peclas", "NICARAGUA", -56, -15, 68, 42, "Cape Horn", "CHILE", 57, 41, -114, 0, "Bodaybo", "RUSSIA", 42, 20, 8, 45, "Vigo", "SPAIN", -21, -38, -21, -25, "Ghanzi", "BOTSWANA", 21, 20, 157, 55, "Honolulu", "USA", -43, -32, -172, -37, "Christchurch", "NEW ZEALAND", }; /* * This program is based on one by david.williams at ablelink.org * Translated from Basic to C by jda, who also eliminated * some frills. */ #define J_PI 3.1415926535898 double degmin2rad(double deg, double min) { return (deg + min / 60.0) * J_PI / 180; } /* We use only an average Earth radius for calculations */ double Radnet = 6371; /* Earth radius in km */ /* * double Radpolar = 6356.9; * double Radequat = 6378.5; */ /* Calculate distance between long1, lat1, long2, lat2 */ double getdist(double long1, double lat1, double long2, double lat2) { double lg, y, t, x, z, r, a2; lg = long2 - long1; y = sin(lat1); t = cos(lat1); x = t * sin(lg); z = t * cos(lg); r = sqrt(y*y + z*z); a2 = cos(atan2(y, z) - lat2); y = - r * a2; return Radnet * (J_PI / 2 + atan2(y, sqrt(1 - y*y))); } /* Calculate distance between two places */ double getd2(int ix1, int ix2) { double long1, lat1, long2, lat2; long1 = degmin2rad(Place[ix1].lodeg, Place[ix1].lomin); lat1 = degmin2rad(Place[ix1].ladeg, Place[ix1].lamin); long2 = degmin2rad(Place[ix2].lodeg, Place[ix2].lomin); lat2 = degmin2rad(Place[ix2].ladeg, Place[ix2].lamin); return getdist(long1, lat1, long2, lat2); } int main(int argc, char **argv) { int ix1, ix2; for (ix1 = 0; ix1 < sizeof(Place)/sizeof(*Place); ix1++) for (ix2 = ix1 + 1; ix2 < sizeof(Place)/sizeof(*Place); ix2++) printf(" %8.1f is Distance from %s (%s) to %s (%s)\n", getd2(ix1, ix2), Place[ix1].city, Place[ix1].country, Place[ix2].city, Place[ix2].country); exit(0); }