#include #include /** * Calculate and output the distance between two points given their * latitudes and longitues in degrees. We assume the Earth is a * sphere with radius 3959 miles. * * Compile and link with * gcc -c -Wall -std=c99 -pedantic distance.c * gcc -o Distance distance.o -lm * * -Wall turns on all compiler warnings, most of them helpful * -std=c99 tells the compiler to use the 1999 version of C (languages evolve) * -pedantic tells the compiler not to use gcc-specific extensions to C99 * * -lm tells the linker to link the math library into the executable; * that is the only C library that requires such special treatment * * From the command line on the Zoo, you can copy this into your current * working directory with * * cp /c/cs223/www/Examples/distance.c . * * @version 0.1 2018-09-04 */ // declare some global constants for readability const double PI = 3.14159265358979; const double EARTH_RADIUS_MILES = 3959; // *declare* my functions (names and argument/return types - no body) // I also write Javadoc-style comments here (there are tools to // convert these comments into reference material) /** * Converts the given angle to degrees. * * @param deg a non-infinite double-precision floating point number * @return the value of that angle in radians */ double to_radians(double deg); /** * Converts the given latitude to colatitude. Latitude measures from * the equator; colatitude measures from the north pole. * * @param deg a double between -90 and 90 * @return the corresponding colatitude */ double colatitude(double deg); int main() { double lat1; // double/float: numeric values w/ fractional part // int/short/long: integers (numbers w/ no fractional part) // gcc on Zoo allows ints to be +/- 2billion // char: a single character (letter, digit, punctuation) double lon1; double lat2, lon2; // hard-code the coordinates for now (handle user input next time) // Tweed New Haven Airport (HVN) lat1 = 41.263750; lon1 = -72.886805; // London City Airport (LCY) lat2 = 51.505278; lon2 = 0.0552780; // convert degrees to radians lat1 = to_radians(lat1); lon1 = to_radians(lon1); lat2 = to_radians(lat2); lon2 = to_radians(lon2); // convert anglular distance from equator to angular distance from North Pole double colat1 = colatitude(lat1); double colat2 = colatitude(lat2); // calculate difference in longitudes double delta_lon = lon1 - lon2; // fun trig to compute angle subtended by great circle between two points // given their colatitudes and difference in longitudes double x = acos(cos(colat1) * cos(colat2) + sin(colat1) * sin(colat2) * cos(delta_lon)); double dist = x * EARTH_RADIUS_MILES; // this will be off from true distance b/c we're assuming a spherical Earth printf("%f\n", dist); // %d for int; %f for float return 0; } double to_radians(double deg) { return deg / 180 * PI; } double colatitude(double deg) { return PI / 2 - deg; }