[英]Distance between line formed by two points to every other point, for every possible line
[英]Distance between point and a line (from two points)
我正在使用 Python+Numpy(也許也可以使用 Scipy)並且有三個 2D 點
(P1, P2, P3);
我試圖讓 P3 的距離垂直於 P1 和 P2 之間繪制的線。 讓P1=(x1,y1)
, P2=(x2,y2)
和P3=(x3,y3)
在矢量符號中,這將非常簡單,但我對 python/numpy 還很陌生,無法獲得任何有效的(甚至接近)。
任何提示表示贊賞,謝謝!
嘗試使用numpy.linalg
的norm函數
d = norm(np.cross(p2-p1, p1-p3))/norm(p2-p1)
np.cross
只為二維向量返回叉積的 z 坐標。 因此,不需要接受答案中的第一個norm
,如果p3
是向量數組而不是單個向量,則實際上是危險的。 最好只是使用
d=np.cross(p2-p1,p3-p1)/norm(p2-p1)
對於一組點p3
它將為您提供與該線的距離數組。
要使上述答案起作用,點必須是 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)
abs((x2-x1)*(y1-y0) - (x1-x0)*(y2-y1)) / np.sqrt(np.square(x2-x1) + np.square(y2-y1))
可以通過公式直接使用,只需要插入值並繁榮它就會起作用。
如果您有斜率和截距,要從點到線的距離,您可以使用 wiki https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line Python 中的公式:
def distance(point,coef):
return abs((coef[0]*point[0])-point[1]+coef[1])/math.sqrt((coef[0]*coef[0])+1)
coef 是一個具有斜率和截距的元組
用下面的線方程測試 -
求點 (5, 6) 到直線 −2x + 3y + 4 = 0 的垂直距離
import numpy as np
norm = np.linalg.norm
p1 = np.array([0,-4/3])
p2 = np.array([2, 0])
p3 = np.array([5, 6])
d = np.abs(norm(np.cross(p2-p1, p1-p3)))/norm(p2-p1)
# output d = 3.328201177351375
這是我從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)
現在您必須找到 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]
現在您可以插入值:
shortest_dis = shortest_distance(x, y, A, B, C)
完整的代碼可能如下所示:
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)
如果有任何不清楚的地方,請告訴我。
叉積對二維情況很有幫助,但它們不能很好地推廣到其他維度。 然而,點產品可以。 兩個正交向量的點積在任何空間都為零,您可以使用它來提出一個簡單的解決方案。
假設您將P4
與P1
- P2
放在同一行。 您可以使用參數t
對其進行參數化,以便
P4 = P1 + t * (P2 - P1)
目標是找到P4
使得
(P3 - P4) . (P2 - P1) == 0
根據t
展開P4
並簡化:
(P3 - P1 - t * (P2 - P1)) . (P2 - P1) == 0
(P3 - P1) . (P2 - P1) == t * ||P2 - P1||^2
t = (P3 - P1) . (P2 - P1) / ||P2 - P1||^2
因此你有
D = ||P3 - P4|| = ||P3 - (P3 - P1) . (P2 - P1) / (||P2 - P1||^2)||
我在我的實用程序庫中編寫了一個名為haggis
的函數。 您可以使用haggis.math.segment_distance
計算到整條線(不僅僅是有界線段)的距離,如下所示:
d = haggis.math.segment_distance(P3, P1, P2, segment=False)
多虧了DotPi的解決方案,我為該行編寫了一個模塊來計算這個和其他一些用 c++ 實現的簡單的行操作,它工作得非常快。 您可以通過 pip 安裝FastLine ,然后以這種方式使用它:
from FastLine import Line
# define a line by two points
l1 = Line(p1=(0,0), p2=(10,10))
# or define a line by slope and intercept
l2 = Line(m=0.5, b=-1)
# compute distance
d1 = l1.distance_to((20,50))
# returns 21.213203435596427
d2 = l2.distance_to((-15,17))
# returns 22.807893370497855
3D 距離應該使用 np.dot defthreeD_corres(points_3_d,pre_points_3_d,points_camera):
for j in range (0,len(pre_points_3_d)):
vec1 = list(map(lambda x:x[0]- x[1],zip(pre_points_3_d[j], points_camera)))
vec2 = list(map(lambda x:x[0]- x[1],zip(pre_points_3_d[j], points_3_d[j])))
vec3 = list(map(lambda x:x[0]- x[1],zip(points_3_d[j], points_camera)))
distance = np.abs(np.dot(vec1_1,vec2_2))/np.linalg.norm(vec3)
print("#########distance:\n",distance)
return distance
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.