python - 从线中查找点距离 - python

标签 python geometry line distance

我有3分

p1 = 48.36736702002282, 11.112351406920268
p2 = 48.36728222003929, 11.112716801718284
p3 = 48.36720362305641,11.112587917596102

我想找到从 p3p1 & p2 的垂直距离。

为此,我的计划是,使用 p1p2 创建一条线,然后尝试找到与点 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) 

我无法理解的是如何使用 p1p2 创建行以及 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/

相关文章:

python - 嵌套列表推导式在 python 中如何工作?

algorithm - 将点平面分成相等的两半

java - 如何在图表上画线?

按斜率排序线

java - 通过 Java 套接字接收混合媒体。你的更好吗?

python - 如何对数字列表求和?

Python线程处理数据库行

PHP + CSS + Lettering.js 创建曲线文字

algorithm - 在法线已知的平面上画一条垂直于直线的直线

python - wxWidgets:BoxSizer 内的面板未按预期运行