I\'ve found about a 1000 answers to this question, but none of them I can use, because I\'m using 4 control points with my curves.
That said, I stumbled on this guy
A 2-dimensional object, or a Point2D, is simply a Vector, and Vector Arithmetic is well-defined in mathematics. For example:
k*(x,y) = (k*x, k*y)
-(x,y) = (-1)*(x,y)
(x1,y1) + (x2,y2) = (x1+x2, y1+y2)
Those are all the formulas you need to calculate k1
, k2
, k3
, and k4
Here’s how to traverse your cubic bezier curve at uniform speed
There isn't one simple formula for getting even length segments along a cubic bezier curve (meaning even arc-length segments). What's involved is calculating many points along the curve and then using interpolation to “nudge” each point into being roughly equidistant.
I can get you nearly there without your having to get a PH.D in Mathematics.
Start by using the common formula to calculate x/y points on the curve from t=0 to t=1 where t=0 represents the startpoint of the curve and t=1 represents the endpoint of the curve. This is the common formula:
// calc the x/y point at t interval
// t=0 at startPt, t=1 at endPt
var x=CubicN(t,startPt.x,controlPt1.x,controlPt2.x,endPt.x);
var y=CubicN(t,startPt.y,controlPt1.y,controlPt2.y,endPt.y);
// cubic helper formula at t interval
function CubicN(t, a,b,c,d) {
var t2 = t * t;
var t3 = t2 * t;
return a + (-a * 3 + t * (3 * a - a * t)) * t
+ (3 * b + t * (-6 * b + b * 3 * t)) * t
+ (c * 3 - c * 3 * t) * t2
+ d * t3;
}
If you calculate enough intervals, say 100 intervals (t += .01 each loop) then you will get a very good approximation of the curve.
That means if you connect the 100 points with lines, the result would look very much like a cubic bezier curve.
But you're not done!
The series of x/y points calculated above are not uniform in arc-distance from each other.
Some neighboring points are close together and some neighboring points are farther apart.
To calculate evenly distributed points:
Result: you can use these equidistant points to traverse your curve.
Additional Refinement: This should result in a visually smooth movement along your Bezier path. But if you desire even more smoothness, just calculate more than 100 points--more points == more smoothness.