如何在两点之间线性插值 x、y 和 z?



假设在开始时间 0s 我有 point_1(10,20,35(,在结束时 10s 我有 point_2(20,40,50(

我将如何使用线性插值来查找点在时间 5s 的位置?

我想在不使用任何 python 库的情况下手动执行此操作。我正在尝试了解如何对 x、y 和 z 平面使用线性插值。

我知道我会从这个开始

start_time = 0
start_x = 10
start_y = 20
start_z = 35
end_time = 10
end_x = 20
end_y = 40
end_z = 50
# I want the point at time 5s
time = 5
def interpolate():
pass

x1x2之间进行插值的公式(1 - t) * x1 + t * x2,其中t范围从 0 到 1。我们需要首先将t放入此范围:

def interpolate(v1, v2, start_time, end_time, t):
t = (t - start_time) / (end_time - start_time)
return tuple((1 - t) * x1 + t * x2 for x1, x2 in zip(v1, v2))

例:

>>> interpolate((10, 20, 35), (20, 40, 50), 0, 10, 5)
(15.0, 30.0, 42.5)

最新更新