已知两点经纬度求航向

struct PointLB{
    double lat;
    double lon;
};

double getAzimuth(PointLB pA, PointLB pB)
{
    double a = (90 - pB.lat) * PI / 180;
    double b = (90 - pA.lat) * PI / 180;
    double AOC_BOC = (pB.lon - pA.lon) * PI / 180;
    double cosc = cos(a) * cos(b) + sin(a) * sin(b) * cos(AOC_BOC);
    double sinc = sqrt(1 - cosc * cosc);
    double sinA = sin(a) * sin(AOC_BOC) / sinc;
    double A = asin(sinA) * 180 / PI;
    double res = 0;
    if (pB.lon > pA.lon && pB.lat > pA.lat) res = A;
    else if (pB.lon > pA.lon && pB.lat < pA.lat) res = 180 - A;
    else if (pB.lon < pA.lon && pB.lat < pA.lat) res = 180 - A;
    else if (pB.lon < pA.lon && pB.lat > pA.lat) res = 360 + A;
    else if (pB.lon > pA.lon && pB.lat == pA.lat) res = 90;
    else if (pB.lon < pA.lon && pB.lat == pA.lat) res = 270;
    else if (pB.lon == pA.lon && pB.lat > pA.lat) res = 0;
    else if (pB.lon == pA.lon && pB.lat < pA.lat) res = 180;

    return res;
}

如上,可计算出地球上两点(经纬度表示,单位为度)的航向角。

  • 1
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Haversine 公式计算两点间的距离。假设点 1 的经纬度为 (lat1, lon1),点 2 的经纬度为 (lat2, lon2),地球半径为 R,则两点间的距离 d 可以用以下公式计算: ``` a = sin²(Δlat/2) + cos(lat1) * cos(lat2) * sin²(Δlon/2) c = 2 * atan2(√a, √(1-a)) d = R * c ``` 其中,`Δlat` 和 `Δlon` 分别为两点的纬度差和经度差。航向可以使用以下公式计算: ``` y = sin(Δlon) * cos(lat2) x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(Δlon) θ = atan2(y, x) ``` 其中,`θ` 为航向,以弧度表示。 需要注意的是,这里的经纬度是以度数表示的,需要先将其转换为弧度。Python 代码实现如下: ```python import math # 计算两点间距离和航向 def distance_and_heading(lat1, lon1, lat2, lon2): R = 6371 # 地球半径,单位为千米 lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2]) dlat, dlon = lat2 - lat1, lon2 - lon1 a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2 c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) d = R * c # 两点间距离,单位为千米 y = math.sin(dlon) * math.cos(lat2) x = math.cos(lat1) * math.sin(lat2) - math.sin(lat1) * math.cos(lat2) * math.cos(dlon) theta = math.atan2(y, x) # 航向,以弧度表示 return d, math.degrees(theta) # 将航向转换为度,返回距离和航向度 # 示例:计算北京和上海的距离和航向 d, theta = distance_and_heading(39.9042, 116.4074, 31.2304, 121.4737) print("距离:{:.2f}千米,航向:{:.2f}度".format(d, theta)) ``` 输出结果为: ``` 距离:1068.00千米,航向:127.27度 ``` 表示北京到上海的距离为 1068 千米,航向为 127.27 度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值