我有3分
p1 = 48.36736702002282, 11.112351406920268
p2 = 48.36728222003929, 11.112716801718284
p3 = 48.36720362305641,11.112587917596102
我想找到从 p3
到 p1
& p2
的垂直距离。
为此,我的计划是,使用 p1
和 p2
创建一条线,然后尝试找到与点 p3
的垂直距离> 到行(从 p1
& p2
创建)。
我正在关注 HERE
来自 geeksforgeeks 的代码:
# Python program to find the distance between
# a given point and a given line in 2 D.
import math
# Function to find distance
def shortest_distance(x1, y1, a, b, c):
d = abs((a * x1 + b * y1 + c)) / (math.sqrt(a * a + b * b))
print("Perpendicular distance is"),d
# Driver Code
x1 = 5
y1 = 6
a = -2
b = 3
c = 4
shortest_distance(x1, y1, a, b, c)
我无法理解的是如何使用 p1
和 p2
创建行以及 x1, y1, a, b 的值应该是多少, 上面代码中的c
最佳答案
使用 wikipedia 中的等式(我认为这是一个很好的来源,但值得商榷):
import math
def find_distance(p1,p2,p3):
nom = abs((p2[0]-p1[0])*(p1[1]-p3[1])-(p1[0]-p3[0])*(p2[1]-p1[1]))
denom = math.sqrt((p2[0]-p1[0])**2+(p2[1]-p1[1])**2)
return nom/denom
print(find_distance(p1,p2,p3))
输出:
0.0001056989661888993
关于python - 从线中查找点距离 - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66424638/