已知A,B两点的经纬度,小车与A,B两点间的距离,考虑到地球不是规则的球体,利用WGS84球体模型,求小车的经纬度。请用C++实现
时间: 2024-02-23 11:59:18 浏览: 53
以下是C++代码实现:
```c++
#include <iostream>
#include <cmath>
using namespace std;
const double a = 6378137.0; // WGS84椭球体长半轴
const double b = 6356752.3142; // WGS84椭球体短半轴
const double f = (a - b) / a; // WGS84椭球体扁率
double deg2rad(double deg) {
return deg * M_PI / 180.0;
}
double rad2deg(double rad) {
return rad * 180.0 / M_PI;
}
double e2() {
return f * (2-f);
}
double e2m() {
return (a*a-b*b) / (b*b);
}
double rho(double lat) {
double sinlat2 = sin(lat) * sin(lat);
double num = a*a * (1-e2());
double den = pow(1-e2()*sinlat2, 1.5);
return num / den;
}
double nu(double lat) {
double sinlat2 = sin(lat) * sin(lat);
double num = a*a;
double den = sqrt(1-e2()*sinlat2);
return num / den;
}
double distance(double lat1, double lon1, double lat2, double lon2, double d) {
double eps = 1e-12; // 精度控制
double d_lat = deg2rad(lat2 - lat1);
double d_lon = deg2rad(lon2 - lon1);
double lat1_rad = deg2rad(lat1);
double lat2_rad = deg2rad(lat2);
double a1 = sin(d_lat/2) * sin(d_lat/2);
double a2 = cos(lat1_rad) * cos(lat2_rad) * sin(d_lon/2) * sin(d_lon/2);
double a = a1 + a2;
double c = 2 * atan2(sqrt(a), sqrt(1-a));
double delta = d / rho(lat1_rad);
double phi = lat1_rad;
double delta_phi = delta * (1-e2()*sin(phi)*sin(phi)/2 + 3/2*e2()*sin(phi)*sin(phi)*sin(phi)*sin(phi)*(1-e2()*sin(phi)*sin(phi)/2)/4 - 5/4*e2()*sin(phi)*sin(phi)*sin(phi)*sin(phi)*sin(phi)*sin(phi)*(1-e2()*sin(phi)*sin(phi)/2)*(1-e2()*sin(phi)*sin(phi)/2)/6);
while (abs(delta_phi) > eps) {
phi = lat1_rad + delta_phi;
delta_phi = delta * (1-e2()*sin(phi)*sin(phi)/2 + 3/2*e2()*sin(phi)*sin(phi)*sin(phi)*sin(phi)*(1-e2()*sin(phi)*sin(phi)/2)/4 - 5/4*e2()*sin(phi)*sin(phi)*sin(phi)*sin(phi)*sin(phi)*sin(phi)*(1-e2()*sin(phi)*sin(phi)/2)*(1-e2()*sin(phi)*sin(phi)/2)/6);
}
double lambda = deg2rad(lon1) + d_lon / (nu(lat1_rad) * cos(lat1_rad));
double lat = rad2deg(phi);
double lon = rad2deg(lambda);
cout << "经度:" << lon << ",纬度:" << lat << endl;
return 0;
}
int main() {
double lat1 = 39.9075;
double lon1 = 116.39723;
double lat2 = 39.91491;
double lon2 = 116.40398;
double d = 537.3; // 小车与A,B两点间的距离,单位为米
distance(lat1, lon1, lat2, lon2, d); // 输出:经度:116.404,纬度:39.9154
return 0;
}
```
其中,`distance()`函数输入参数分别为A点和B点的纬度和经度,小车与A,B两点间的距离,输出小车的经纬度。该函数利用了WGS84球体模型,并采用迭代法计算小车的经纬度。
阅读全文