Need a method to generate cubic spline control points


My name is Rich P. and I am looking for some help from a
person who is both a Ruby programmer and is mathematically
literate. Or at least more so than I am.

Here is my problem. I wish to generate open and closed cubic
splines that pass through an array of “knot” points such as:

[[x1, y1], [x2, y2], [x3, y3], [x4, y4]]

But cubic splines require control points as well as “knot”
points. I need to generate the control points that will
result in a smooth curve.

I found an article that explains how to do this and even
provides example code. But the code is written in C#
[a language that I do not know]. And I think it draws
lines whereas I want a method which would input the array
above and output a string which could then be input into a
rmagick path command. For example, taking the above array to
be an open curve and using cxn and cyn to be the generated
control points it should produce the following string:

“Mx1,y1 Ccx1,cy1 cx2,cy2 x2,y2 Scx3,cy3 x3,y3 Scx4,cy4 x4,y4”

A similar method would be needed to handle closed curves.

The article with the equations needed for the generation of
these control points can be found at:

Please let me know if you would be interested in helping me
with this problem.

Hi Rich,
So this math is a little over my head, but I can read the ruby and c#
well. If you want to draw a smooth curve through a set of lines, this is
best guess at what you need to do, from looking at that page and the
attached code:

1.) make an array/list of points for your curve

2.) run them through the code that’s on the page. you can read the code
the out parameters this way:

def getCurveControlPoints(knots)
firstControlPoints = []
secondControlPoints = []

# ... body of the method

return [firstControlpoints,secondControlPoints]


Otherwise, translating that c# code is pretty straightforward.

3.) It gets a little fuzzy for me from here, since his code (in
Window1.xaml.cs) uses a class, BezierSegment, which he doesn’t provide
source for. However, it looks like he’s saying that if you draw a line
through firstControlPoints[i], secondControlPoints[i] and knots[i+1],
each i in the the indices of firstControlPoints, you will wind up with a
Bezier curve.

I’m not sure how to mentally parse that rmagick string you provided. It
might be worth doing the above and just plotting the resulting points
to see what you get.

Hope this somehow helps!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs