Curve fitting is explained in this question: How can I fit a Bézier curve to a set of data?
However, there is a must easier method to interpolate the points. It goes this way:
- Refine: Place a point in the middle of each edge.
- Dual: Place a point in the middle of each edge and remove the old points.
- Repeat the Dual step several times.
- Repeat from step one until the curve is smooth enough.
You can easily see that it works on paper. The resulting curve begins to approximate a series of bezier splines.
It is described more formally in this PDF file (Section 3): http://www.cc.gatech.edu/~jarek/courses/handouts/curves.pdf
Here is some Javascript code to do it:
function bspline_smooth(points, order) {
// insert a point in the middle of each edge.
function _refine(points) {
var i, index, len, point, refined;
points = [points[0]].concat(points).concat(points[points.length-1]);
refined = [];
index = 0;
for (i = 0, len = points.length; i < len; i++) {
point = points[i];
refined[index * 2] = point;
if (points[index + 1]) {
refined[index * 2 + 1] = _mid(point, points[index + 1]);
}
index += 1;
}
return refined;
}
// insert point in the middle of each edge and remove the old points.
function _dual(points) {
var dualed, i, index, len, point;
dualed = [];
index = 0;
for (i = 0, len = points.length; i < len; i++) {
point = points[i];
if (points[index + 1]) {
dualed[index] = _mid(point, points[index + 1]);
}
index += 1;
}
return dualed;
}
function _mid(a, b) {
return new Point(
a.x + ((b.x - a.x) / 2),
a.y + ((b.y - a.y) / 2) );
}
if (!order) {
return points;
}
return bspline_smooth(_dual(_dual(_refine(points))), order - 1);
}