Python 点到线的距离(从两点开始)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39840030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:48:28  来源:igfitidea点击:

Distance between point and a line (from two points)

pythonnumpyvectorscipypoint

提问by user1185675

I'm using Python+Numpy (can maybe also use Scipy) and have three 2D points

我正在使用 Python+Numpy(也可以使用 Scipy)并且有三个 2D 点

(P1, P2, P3); 

I am trying to get the distance from P3 perpendicular to a line drawn between P1 and P2. Let P1=(x1,y1), P2=(x2,y2)and P3=(x3,y3)

我试图让 P3 的距离垂直于 P1 和 P2 之间绘制的线。让P1=(x1,y1)P2=(x2,y2)并且P3=(x3,y3)

In vector notation this would be pretty easy, but I'm fairly new to python/numpy and can't get anythng that works (or even close).

在矢量表示法中,这很容易,但我对 python/numpy 还很陌生,无法获得任何有效(甚至接近)的东西。

Any tips appreciated, thanks!

任何提示表示赞赏,谢谢!

回答by DotPi

Try using the normfunction from numpy.linalg

尝试使用规范的功能numpy.linalg

d = norm(np.cross(p2-p1, p1-p3))/norm(p2-p1)

回答by Szymon Szott

For the above-mentioned answers to work, the points need to be numpy arrays, here's a working example:

为了使上述答案起作用,点必须是 numpy 数组,这是一个工作示例:

import numpy as np
p1=np.array([0,0])
p2=np.array([10,10])
p3=np.array([5,7])
d=np.cross(p2-p1,p3-p1)/np.linalg.norm(p2-p1)

回答by Martin Hardcastle

np.crossreturns the z-coordinate of the cross product only for 2D vectors. So the first normin the accepted answer is not needed, and is actually dangerous if p3is an array of vectors rather than a single vector. Best just to use

np.cross仅返回二维向量的叉积的 z 坐标。因此,norm不需要接受的答案中的第一个,如果p3是向量数组而不是单个向量,则实际上很危险。最好只是使用

d=np.cross(p2-p1,p3-p1)/norm(p2-p1)

which for an array of points p3will give you an array of distances from the line.

对于一组点,p3它将为您提供与线的距离数组。

回答by id101112

abs((x2-x1)*(y1-y0) - (x1-x0)*(y2-y1)) / np.sqrt(np.square(x2-x1) + np.square(y2-y1))

Can be used directly through the formula, just have to plug in the values and boom it will work.

可以通过公式直接使用,只需要插入值并繁荣它就会起作用。

回答by Aydar Akhmetzyanov

To find distance to line from point if you have slope and intercept you can use formula from wiki https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_linePython:

如果您有斜率和截距,要从点到线的距离,您可以使用维基https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_linePython 中的公式 :

def distance(point,coef):
    return abs((coef[0]*point[0])-point[1]+coef[1])/math.sqrt((coef[0]*coef[0])+1)

coef is a tuple with slope and intercept

coef 是一个具有斜率和截距的元组

回答by Song

Shortest Distance from Point to a Line

点到线的最短距离

This is the code I got from https://www.geeksforgeeks.org:

这是我从https://www.geeksforgeeks.org得到的代码:

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)

Now you have to find A, B, C, x, and y.

现在您必须找到 A、B、C、x 和 y。

import numpy as np
closest = []
x = (x ,y)
y = (x, y)
coef = np.polyfit(x, y, 1)
A = coef[0]
B = coef[1]
C = A*x[0] + B*x[1]

Now you can plug in the values:

现在您可以插入值:

shortest_dis = shortest_distance(x, y, A, B, C)

The full code may look like this:

完整的代码可能如下所示:

import math
import numpy as np

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)

closest = []
x = (x ,y)
y = (x, y)
coef = np.polyfit(x, y, 1)
A = coef[0]
B = coef[1]
C = A*x[0] + B*x[1]
shortest_dis = shortest_distance(x, y, A, B, C)

Please let me know if any of this is unclear.

如果有任何不清楚的地方,请告诉我。