繁体   English   中英

如何找到多条线与曲线的交点?

[英]how can I find intersection of multiple lines with a curve?

我有一个包含 x 和 y 的文件。 对于从 y 轴穿过的每条线,我可以找到交点,但我希望有一种自动方法来找到从 y 轴穿过的一堆线的交点,如下图所示:

透视结果在此处输入图像描述

我为逐个查找交叉点而编写的代码如下:

import numpy as np
import matplotlib.pyplot as plt
with open('txtfile1.out', 'r') as f:
    lines = f.readlines()
    x = [float(line.split()[0]) for line in lines]
    y = [float(line.split()[1]) for line in lines]
xx = []
for i in range(1,len(x)):
    if (y[i] > 0 and y[i-1] < 0) or (y[i] < 0 and y[i-1] > 0):
        xx.append((x[i]+x[i-1])/2)

yx = [0 for _ in range(len(xx))]
plt.plot(x,y)
plt.plot(xx,yx, color="C2", marker="o", ls="", ms=10)

我拥有的东西

当前结果在此处输入图像描述

您可以尝试设置一个额外的循环来检查您输入的多个交集值,并使用字典来保存与交集值作为键的匹配列表。 从理论上讲,这应该 plot 您想要的 y 的所有交点进入同一个图表

import numpy as np
import matplotlib.pyplot as plt
with open('txtfile1.out', 'r') as f:
    lines = f.readlines()
    x = [float(line.split()[0]) for line in lines]
    y = [float(line.split()[1]) for line in lines]
intersections = [0, -20, 10]
intersection_matches = {intersection: [] for intersection in intersections}
# or just define directly: intersection_matches ={ 0: [] , -20: [], 10: [] }
for i in range(1, len(x)):
  for intersection, xx in intersection_matches.items():
    if (y[i] > intersection and y[i-1] < intersection or (y[i] < intersection and y[i-1] > intersection)):
       xx.append((x[i]+x[i-1])/2)

plt.plot(x,y)
for intersection, xx in intersection_matches.items():
    yx = [intersection] * len(xx)
    plt.plot(xx, yx, color="C2", marker="o", ls="", ms=10)

import numpy as np
import matplotlib.pyplot as plt
with open('txtfile1.out', 'r') as f:
    lines = f.readlines()
    x = [float(line.split()[0]) for line in lines]
    y = [float(line.split()[1]) for line in lines]
xx = []
treshold = #what ever you want
for i in range(1,len(x)):
    if (y[i] > treshold and y[i-1] < treshold) or (y[i] < treshold and y[i-1] > treshold):
        xx.append((x[i]+x[i-1])/2)

yx = [treshold for _ in range(len(xx))]
plt.plot(x,y)
plt.plot(xx,yx, color="C2", marker="o", ls="", ms=10)

y= 阈值是您要观察交叉点的位置

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM