用python写出代码实现: 用户在需要输入A点经纬度、A点海拔和B点经纬度、B点距海拔, 计算后终端输出:A点与在B点的距离,单位:千米; B点在A点的什么方位;A点和B点之间的角度是多少度。最后形成示意图,在图上标注好得出来的信息。
时间: 2024-03-09 21:44:57 浏览: 58
以下是使用Python编写的代码实现:
```python
import math
# 地球平均半径
EARTH_RADIUS = 6371.393
def get_distance(lat1, lng1, alt1, lat2, lng2, alt2):
"""
根据经纬度和海拔计算两点之间的距离
"""
rad_lat1 = math.radians(lat1)
rad_lng1 = math.radians(lng1)
rad_lat2 = math.radians(lat2)
rad_lng2 = math.radians(lng2)
delta_lat = rad_lat1 - rad_lat2
delta_lng = rad_lng1 - rad_lng2
a = math.sin(delta_lat / 2) ** 2 + math.cos(rad_lat1) * math.cos(rad_lat2) * math.sin(delta_lng / 2) ** 2
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
distance = EARTH_RADIUS * c
height_diff = abs(alt1 - alt2)
return math.sqrt(distance ** 2 + height_diff ** 2)
def get_bearing(lat1, lng1, lat2, lng2):
"""
根据经纬度计算两点之间的方位角
"""
rad_lat1 = math.radians(lat1)
rad_lng1 = math.radians(lng1)
rad_lat2 = math.radians(lat2)
rad_lng2 = math.radians(lng2)
delta_lng = rad_lng2 - rad_lng1
y = math.sin(delta_lng) * math.cos(rad_lat2)
x = math.cos(rad_lat1) * math.sin(rad_lat2) - math.sin(rad_lat1) * math.cos(rad_lat2) * math.cos(delta_lng)
bearing = math.atan2(y, x)
return (math.degrees(bearing) + 360) % 360
# 输入A点经纬度和海拔
lat1 = float(input("请输入A点的纬度:"))
lng1 = float(input("请输入A点的经度:"))
alt1 = float(input("请输入A点的海拔:"))
# 输入B点经纬度和距海拔
lat2 = float(input("请输入B点的纬度:"))
lng2 = float(input("请输入B点的经度:"))
alt2 = float(input("请输入B点距海拔:"))
# 计算两点之间的距离
distance = get_distance(lat1, lng1, alt1, lat2, lng2, alt2)
print("A点与B点之间的距离为:%.2f千米" % distance)
# 计算B点在A点的方位
bearing = get_bearing(lat1, lng1, lat2, lng2)
print("B点在A点的方位为:%.2f度" % bearing)
# 计算A点和B点之间的角度
angle = math.degrees(math.acos((EARTH_RADIUS ** 2 + EARTH_RADIUS ** 2 - distance ** 2) / (2 * EARTH_RADIUS * EARTH_RADIUS)))
print("A点和B点之间的角度为:%.2f度" % angle)
```
以上代码中,我们使用了`get_distance`函数来计算两点之间的距离,使用`get_bearing`函数来计算B点在A点的方位,使用数学公式计算A点和B点之间的角度。最终输出距离、方位和角度,并根据这些信息在示意图上标注出来。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)