Geometry: Great Circle Distance

less than 1 minute read

The code has been modified slightly and now uploaded to the site.

Question

<— Return Home

42.cpp

#include <iostream>
#include <cmath>
using namespace std;

// Don't know why answer is slightly off, example output might be unedited and therefore incorrect

int main(){
    double d;
    double radius = 6378.1;
    // double radius = 6371.01;
    double x1;
    double y1;
    double x2;
    double y2;

    std::cout << "Enter point 1 (latitude and longitude) in degrees: \n";
    std::cin >> x1;
    std::cin >> y1;

    std::cout << "Enter point 2 (latitude and longitude) in degrees: \n";
    std::cin >> x2;
    std::cin >> y2;

    d = radius * acos(sin(x1 * M_PI/180) * sin(x2 * M_PI/180) + cos(x1 * M_PI/180) * cos(x2 * M_PI/180) * cos ((y1 * M_PI/180) - (y2 * M_PI/180)));

    std::cout << "The distance between the two points is " << d << " km\n";
}

<— Return Home

Input (42input.txt)

39.55
-116.25
41.5
87.37

<— Return Home