How to approximate/connect two continuous cubic Bézier curves with/to a single one?
Solution 1:
You probably already know this, but let me explicitly point out:
== exact match ==
It is not possible to construct a single cubic Bezier curve that exactly matches your new curve.
A single cubic Bezier curve has a constant "curvature" A along its length. When you shrink it, the new curve has "sharper" curvature B (but still constant along the length of the new curve).
It is not possible to construct a single cubic Bezier curve -- since, like all single cubic curves, it has constant curvature C along its length -- such that C=A along the part of the length, and C=B along the rest of the length, where A<>B.
(I'm mis-using the term "curvature" here for something that is more precisely the 3rd derivative of a curve, sometimes called the "jerk").
== approximation ==
Perhaps the simplest approximation is: Use the initial starting point and first control knot -- p0 and p1, and the final control knot and final ending point -- r'2 and r'3. Use those 4 points as the start, control knots, and endpoint of a Bezier curve that approximates the one you want: a0, a1, a2, a3.
This approximation exactly hits the endpoints of your desired curve, and has the same initial and final slope at those endpoints, but it slightly diverges from your desired curve in the middle. In particular, it probably doesn't go exactly through the cutpoint K.
There are many other possible approximations you could make. The "Don Lancaster's Guru's Lair Cubic Spline Library" may have the details I'm leaving out:
- It is possible to nudge a1 along the line p0-p1, or to nudge a2 along the line r'2-r'3 -- or both -- such that the approximation curve not only starts and ends at the same points and slopes, but also passes through the cutpoint K.
- pick any 4 points along your new curve (perhaps the endpoints, the cutpoint K, and some other point) and generate a cubic Bezier curve that goes exactly through all 4 points.
- pick many points along your new curve -- bunched together in places where curve matching is important, perhaps near the endpoints and point K, and spaced further apart where curve matching is not so important -- and generate a cubic Bezier curve that is a least-squares best fit to those points.
Solution 2:
As others have pointed out, an exact solution with a single Bezier cubic is not possible.
If you're going to approximate, there are two obvious choices for the approximant:
(1) A single Bezier cubic. I wouldn't expect this to work very well. The approximation error will be larger if the deformation of the sub-curve is significant. In fact, the maximum error will be the maximum of the distances $r_1r_1'$, $r_2r_2'$, $p_3r_3'$.
(2) A cubic spline (i.e. a sequence of Bezier cubics). But, you don't need an approximation process to produce this spline -- you already have a sequence of Bezier cubics, namely the two you got from the de Casteljau subdivision. You can join these two cubics together into a single $C_1$ spline, if you want, but I don't see any great benefit from doing that.
Solution 3:
You can calculate the distance of q2 k and k r′1 as LeftLength and RightLength. We can image we have got a cubic curve, which we subdivide at k, but what is the t value? We can calculate t from de Casteljau’s algorithm, that is t = leftLength / rightLength We also know that the new curve's p1' is along p0->p1 direction, and p2' is along p3->p2, so we can use t to calculate the new curve's p1' and p2'.