0

Lets say at start time 0s I have point_1(10,20,35) and at end time 10s I have point_2(20,40,50)

How would I go about using linear interpolation to find where a point would be at time 5s?

I want to do this by hand without using any python libraries. I'm trying to understand how to use linear interpolation with an x,y, and z plane.

I know i would start with this

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
hinsonan
  • 53
  • 8
  • An interpolation generally requires a parameter to tell you which point between the endpoints is desired. – Mark Ransom Dec 17 '19 at 17:59
  • If all i have is that I want it at time 5 seconds is it possible to solve? – hinsonan Dec 17 '19 at 18:01
  • Yes it's possible to solve, I'm just pointing out that your function `interpolate` is missing a parameter. – Mark Ransom Dec 17 '19 at 18:03
  • There are libraries that can do this for you, explained here. https://stackoverflow.com/questions/21836067/interpolate-3d-volume-with-numpy-and-or-scipy – Parcevel Dec 17 '19 at 18:05

1 Answers1

3

The formula to interpolate between x1 and x2 is (1 - t) * x1 + t * x2 where t ranges from 0 to 1. We need to put t into this range first:

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))

Example:

>>> interpolate((10, 20, 35), (20, 40, 50), 0, 10, 5)
(15.0, 30.0, 42.5)
kaya3
  • 47,440
  • 4
  • 68
  • 97
  • Where did you find that formula `(1 - t) * x1 + t * x2` the interpolate function I found was not like that – hinsonan Dec 17 '19 at 18:10
  • Any basic textbook will give this formula, or an equivalent one like `x1 + t * (x2 - x1)`. See Wikipedia for example: https://en.wikipedia.org/wiki/Linear_interpolation#Programming_language_support – kaya3 Dec 17 '19 at 18:12
  • Ok yeah you're right I was looking at the other equivalent. – hinsonan Dec 17 '19 at 18:14