Jit*_*thu 0 python numpy matplotlib scipy python-3.x
如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)
Run Code Online (Sandbox Code Playgroud)
在这里,我需要在连接一组点的线上等距查询点
xcoords, ycoords = [0,1,5,8], [0,3,6,7]
Run Code Online (Sandbox Code Playgroud)
将2D分割线细分为等长部分:
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');
Run Code Online (Sandbox Code Playgroud)