[英]How to find the intersection points between two plotted curves in matplotlib?
[英]Detect points upstream and downstream of an intersection between two curves
我有兩條曲線,定義為
X1=[9, 10.5, 11, 12, 12, 11, 10, 8, 7, 7]
Y1=[-5, -3.5, -2.5, -0.7, 1, 3, 4, 5, 5, 5]
X2=[5, 7, 9, 9.5, 10, 11, 12]
Y2=[-2, 4, 1, 0, -0.5, -0.7, -3]
並且通過我正在使用的系統代碼中編寫的函數,我可以獲得交點的坐標。
loop1=Loop([9, 10.5, 11, 12, 12, 11, 10, 8, 7, 7],[-5, -3.5, -2.5, -0.7, 1, 3, 4, 5, 5, 5])
loop2=Loop([5, 7, 9, 9.5, 10, 11, 12], [-2, 4, 1, 0, -0.5, -0.7, -3])
x_int, y_int = get_intersect(loop1,loop2)
Intersection = [[],[]]
Intersection.append(x_int)
Intersection.append(y_int)
對於兩條曲線,我需要找到由 (x_int, y_int) 標識的交點上游和下游的點。
我試過的是這樣的:
for x_val, y_val, x, y in zip(Intersection[0], Intersection[1], loop1[0], loop1[1]):
if abs(x_val - x) < 0.5 and abs(y_val - y) < 0.5:
print(x_val, x, y_val, y)
問題是結果受到我決定的 delta 的極大影響(在這種情況下為 0.5),這給了我錯誤的結果,特別是如果我使用更多的十進制數(這實際上是我的情況)。
我怎樣才能使循環更加健壯並實際找到所有且僅找到交叉點上游和下游的點?
非常感謝您的幫助
TL;TR:循環多段線段並測試交點是否在段端點之間。
更強大(比 OP 中的“delta”)方法是找到折線的一段,其中包含交點(或一般給定的點)。 該段應 IMO 成為get_intersect
函數的一部分,但如果您無權訪問它,則必須自己搜索該段。
由於舍入誤差,給定的點並不完全位於線段上,因此您仍然有一些tol
參數,但結果應該對其(非常低的)值“幾乎不敏感”。
該方法使用簡單的幾何,即點積和叉積及其幾何含義:
a
和b
點積除以|a|
是的投影(長度) b
到的方向a
。 再次除以|a|
將值歸一化到范圍[0;1]
a
和b
叉積是a和b為邊的平行四邊形的面積。 將其除以長度的平方使其成為距離的無量綱因子。 如果點正好位於線段上,則叉積為零。 但是浮點數需要一個小的容差。X1=[9, 10.5, 11, 12, 12, 11, 10, 8, 7, 7]
Y1=[-5, -3.5, -2.5, -0.7, 1, 3, 4, 5, 5, 5]
X2=[5, 7, 9, 9.5, 10, 11, 12]
Y2=[-2, 4, 1, 0, -0.5, -0.7, -3]
x_int, y_int = 11.439024390243903, -1.7097560975609765
def splitLine(X,Y,x,y,tol=1e-12):
"""Function
X,Y ... coordinates of line points
x,y ... point on a polyline
tol ... tolerance of the normalized distance from the segment
returns ... (X_upstream,Y_upstream),(X_downstream,Y_downstream)
"""
found = False
for i in range(len(X)-1): # loop over segments
# segment end points
x1,x2 = X[i], X[i+1]
y1,y2 = Y[i], Y[i+1]
# segment "vector"
dx = x2 - x1
dy = y2 - y1
# segment length square
d2 = dx*dx + dy*dy
# (int,1st end point) vector
ix = x - x1
iy = y - y1
# normalized dot product
dot = (dx*ix + dy*iy) / d2
if dot < 0 or dot > 1: # point projection is outside segment
continue
# normalized cross product
cross = (dx*iy - dy*ix) / d2
if abs(cross) > tol: # point is perpendicularly too far away
continue
# here, we have found the segment containing the point!
found = True
break
if not found:
raise RuntimeError("intersection not found on segments") # or return None, according to needs
i += 1 # the "splitting point" has one higher index than the segment
return (X[:i],Y[:i]),(X[i:],Y[i:])
# plot
import matplotlib.pyplot as plt
plt.plot(X1,Y1,'y',linewidth=8)
plt.plot(X2,Y2,'y',linewidth=8)
plt.plot([x_int],[y_int],"r*")
(X1u,Y1u),(X1d,Y1d) = splitLine(X1,Y1,x_int,y_int)
(X2u,Y2u),(X2d,Y2d) = splitLine(X2,Y2,x_int,y_int)
plt.plot(X1u,Y1u,'g',linewidth=3)
plt.plot(X1d,Y1d,'b',linewidth=3)
plt.plot(X2u,Y2u,'g',linewidth=3)
plt.plot(X2d,Y2d,'b',linewidth=3)
plt.show()
結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.