Python:沿着连接一组点的直线的等距点

2024-06-20 15:03:12 发布

您现在位置:Python中文网/ 问答频道 /正文

作为numpy.linspace给出线性连接的两点之间的等距点。我能得到沿着连接一组点的直线的等距点吗。在

例如:

import numpy as np
npts = 10
xcoords, ycoords = [0,1], [0,1]
xquery = np.linspace(xcoords[0],xcoords[1], npts)
yquery = np.linspace(ycoords[0],ycoords[1], npts)

这里我需要在连接一组点的线上等距离的查询点

^{pr2}$

Tags: importnumpy距离asnp线性直线等距
2条回答

等长部分的二维分段线的细分:

import matplotlib.pyplot as plt
%matplotlib inline

from scipy.interpolate import interp1d
import numpy as np


x = [0, 1, 8, 2, 2]
y = [1, 0, 6, 7, 2]

# Linear length on the line
distance = np.cumsum(np.sqrt( np.ediff1d(x, to_begin=0)**2 + np.ediff1d(y, to_begin=0)**2 ))
distance = distance/distance[-1]

fx, fy = interp1d( distance, x ), interp1d( distance, y )

alpha = np.linspace(0, 1, 15)
x_regular, y_regular = fx(alpha), fy(alpha)

plt.plot(x, y, 'o-');
plt.plot(x_regular, y_regular, 'or');
plt.axis('equal');

result

编辑:为了澄清这个答案只提供了x方向上的等距点。我对这个问题的误解

我相信你要找的是插值法? 文件scipy.插值在这里:https://docs.scipy.org/doc/scipy-1.0.0/reference/tutorial/interpolate.html#d-interpolation-interp1d

但为了快速展示您的例子:

from scipy.interpolate import interp1d
x=[0,1,5,8]
y=[0,3,6,7]
f=interp1d(x,y)

然后在f中输入您希望查询的新x点,如下所示(xnew不能超过x的最小/最大界限)

^{pr2}$

看一看就是情节

import matplotlib.pyplot as plt
plt.plot(xnew,ynew,'ro',x,y,'x')
plt.show()

相关问题 更多 >