在由 numpy 数组创建的曲线中查找弧长
finding arc length in a curve created by numpy array
我有一个 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])
如果我把它们连起来就会像下图一样
点是曲线上的顶点。
有什么方法可以找到这条曲线上任意两个给定点之间的弧长,例如两个蓝点?
要获得曲线任意两点之间的曲线长度,您需要将这些点定义为该曲线的顶点。
在下面的例子中,我在需要的位置插入两个蓝色点,然后求和这两个点之间所有顶点的欧氏距离:
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
更新
根据评论:为了找到曲线上距离原点 len
个单位的点 point
的坐标(curve[0]
),我们首先累加起来曲线段的长度以找到我们的点所在的段。设此段为 curve[i]
和 curve[i+1]
之间的线。然后我们根据以下草图找到该段的部分 lambda (l
):
有了这个 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
我有一个 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])
如果我把它们连起来就会像下图一样
点是曲线上的顶点。
有什么方法可以找到这条曲线上任意两个给定点之间的弧长,例如两个蓝点?
要获得曲线任意两点之间的曲线长度,您需要将这些点定义为该曲线的顶点。
在下面的例子中,我在需要的位置插入两个蓝色点,然后求和这两个点之间所有顶点的欧氏距离:
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
更新
根据评论:为了找到曲线上距离原点 len
个单位的点 point
的坐标(curve[0]
),我们首先累加起来曲线段的长度以找到我们的点所在的段。设此段为 curve[i]
和 curve[i+1]
之间的线。然后我们根据以下草图找到该段的部分 lambda (l
):
有了这个 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