- Home /
How can I stretch an array of points, interpolating the values.
I've got another interpolation question that I can't seem to wrap my head around. It seems simple, but still, I can't figure it out.
I have a simple array of points that never changes. Let's pretend it looks like this:
var array_base = [1.0, 3.0, 6.0, 9.0, 12.0]
And I want to stretch it by some value that is always greater than the length of the original array. Let's say I want to stretch it to 10 points. I'm looking for an interpolated response that looks something like this:
var array_stretched = [1.0, 2.0, 3.0, 4.0, 6.0, 7.0, 9.0, 10.0, 11.0, 12.0]
That's obviously an oversimplified example. The function should work with any length that is greater than the original array. My math starts to break down when I think about how to calculate something like, say, 6 points, where the original array is 5 points long:
var array_stretched = [1.0, 2.7, 4.1, 5.9, 7.1, 12.0]
That's obviously incorrect, but hopefully it illustrates the idea. My example uses a simple array with increasing numbers, but this function should work on any array of numbers, smoothly interpolating the values for the new points.
Any ideas?
Answer by Graham-Dunnett · Feb 06, 2013 at 09:50 PM
Sounds like you want an interpolating cubic spline. Welcome to your new best friend, the Catmull-Rom spline. Check out http://www.mvps.org/directx/articles/catmull/ for a good intro, or for a Unity example http://www.ernestorojo.com/projects/catmull-rom-unity-demo/.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092252im_/https://answers.unity.com/themes/thub/images/avi.jpg)