最近在工作中算法中遇到求两条曲线的交点的问题,开始在网上找了良久没找到什么可用的办法,后来自己就在再就在纸上画,发现一个简单的算法可以求出这些交点来。看下图就知道了,
就是按照几何来计算比例:
具体代码如下:
import numpy as np
import matplotlib.pyplot as plt
x1 = np.array([712,653,625,605,617,635,677,762,800,872,947,1025,1111,1218,1309, 500])
y1 = np.array([2022,1876,1710,1544,1347,1309,1025,995,850,723,705,710,761,873,1050, 2000])
x_start = np.min(x1)
x_end = np.max(x1)+1
x_line = x1.copy()
y_line = x_line * 0.9 + 500
y=y1-y_line
nLen=len(x1)
xzero=np.zeros((nLen,))
yzero=np.zeros((nLen,))
for i in range(nLen-1):
if np.dot(y[i], y[i+1]) == 0:# %等于0的情况
if y[i]==0:
xzero[i]=i
yzero[i]=0
if y[i+1] == 0:
xzero[i+1]=i+1
yzero[i+1]