# How can I resample a list of {x,y} data for evenly spaced points?

I have generated a list of {x,y} points using a position function of a particle moving in 2D space. If you run the following code you'll see a small bit of the particle's motion (I cut out most of it since it is a very large list of points). You can see that each of the points are not evenly spaced from each other. However, I want to interpolate between each point so that I can get a "resample" of points which are all evenly spaced. That way I can get a proper histogram of the particles position on a grid. I guess using ListInterpolation[] isn't working because it is not a continuous function or something!

myList = {{12.633, 0.}, {12.5796, 1.05904}, {12.4203, 2.10566}, {12.1574,
3.12766}, {11.7949, 4.11327}, {11.338, 5.05131}, {10.7935,
5.93142}, {10.1693, 6.74423}, {9.47453, 7.48147}, {8.71909,
8.13617}, {7.91375, 8.70271}, {7.06978, 9.17696}, {6.19883,
9.55626}, {5.3127, 9.83955}, {4.42313, 10.0273}, {3.54156,
10.1214}, {2.67902, 10.1253}, {1.84582, 10.0439}, {1.05149,
9.88323}, {0.304511, 9.65046}, {-0.387734, 9.35381}, {-1.01913,
9.00228}, {-1.58492, 8.60551}, {-2.08174, 8.17357}, {-2.50769,
7.71676}, {-2.86234, 7.24541}, {-3.14668, 6.7697}, {-3.36313,
6.2994}, {-3.51543, 5.84373}, {-3.60852, 5.4112}, {-3.64849,
5.00937}, {-3.64236, 4.64479}, {-3.59797, 4.32283}, {-3.52378,
4.04761}, {-3.42869, 3.82189}, {-3.32184, 3.64707}, {-3.21242,
3.52316}, {-3.10945, 3.44876}, {-3.0216, 3.42114}, {-2.957,
3.43629}, {-2.92306, 3.48901}, {-2.92631, 3.57302}, {-2.97223,
3.68114}, {-3.06517, 3.80538}, {-3.20822, 3.93715}, {-3.40314,
4.06746}, {-3.65031, 4.18708}, {-3.9487, 4.28677}, {-4.29591,
4.35747}, {-4.68816, 4.39047}, {-5.12039, 4.37766}, {-5.58633,
4.31165}, {-6.07861, 4.18598}}

ListPlot[myList, AspectRatio -> 1]


From other examples I have seen, interpolation isn't so simple, so any help here would be appreciated!

• Are the sample points equally spaced in time or is the particle moving at a constant velocity? That would seem to matter in constructing a histogram.
– JimB
Commented Jun 9, 2020 at 19:20
• The sample points are equally spaced in time. I want them equally spaced in space. The problem is the particle is certainly not moving at constant velocity.
– user72418
Commented Jun 9, 2020 at 19:51
• Thanks for answering that but my concern still remains: If the sample points are equally spaced in time, doesn't the amount out time spent in a grid cell matter? If so, why not just use the points as is and figure out which grid cell to which each belongs? So an area where a particle hangs out a large amount of time will be counted less than an area that a particle spends very little time. The answers given you what you ask (and very nicely). I just question the construction of the histogram.
– JimB
Commented Jun 9, 2020 at 20:18
• @JimB The end goal is to have a density histogram of the distance covered by the particle. Since coverage does not depend on velocity, and because I didn't know the proper way to weigh each point based on velocity, I thought I could just make the points evenly spaced to eliminate the need to take the velocity at each point into cosideration. Am I off in this thinking?
– user72418
Commented Jun 10, 2020 at 12:40
• My intent was to get you to think that there are (at least) two different entities. In wildlife biology (which admittedly is a bit different from physics) the amount of time a critter spends in an area is what is of main interest (and where most of the time spent is usually labeled as the "home range"). But high concentrations of time do not necessarily translate to "importance" (as time spent at a lake or stream for water might be short but definitely essential). The point is that what you do needs to match with your objective whatever that is.
– JimB
Commented Jun 10, 2020 at 15:20

For linear interpolation, you can interpolate the x and y components separately. Each component becomes a one-dimensional function. The function value is e.g. the x component; the function parameter is the arc length. Since the parameter is the arc length, that makes it easy to sample from it uniformly. In my code below I make it even easier by rescaling the arc length so it runs from 0 to 1. All we have to do to sample the curve uniformly, then, is to sample the interpolation functions uniformly from 0 to 1.

arcLength = Rescale@Prepend[0]@Accumulate[Norm /@ Differences[myList]];
{x, y} = Transpose[myList];
xinterp = Interpolation[Transpose[{arcLength, x}], InterpolationOrder -> 1];
yinterp = Interpolation[Transpose[{arcLength, y}], InterpolationOrder -> 1];
pts = Table[Through[{xinterp, yinterp}[t]], {t, 0, 1, 0.02}];

ListLinePlot[
myList,
Epilog -> {
Red,
PointSize[Medium],
Point[pts]
}]


Few more alternatives:

### 1. GraphUtilitiesLineScaledCoordinate

You can use the built-in function GraphUtilitiesLineScaledCoordinate:

Needs["GraphUtilities"]

n = 50;

lscpts = LineScaledCoordinate[myList, N@#] & /@ Subdivide[n];

ListLinePlot[myList, Epilog -> {Red, Point@lscpts}]


Compare with pts from C.E.'s answer:

Total @ Chop[Abs[pts - lscpts]]

 {0, 0}


### 2. MeshFunctions -> {ArcLength}

llp = ListLinePlot[myList, MeshFunctions -> {ArcLength}, Mesh -> (n-1),  MeshStyle -> Red]


Extract the coordinates of mesh points and add the first and last element of myList:

mfpts = Join[{First @ myList},
Cases[Normal[llp], Point[x_] :> x, All],
{Last @ myList}];


Compare with lscpts:

Total @ Chop[Abs[lscpts - mfpts]]

{0, 0}


### 3. Illustrating JimB's comment:

" just use the points as is and figure out which grid cell to which each belongs":

DensityHistogram[myList, {{1}, {1}}, Mesh -> All,
MeshStyle -> Directive[Thin, Gray],
ChartLegends -> BarLegend[Automatic, LegendMarkerSize -> {30, 350}],
Epilog -> {Red, PointSize[Medium], Point @ myList},
AspectRatio -> Automatic, ImageSize -> Large]
`