简体   繁体   中英

How to calculate even distance along an interpolated path (Python2.7)?

I have a (long) list of x,y tuples that collectively describe a path (ie. mouse-activity sampled at a constant rate despite non-constant velocities).

My goal is to animate that path at a constant rate. So I have segments that are curved, and segments that are straight, and the delta-d between any two points isn't guaranteed to be the same.

Given data like:

[(0,0), (0,2), (4,6).... ] where the length of that list is ~1k-2k points, is there any way besides brute-force counting line-segment lengths between each point and then designating every n-length a "frame"?

If you use Numpy arrays to represent your data then you can vectorise the computation. That's as efficient as you're going to get.

I'm sure there's an elegant way to do this with pandas, but until then, here's a simple idea if you can live with some error. You can do this a few different ways but here's the gist of it:

Treat each tuple as a node in a linked list. Define the desired length, D , between each point. As you move through the list, if the next node is not a distance D from the current node, adjust its x,y coordinates accordingly (or insert/delete nodes as necessary) so that it is a distance D from the current node along the line segments that connect the nodes.

Like I said, you'll have to live with some error because your original points will be adjusted/deleted. If you generate points to create more resolution prior to this, you can probably lessen the error.

If you were to use an approximation that the path between the points represented between eg tuple1 and the next tuple, tuple2 is a straight line, then:

abs(complex(tuple2[0]-tuple1[0], tuple2[1]-tuple1[1]))

gives you the length traversed between those two points, the sum of which would be the total length. This divided by the total time would give the length to traverse in a unit of time. So still brute force, but perhaps a more efficient way to do it, especially if used in a list comprehension together with a sum function. As mark s. notes, if you can increase the sampling resolution, the approximation would improve.

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