已知A、B、C、三点的经纬度,请用qt语言计算出A到BC的直线或延长线的距离
时间: 2024-10-21 14:17:42 浏览: 14
在Qt中,计算两点之间的距离通常需要用到地理编码库或者Haversine公式,因为简单的欧几里得距离并不能准确反映地球上两点间的实际距离。假设我们已经获得了经纬度坐标,并且假设它们都是`QPointF`类型的实例,每个点包含经度(longitude)和纬度(latitude),可以采用以下步骤:
```cpp
#include <QPointF>
#include <cmath>
// Haversine公式计算地球上两点间的距离
double haversine(const QPointF& point1, const QPointF& point2) {
double R = 6371; // 地球平均半径,单位公里
double lat1 = qDegreesToRadians(point1.y());
double lon1 = qDegreesToRadians(point1.x());
double lat2 = qDegreesToRadians(point2.y());
double lon2 = qDegreesToRadians(point2.x());
double dlat = lat2 - lat1;
double dlon = lon2 - lon1;
double a = pow(sin(dlat / 2), 2) + cos(lat1) * cos(lat2) * pow(sin(dlon / 2), 2);
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
return R * c;
}
// 示例
QPointF A(52.2296756, 21.0122287); // 经纬度示例,替换为你提供的A点
QPointF B(52.406374, 16.9251681); // B点
QPointF C(52.374925, 17.0718199); // C点
double AB_distance = haversine(A, B);
double BC_distance = haversine(B, C);
double AC_distance = AB_distance + BC_distance; // 如果需要计算AC延长线距离,则取两者之和
qDebug() << "AB distance (km): " << AB_distance << "\n";
qDebug() << "BC distance (km): " << BC_distance << "\n";
qDebug() << "AC extended distance (km): " << AC_distance << "\n";
//
阅读全文