Catmull-Rom interpolation in Ruby

493 Views Asked by At

I have a list of points that I want to draw a smooth line between. I am using the RVG library for drawing so if i could get a SVG string from my points I would be happy. Searched around and found that Catmull-Rom probably is the algorithm to use.

Found some implementations in the Kamelopard and Rubyvis libraries, but couldn't understand how to use them from my list of points.

So, the question is, how can I take my array of (x,y) points and get a Catmull-Rom interpolated SVG curve from them?

1

There are 1 best solutions below

0
On

Catmull-Rom is probably a good place to start. I recently re-implemented the Kamelopard version, and found this helpful: http://www.cs.cmu.edu/~462/projects/assn2/assn2/catmullRom.pdf

It's fairly straightforward, provided you understand the matrix multiplication. You'll end up with a matrix equation you'll need to evaluate a bunch of times, once per point on the path you're drawing. If you have control points A, B, C, and D, and you want to draw the curve between B and C, make a matrix where A, B, C, and D are the rows, and plug it into the equation at the top of the paper I linked to. It will be the last matrix in the list. The other values you'll need to know are "u", which ranges from 0 to 1, and "T", the "tension" of the spline. You'll evaluate the equation multiple times, incrementing u across its domain each time. You can set the tension to whatever you want, between 0 and 1, and it will affect how sharply the spline curves. 0.5 is a common value.

If you're trying to evaluate the curve between, for instance, the first two control points on your list, or the last two, you'll find you have problems making your matrix, because you need the two control points on either side of the point you're evaluating. In these cases, just duplicate the first or last control point, as necessary.