C++文件:zhuanhuan.cpp
#include <iostream>
#include <cmath>
constexpr auto PI = 3.14;
struct Point { double x, y, z; };
struct Coordinate { double lat, lon, height; };
Coordinate toGeodetic(const Point& point)
{
const double a = 6378137.0;
const double f = 1.0 / 298.257223563;
const double b = a * (1 - f);
const double e2 = (a * a - b * b) / (a * a);
const double p = point.x / (a * a);
const double q = point.y / (b * b);
const double r = point.z / (b * b);
Coordinate coord; coord.height = point.z;
coord.lat = atan((e2 * q + r) / (p + q + r)) * 180 / PI; coord.lon = atan((q + r) / p) * 180 / PI;
return coord;
}
Point toCartesian(const Coordinate& coord)
{
const double a = 6378137.0;
const double f = 1.0 / 298.257223563;
const double b = a * (1 - f);
const double e2 = (a * a - b * b) / (a * a);
Point point;
point.x = a * a * tan(coord.lat * PI / 180) * cos(coord.lon * PI / 180);
point.y = b * b * tan(coord.lat * PI / 180) * sin(coord.lon * PI / 180);
point.z = b * b * tan(coord.lat * PI / 180) * cos(coord.lon * PI / 180);
return point;
}
void convertToGeodetic(Point point, Coordinate& coord)
{
coord = toGeodetic(point);
}
void convertToCartesian(Coordinate coord, Point& point)
{
point = toCartesian(coord);
}
int main() {
Point AA = { -106935.2425, 5549269.5458, 3139220.0513 };
Coordinate AAA = { 0,0,0 };
convertToGeodetic(AA, AAA);
std::cout << "AAAA: " << AAA.lat << ", " << AAA.lon << ", " << AAA.height << std::endl;
Point BB = { -2661732.9209, 3581364.9183, 4542367.5601 };
Coordinate BBB = { 0,0,0 };
convertToGeodetic(BB, BBB);
std::cout << "BBBB: " << BBB.lat << ", " << BBB.lon << ", " << BBB.height << std::endl;
Point CC = { -2831733.2789,4675665.0430,3275373.1174 };
Coordinate CCC = { 0,0,0 };
convertToGeodetic(CC, CCC);
std::cout << "CCCC: " << CCC.lat << ", " << CCC.lon << ", " << CCC.height << std::endl;
Coordinate DD = { 30.531678420,114.357237473,28.1000 };
Point DDD = { 0,0,0 };
convertToCartesian(DD, DDD);
std::cout << "DDDD: " << DDD.x << ", " << DDD.y << ", " << DDD.z << std::endl;
Coordinate EE = { 43.808018931,87.600677706,852.9000 };
Point EEE = { 0,0,0 };
convertToCartesian(EE, EEE);
std::cout << "EEEE: " << EEE.x << ", " << EEE.y << ", " << EEE.z << std::endl;
Coordinate FF = { 31.099675186,121.200447167,23.2000 };
Point FFF = { 0,0,0 };
convertToCartesian(FF, FFF);
std::cout << "FFFF: " << FFF.x << ", " << FFF.y << ", " << FFF.z << std::endl;
}
输入命令行:g++ -std=c++11 zhuanhuan.cpp -o zhuanhuan
然后输入命令:./zhuanhuan