找到直线和分段线性曲线之间的交点

时间:2017-03-25 12:12:28

标签: python numpy curve

我有两条曲线,一条是一条线,例如y = x / 4,另一条曲线是我用线段连接的一组点(例如:x = [1, 2.5, 3.4, 5.8, 6]y = [2, 4, 5.8, 4.3, 4]在2D平面中形成5个点,形成分段线性曲线),我应该找到这两条曲线之间的交叉点。我应首先形成这个分段线性曲线,然后找到交点。幸运的是,我发现我可以使用numpy.polyfit来查找每个线段的多项式系数,如下所示:

import numpy as np
import matplotlib.pyplot as plt
x = [0, 1, 2, 4, 6]  # in my primary problem i dont have just 5 points and i have approximately 200 points !
y = [0, 0, 3, -1, 2]
x = np.array(x)
y = np.array(y)
a = [np.polyfit(x[i:(i+2)], y[i:(i+2)], 1) for i in range(len(x)-1)]
plt.plot(x, y, 'o')
plt.show()

但现在我对如何使用这些系数找到两个图之间的交叉点感到很困惑!? (在我的主要问题中,我没有5分,我有大约200分!)解决这个问题的一个想法是使用((求解))命令检查线段和线段的交点,但这非常耗时,此命令不适用于线(())。

1 个答案:

答案 0 :(得分:0)

没有理由应该只有一个交叉点。对于你的例子,如果你花了$ y = 1 $,你会有三个交叉点,如果你花了$ y = -2 $,你就没有了,如果花了$ y = 0 $,你会无限的许多。

要全部找到它们,一种方法是考虑连接xy元素的每个线段,并且已经找到了斜率和相交的元素,并扩展了要在实线上定义的线段。现在,使用the procedures in this question中的一个找到给定行和扩展行之间的交叉点。如果有0个交点,则原始未延伸线段也有0个交点,如果有1个交点,如果x值位于定义线段的x值范围内,则未延伸的线段将有1个交点(和0否则),如果有无限多个交叉点,那么线段上的每个点都将位于交叉点。为每个线段重复此过程。

def get_intersection(line1, line2):
    """Returns the intersection, if any, between two lines, None if the lines are equal, and
    the empty array if the lines are parallel.

    Args:
        line1 (numpy.array): [slope, intercept] of the first line.
        line2 (numpy.array): [slope, intercept] of the second line.

    Taken from https://stackoverflow.com/a/42727584/5085211.
    """
    if (np.array_equal(line1, line2)):
        return None
    if line1[0] == line2[0]:
        return np.empty(0)
    l1 = np.insert(line1, 1, -1)
    l2 = np.insert(line2, 1, -1)
    x, y, z = np.cross(l1, l2)
    return np.hstack([x, y]) / z

line_of_interest = [0.25, 0]  # y = x/4

for i in range(len(x)-1):
    p = get_intersection(a[i], line_of_interest)
    if np.array_equal(p, []):
        continue
    elif np.array_equal(p, None):
        print('Entire line segment between {0} and {1}'.format(x[i], x[i+1]))
    elif x[i] <= p[0] and p[0] < x[i+1]:
        print(p)

# Prints:
# [ 0.  0.]
# [ 1.09090909  0.27272727]
# [ 3.11111111  0.77777778]
# [ 5.6  1.4]
相关问题