gpt4 book ai didi

c++ - 大圆距离c++

转载 作者:行者123 更新时间:2023-11-28 07:21:24 26 4
gpt4 key购买 nike

我已经包含了大圆距离公式的代码(现在)。代码一直有效,直到其中一个纬度/经度附加了 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

最佳答案

您不使用各种方向检查函数或 positivify 函数的返回值,它们按值获取参数。所以它们没有效果。您要么需要通过引用向它们传递参数,要么使用它们的返回值。否则,它们会计算一个值并将其返回给忽略它的代码。

关于c++ - 大圆距离c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19414376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com