经纬度多边形计算面积的c++代码
时间: 2023-08-09 11:10:47 浏览: 204
以下是使用GeographicLib库计算经纬度多边形面积的C++代码示例:
```c++
#include <iostream>
#include <vector>
#include <GeographicLib/Geodesic.hpp>
using namespace std;
using namespace GeographicLib;
double GetPolygonArea(vector<pair<double, double>> const& polygon)
{
double area = 0.0;
int n = polygon.size();
Geodesic geod(Constants::WGS84_a(), Constants::WGS84_f());
for (int i = 0; i < n; i++)
{
int j = (i + 1) % n;
double lat1 = polygon[i].first, lon1 = polygon[i].second;
double lat2 = polygon[j].first, lon2 = polygon[j].second;
double s12;
geod.Inverse(lat1, lon1, lat2, lon2, s12);
area += s12 * Constants::WGS84_a() * Constants::WGS84_a() * sin((90.0 - lat2) * Constants::degree()) * Constants::degree();
}
return abs(area) / 2.0;
}
int main()
{
vector<pair<double, double>> polygon = { {39.9042, 116.4074}, {31.2304, 121.4737}, {22.5431, 114.0579} };
double area = GetPolygonArea(polygon);
cout << "Polygon area: " << area << " m^2" << endl;
return 0;
}
```
这里定义了一个名为`GetPolygonArea`的函数来计算多边形的面积,它接受一个`vector`类型的经纬度点向量,并返回多边形的面积。该函数使用了GeographicLib库中的`Geodesic`类来计算两点之间的距离,并使用海龟公式计算多边形面积。在主函数中,我们创建了一个三角形多边形,调用`GetPolygonArea`函数计算其面积,并输出结果。
阅读全文