gpt4 book ai didi

python - 计算点和线段之间的经纬度距离

转载 作者:行者123 更新时间:2023-12-03 02:54:05 25 4
gpt4 key购买 nike

我有一条定义了起点和终点的线段:

A: 
x1 = 10.7196405787775
y1 = 59.9050401935882

B:
x2 = 10.7109989561813
y2 = 59.9018650448204

其中 x 定义经度,y 定义纬度。

我也有一个观点:

P:
x0 = 10.6542116666667
y0 = 59.429105

如何计算线段和点之间的最短距离?我知道如何在笛卡尔坐标中执行此操作,但不知道如何在长/纬度坐标中执行此操作。

最佳答案

这是一个公式 Wikipedia 的实现:

def distance(p0, p1, p2): # p3 is the point
x0, y0 = p0
x1, y1 = p1
x2, y2 = p2
nom = abs((y2 - y1) * x0 - (x2 - x1) * y0 + x2 * y1 - y2 * x1)
denom = ((y2 - y1)**2 + (x2 - x1) ** 2) ** 0.5
result = nom / denom
return result

print distance((0, 0), (3, 4), (5, 6))

# should probably test less obvious cases
assert 1 == distance((0, 0), (1, 1), (2, 1))
# change 0.001 to whatever accuracy you demand on testing.
# Notice that it's never perfect...
assert 0.5 * (2 ** 0.5) - distance((0, 0), (1, 0), (0, 1)) < 0.001

关于python - 计算点和线段之间的经纬度距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27461634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com