将闭合曲线拟合到一组离散点并找到它的周长
Fitting a closed curve to a set of discrete points and finding it's perimeter
我有一组点 pts,它们形成一个循环,如下所示:
Closed curve
我使用的代码如下:
pts=np.array( [ [1145,1130],
[1099.5,1056.5],
[1026,1062],
[950.3,1054.3],
[909,1130],
[940.4,1215.6],
[1026,1264],
[1111.6,1215.6]
])
pts = np.vstack([pts, pts[0]])
#pts = np.array([...]) # Your points
x, y = pts.T
i = np.arange(len(pts))
# 5x the original number of points
interp_i = np.linspace(0, i.max(), 5 * i.max())
xi = interp1d(i, x, kind='cubic')(interp_i)
yi = interp1d(i, y, kind='cubic')(interp_i)
fig, ax = plt.subplots()
ax.plot(xi, yi)
ax.plot(x, y, 'ko')
plt.show()
我想知道如何求这条曲线的长度/这个图形的周长。有什么办法吗?
您可以将曲线的周长逼近为线段之和。
设(xj,yj)和(xj+1,yj+1)为线段在xy平面上的起点坐标和终点坐标,则其长度可写为:
L_j = sqrt{[x_(j+1) - x_(j)]^2 + [y_(j+1) - y_(j)]^2}
因此,您只需对所有 L_j 段求和即可获得闭合曲线周长的近似值。
一个 python 代码示例是:
L = np.sqrt((xi[-1] - xi[0])**2 + (yi[-1] - yi[0])**2) # let the initial value of L be the length of the line segment between the last and the first points
for j in range(0,len(xi)):
L = L + np.sqrt((xi[j+1] - xi[j])**2 + (yi[j+1] - yi[j])**2)
print L
我有一组点 pts,它们形成一个循环,如下所示: Closed curve
我使用的代码如下:
pts=np.array( [ [1145,1130],
[1099.5,1056.5],
[1026,1062],
[950.3,1054.3],
[909,1130],
[940.4,1215.6],
[1026,1264],
[1111.6,1215.6]
])
pts = np.vstack([pts, pts[0]])
#pts = np.array([...]) # Your points
x, y = pts.T
i = np.arange(len(pts))
# 5x the original number of points
interp_i = np.linspace(0, i.max(), 5 * i.max())
xi = interp1d(i, x, kind='cubic')(interp_i)
yi = interp1d(i, y, kind='cubic')(interp_i)
fig, ax = plt.subplots()
ax.plot(xi, yi)
ax.plot(x, y, 'ko')
plt.show()
我想知道如何求这条曲线的长度/这个图形的周长。有什么办法吗?
您可以将曲线的周长逼近为线段之和。
设(xj,yj)和(xj+1,yj+1)为线段在xy平面上的起点坐标和终点坐标,则其长度可写为:
L_j = sqrt{[x_(j+1) - x_(j)]^2 + [y_(j+1) - y_(j)]^2}
因此,您只需对所有 L_j 段求和即可获得闭合曲线周长的近似值。
一个 python 代码示例是:
L = np.sqrt((xi[-1] - xi[0])**2 + (yi[-1] - yi[0])**2) # let the initial value of L be the length of the line segment between the last and the first points
for j in range(0,len(xi)):
L = L + np.sqrt((xi[j+1] - xi[j])**2 + (yi[j+1] - yi[j])**2)
print L