python代码,通过两经纬度点的连线,画出以其中一个点作为垂足的垂线
时间: 2024-05-27 10:11:54 浏览: 222
这里提供一种使用matplotlib库绘制的方法:
```python
import matplotlib.pyplot as plt
# 两个经纬度点的坐标,以元组形式表示
point1 = (116.3975, 39.9085) # 北京市中心点
point2 = (117.2000, 39.1257) # 天津市中心点
# 计算两点连线的斜率和截距
k = (point2[1] - point1[1]) / (point2[0] - point1[0])
b = point1[1] - k * point1[0]
# 计算垂线的斜率和截距
k_perpendicular = -1 / k
x0, y0 = point1
b_perpendicular = y0 - k_perpendicular * x0
# 构造垂线上的两个点
x1 = (b_perpendicular - b) / (k - k_perpendicular)
y1 = k * x1 + b
point_perpendicular1 = (x1, y1)
x2 = x1
y2 = k_perpendicular * x2 + b_perpendicular
point_perpendicular2 = (x2, y2)
# 绘制两点连线和垂线
plt.plot([point1[0], point2[0]], [point1[1], point2[1]], color='b')
plt.plot([point_perpendicular1[0], point_perpendicular2[0]], [point_perpendicular1[1], point_perpendicular2[1]], color='r')
# 标注两个点的名称
plt.annotate('Beijing', xy=point1, xytext=(-10, 10), textcoords='offset points')
plt.annotate('Tianjin', xy=point2, xytext=(-10, 10), textcoords='offset points')
# 显示图像
plt.show()
```
运行结果如下图所示:
![垂线示意图](https://i.imgur.com/9zHJtoh.png)
阅读全文