The goal is to fit a sequence of cubic Bezier curve over a sequence of ordered points. We use a classic Bezier fitting technique developed by Philip Schneider. A Bezier curve Q of order n is defined by its control points viโ: Q(u)=โi=0nโviโBinโ(u), where Binโ(u) are the Bernstein polynomials of degree n, Binโ(u)=(inโ)ui(1โu)nโi. In our case, we choose n=3, i.e. the cubic Bezier curves with four control points.
We need to minimize the distance from a Bezier curve to a set S of n points. Formally, we define an objective function F=โj=1nโโฃโฃpiโโQ(uiโ)โฃโฃ2, where uiโ is the parameter of the closest point on cubic Bezier curve Q to the point piโ.
Let t1โ and t2โ be the tangents at the start and end of the curve. Then write the second and the third control points in terms of the tangents, v1โ=ฮฑ1โt1โ+v0โ and v2โ=ฮฑ2โt2โ+v3โ where ฮฑ1โ and ฮฑ2โ are the distances to the nearest end points in the tangent direction. Therefore, the function F can be minimized by finding the right parameters ฮฑ1โ and ฮฑ2โ. In particular, we set the derivatives of F with respect to ฮฑ1โ and ฮฑ2โ, and set them to zero:
โฮฑ1โโFโ=โi=1nโ2(piโโQ(uiโ))โ
t1โB13โ(uiโ)=0
โฮฑ2โโFโ=โi=1nโ2(piโโQ(uiโ))โ
t2โB23โ(uiโ)=0
This linear system of equations can be solved via matrix inversion. Let Ai,jโ=tjโBj3โ(u1โ). Then,
(โi=1nโAi,12โ)ฮฑ1โ+(โi=1nโAi,1โโ
Ai,2โ)ฮฑ2โ=โi=1nโ(piโโ(v0โB03โ(uiโ)+v0โB13โ(u1โ)+v3โB23โ(uiโ)+v3โB33โ(uiโ)))โ
Ai,1โ
(โi=1nโAi,22โ)ฮฑ2โ+(โi=1nโAi,1โโ
Ai,2โ)ฮฑ1โ=โi=1nโ(piโโ(v0โB03โ(uiโ)+v0โB13โ(u1โ)+v3โB23โ(uiโ)+v3โB33โ(uiโ)))โ
Ai,2โ
Condensing the equations into a matrix,
[c1,1โc2,1โโc1,2โc2,2โโ][ฮฑ1โฮฑ2โโ]=[x1โx2โโ]
Then solving these two equations allow us to obtain a good approximation.
There is still the problem of finding the parameter of the closest point for each point piโ. We can use Newton-Raphson iteration to get a better u: uโuโfโฒ(u)f(u)โ where f=(piโโQ(u))โ
Qโฒ(u) and fโฒ=Qโฒ(u)โ
Qโฒ(u)+(piโโQ(u))โ
Qโฒโฒ(u).