Assume that I have a line composed of N ordered points (here: N=5: a, b, c, d, and e) which subdivide it into N-1 (here: 4) segments. It is my intention to subdivide the line into M > N-1 segments, while honoring two requirements:
- The segments should be as similar in length to each other as possible
- The shape of the line mustn't be altered
Without requirement (2) we could simply create M+1 points along the path of the original line and honor requirement (1) perfectly:
import numpy as np
import scipy.interpolate
import matplotlib.pyplot as plt
XY = np.asarray([[1,1],[2.5,3],[4,2.5],[7,3],[9,1]])
distance = np.zeros(5)
for i in range(4):
distance[i+1] = np.sqrt((XY[i+1,0]-XY[i,0])**2+(XY[i+1,1]-XY[i,1])**2)
distance = np.cumsum(distance)
distance /= distance[-1]
# Get an interpolator for x
x_itp = scipy.interpolate.interp1d(distance,XY[:,0])
y_itp = scipy.interpolate.interp1d(distance,XY[:,1])
# Plot the original line
plt.scatter(XY[:,0],XY[:,1],color='b')
plt.plot(XY[:,0],XY[:,1],'b')
# Get 10 equidistant points
XY_new = np.column_stack((
x_itp(np.linspace(0,1,10)),
y_itp(np.linspace(0,1,10)) ))
# Plot the new line
plt.scatter(XY_new[:,0],XY_new[:,1],color='r',marker='x')
plt.plot(XY_new[:,0],XY_new[:,1],'r--')
Unfortunately, if the original vertices are not among these new points, the new segmented line would have a slightly different shape (cutting corners):
Do you have any suggestions or ideas on how I could create an algorithm which fulfills these requirements?



