0

我有一个 NumPy 数组,可用于创建曲线,该数组看起来像这样。

curve=np.asarray([0,0,0], [0.5,0,0], [0.8,0.2,0], [1,0.5,0], [1.2,0.7,0])

如果我将它们连接起来,它将如下图所示在此处输入图像描述

这些点是曲线上的顶点。

有没有办法找到这条曲线上任意两个给定点之间的弧长,例如两个蓝点?

4

1 回答 1

1

要获得曲线任意两点之间的曲线长度,您需要将这些点定义为该曲线的顶点。

在下面的例子中,我在需要的位置插入两个蓝色点,然后总结这两个点之间所有顶点的欧几里得距离:

import numpy as np

curve = np.asarray([[0,0,0], [0.5,0,0], [0.8,0.2,0], [1,0.5,0], [1.2,0.7,0]])
points = np.array([[.65,.1,0],[.9,.35,0]])

#insert points in curve 
start = np.searchsorted(curve[:,0], points[0,0])
curve = np.insert(curve, start, points[0], axis=0)
end = np.searchsorted(curve[:,0], points[1,0])
curve = np.insert(curve, end, points[1], axis=0)
print(curve)
#[[0.   0.   0.  ]
# [0.5  0.   0.  ]
# [0.65 0.1  0.  ]
# [0.8  0.2  0.  ]
# [0.9  0.35 0.  ]
# [1.   0.5  0.  ]
# [1.2  0.7  0.  ]]

def curve_length(curve):
    """ sum of Euclidean distances between points """
    return np.sum(np.sqrt(np.sum((curve[:-1] - curve[1:])**2,axis=1)))

print(curve_length(curve[start:end+1]))
#0.3605551275463989

更新

根据评论:为了找到point曲线上与len原点相距单位的点的坐标(curve[0]设此段为 和 之间的curve[i]线curve[i+1]l然后我们根据以下草图 找到该段的部分 lambda ( ): 使用此 lambda,我们可以计算搜索到的点:在此处输入图像描述

import numpy as np
import matplotlib.pyplot as plt

curve = np.asarray([[0,0,0], [0.5,0,0], [0.8,0.2,0], [1,0.5,0], [1.2,0.7,0]])
len = 1

#len is reached between curve[i] and curve[i+1]
cs = np.cumsum(np.sqrt(np.sum((curve[:-1] - curve[1:])**2, axis=1)))
i = np.argmin(cs < len)
dist = len - cs[i-1]

l = dist / np.sqrt(np.sum((curve[i+1] - curve[i])**2))
point = l * (curve[i+1] - curve[i]) + curve[i]
print(point)
#[0.8773501  0.31602515 0.        ]

可视化:

plt.plot(curve[:,0], curve[:,1])
plt.plot(point[0], point[1], 'o')

在此处输入图像描述

确认:

end = np.searchsorted(curve[:,0], point[0])
curve = np.insert(curve, end, point, axis=0)
print(curve_length(curve[:end+1]))
#1.0
于 2020-09-21T07:38:27.947 回答