我已经包含了大圆距离公式的代码(现在)。代码一直有效,直到其中一个纬度/经度将 S/E 附加为它们各自的方向(我试图将 S/E 用作“负方向”)。非常感谢任何帮助!
#include <iostream>
#include <math.h>
#include "lat.hpp"
#include <string>
#define PI 3.14159265
double greatCircleDistance(double lat1, double long1, char latDir1 ,
char longDir1, double lat2, double long2,
char latDir2, char longDir2)
{
double dLat;
double dLong;
latDirectionCheck(latDir1, lat1);
latDirectionCheck(latDir2, lat2);
longDirectionCheck(longDir1, long1);
longDirectionCheck(longDir2, long2);
dLat = (lat1 - lat2) ;
dLong = (long1 - long2);
positivify(dLat);
positivify(dLong);
positivify(lat1);
positivify(lat2);
//convert all numbers to Rad
dLat = convertRadians(dLat);
dLong = convertRadians(dLong);
lat1 = convertRadians(lat1);
lat2 = convertRadians(lat2);
double R = 3959.9;
double num1 = sin(dLat/2);
double num2 = sin(dLong/2);
double a = pow(num1,2) + cos(lat1) * cos(lat2) *
pow(num2,2);
double c = 2 * atan2(sqrt(a), sqrt(1-a));
double d = R * c;
return d;
}
double latDirectionCheck(char check, double convert) {
if(check == 'S')
convert = -convert;
return convert;
}
double longDirectionCheck(char check, double convert) {
if(check == 'E')
convert = -convert;
return convert;
}
double positivify(double num) {
if (num < 0)
num = num*(-1);
return num;
}
bool compareFarDistance(double num1, double num2) {
bool statement = false;
if(num1 > num2)
statement = true;
return statement;
}
bool compareCloseDistance(double num1, double num2) {
bool statement = false;
if(num1 < num2)
statement = true;
return statement;
}
double convertRadians(double num) {
num = PI*(num/180);
return num;
}
double convertDegrees(double num) {
num = 180 * (num/PI);
return num;
}
输入:23.4356/S 46.4731/W
距离输出:4335.48