简体   繁体   中英

How to linear interpolate the x, y, and z between two points?

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM