python - 检测两条曲线交点的上游和下游点
问题描述
我有两条曲线,由
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)
问题是结果受到我决定的增量(在这种情况下为 0.5)的极大影响,这给了我错误的结果,特别是如果我使用更多的十进制数字(这实际上是我的情况)。
我怎样才能使循环更健壮,并真正找到交叉口上游和下游的所有点?
非常感谢您的帮助
解决方案
TL;TR:遍历折线段并测试交点是否在段端点之间。
一种更稳健(比 OP 中的“delta”)的方法是找到包含交叉点(或一般给定点)的折线段。该段应该是 IMOget_intersect
功能的一部分,但如果您无权访问它,则必须自己搜索该段。
由于舍入误差,给定点并不完全位于该段上,因此您仍然有一些tol
参数,但结果应该对其(非常低的)值“几乎不敏感”。
该方法使用简单的几何,即点积和叉积及其几何含义:
- 向量的
a
点积b
除以 的|a|
投影(长度)b
到 的方向a
。再次除以|a|
将值标准化为范围[0;1]
- 和 的叉积是以a
a
和bb
为边的平行四边形的面积。将其除以长度的平方使其成为一些无量纲的距离因子。如果点正好位于线段上,则叉积为零。但是浮点数需要一个小的容差。
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()
结果:
推荐阅读
- python - 如何使用 Matplotlib 显示折线图的阴影?
- unity3d - Unity3D 如何通过拖动和选择/套索选择以 3D 方式选择多个对象?
- python - QStandardItemModel 不会在 QTreeView InternalMove 上发出 rowsMoved() 信号
- java - 为没有参数的乘法创建递归算法
- arrays - 如何通过角度材料中的特定数字迭代图标
- spring - 管理 Spring Boot 应用程序的构建配置文件/配置
- python - 如何将字符串转换为pyspark中的数组数组?
- reactjs - 为什么当我使用 axios 将数据发布到我的 mongodb 云时,我得到了一个带有 _id 和空字段的对象?
- javascript - TinyMCE 5 file_picker_callback 使用自定义处理程序上传
- azure - 通过 Azure IoT 中心将 REST API 请求发送到 Raspberry Pi 网络服务器