【发布时间】:2021-10-16 19:29:23
【问题描述】:
我已尝试按照here 的说明进行操作,但与this 站点相比,我得到了疯狂的结果。
这是我的代码。
#include <cmath>
double solveNR(double latitude, double epsilon) {
if (abs(latitude) == M_PI / 2) {
return latitude;
}
double theta = latitude;
while (true) {
double nextTheta = theta - (2 * theta * std::sin(2 * theta) - M_PI * std::sin(latitude)) / (2 + 2 * std::cos(2 * theta));
if (abs(theta - nextTheta) < epsilon) {
break;
}
theta = nextTheta;
}
return theta;
}
void convertToXY(double radius, double latitude, double longitude, double* x, double* y) {
latitude = latitude * M_PI / 180;
longitude = longitude * M_PI / 180;
double longitudeZero = 0 * M_PI / 180;
double theta = solveNR(latitude, 1);
*x = radius * 2 * sqrt(2) * (longitude - longitudeZero) * std::cos(theta) / M_PI;
*y = radius * sqrt(2) * std::sin(theta);
}
例如,
180 经度 = 21
90 纬度 = 8.1209e+06
假设半径为 5742340.81
我发现this 资源似乎可以计算出正确的答案。但我无法解析它有何不同。
【问题讨论】:
-
你如何定义 PI?使用您的代码以及 180 和 90 的输入,我得到
9.94552e-10和8.1209e+06的输出。我使用#include <math.h>定义了 PI -
@J'e 不是你,而是 math.h 定义了
M_PI,但这是非标准的。标准方式是std::numbers::piin C++20 -
对不起,我将 PI 定义为 3.14159