# Convert cubic spline to Bézier curve and get control points

There is a cubic spline represented by the standard equation: $$f(x) = a + b (x - x_0) + c (x - x_0)^2 + d (x - x_0)^3$$ and 2 endpoints:

• $$P_0~ [x, y]$$ - starting point
• $$P_1~ [x, y]$$ - end point

Is it possible to convert it to a cubic Bézier curve and get all control points (CV0 and CV1).

What I was thinking of is to build a system of 2 parametric equations of the Bezier curve and 2 points $$P_i(t) = (1 - t_i)^3 \cdot P_0 + 3t(1 - t_i)^2 \cdot \text{CV}_0 + 3t^2(1 - t_i) \cdot \text{CV}_1 + t_i^3 \cdot P_1$$ I can calculate Pi[x, y] that belongs to a cubic spline. But how to get the t_i value for the current point? Or there is different approach?

\begin{align} f(x) &= a + b (x - x_0) + c (x - x_0)^2 + d (x - x_0)^3 \tag{1}\label{1} . \end{align}

Note that \eqref{1} defines $$y$$ as a function of $$x$$. Combined with two values of $$x$$, $$x_0$$ and $$x_3$$, we have all we need to make a conversion to equivalent 2D cubic Bezier segment, defined by its four control points

\begin{align} P_0(x_0,y_0),\,&P_1(x_1,y_1),\,P_2(x_2,y_2),\,P_3(x_3,y_3) \tag{2}\label{2} . \end{align} The end points are

\begin{align} P_0&=(x_0,y_0)=(x_0,f(x_0))=(x_0,a) \tag{3}\label{3} ,\\ P_3&=(x_3,y_3)=(x_3,f(x_3)) \tag{4}\label{4} . \end{align}

2D cubic Bezier segment is defined as usual,

\begin{align} B_3(t)&=(x(t),y(t)) \tag{5}\label{5} ,\\ x(t)&=x_0\,(1-t)^3+3\,x_1\,(1-t)^2\,t+3\,x_2\,(1-t)\,t^2+x_3\,t^3 \tag{6}\label{6} ,\\ y(t)&=y_0\,(1-t)^3+3\,y_1\,(1-t)^2\,t+3\,y_2\,(1-t)\,t^2+y_3\,t^3 ,\quad t\in[0,1] \tag{7}\label{7} . \end{align}

We know that $$x$$ is linear in $$t$$, so we must have \begin{align} x_0\,(1-t)+x_3\,t &=x_0\,(1-t)^3+3\,x_1\,(1-t)^2\,t+3\,x_2\,(1-t)\,t^2+x_3\,t^3 \tag{8}\label{8} ,\\ (x_3-x_0)t+x_0 &=(3x_1-x_0-3x_2+x_3)t^3+(3x_0+3x_2-6x_1)t^2+3(x_1-x_0)t+x_0 \quad \forall t\in[0,1] \tag{9}\label{9} , \end{align}

hence we have $$x_1,x_2$$ evenly distributed between the endpoints $$x_0$$ and $$x_3$$:

\begin{align} x_1 &= \tfrac13 (2x_0+x_3)=x(t)\Big|_{t=1/3} \tag{10}\label{10} ,\\ x_2 &= \tfrac13 (x_0+2x_3)=x(t)\Big|_{t=2/3} \tag{11}\label{11} . \end{align}

Corresponding $$y$$ points on the curve \eqref{1} are

\begin{align} y(\tfrac13)&=f(x_1) \tag{12}\label{12} ,\\ y(\tfrac23)&=f(x_2) \tag{13}\label{13} . \end{align}

The last pair of equations is a linear system with two unknowns, $$y_1$$ and $$y_2$$ which can be trivially solved as

\begin{align} y_1 &= \tfrac13\,b\,(x_3-x_0)+a \tag{14}\label{14} ,\\ y_2 &= \tfrac13(x_3-x_0)(c(x_3-x_0)+2b)+a \tag{15}\label{15} . \end{align}

Example

\begin{align} a &= 7 ,\quad b = 2 ,\quad c = 2 ,\quad d = -1 \tag{16}\label{16} ,\\ x_0 &= -1 ,\quad x_3 = 3 \tag{17}\label{17} ,\\ y_0&=a=7 ,\quad y_3=f(3)=-17 \tag{18}\label{18} ,\\ x_1 &= \tfrac13(2x_0+x_3)=\tfrac13 \tag{19}\label{19} ,\\ x_2 &= \tfrac13(x_0+2x_3)=\tfrac53 \tag{20}\label{20} ,\\ y_1 &= \tfrac13\,b\,(x_3-x_0)+a =\tfrac{29}3 \tag{21}\label{21} ,\\ y_2 &= \tfrac13(x_3-x_0)(c(x_3-x_0)+2b)+a =23 \tag{22}\label{22} . \end{align}

• I am extremely grateful for your time on such a detailed answer. I have seen the use of 1/3 (2/3) in other sources, but did not understand where it comes from and why. Thank you @g.kov ! Jul 27 '20 at 16:25
• @newWeb: My pleasure, and welcome to Math.SE. If you've found the answer useful, you can mark it as "accepted". Jul 27 '20 at 16:56
• @newWeb: Also, in this answer you could find some more info on the representation of linear segments in the form of higher order Bezier curves. Jul 27 '20 at 17:09
• Should't y1 = a = 7 be y0 = a = 7? Nov 24 '20 at 0:46
• @ SSteve:Yes, thanks, typo fixed. Nov 24 '20 at 6:11

Let us assume a curve defined on $$t\in[0,1]$$

$$x(t)=a+bt+ct^2+dt^3$$

with the boundary conditions

$$x_0=a,\\x'_0=b,\\x_1=a+b+c+d,\\x'_1=b+2c+3d.$$

From this we draw

$$a=x_0,\\b=x'_0,\\c=3(x_1-x_0)-x'_1-2x'_0,\\d=-2(x_1-x_0)+x'_1+x'_0.$$

Now the Bezier polynomial is

$$w_0(1-t)^3+3w_1(1-t)^2t+3w_2(1-t)t^2+w_3t^3= \\w_0+3(w_1-w_0)t+3(w_2-2w_1+w_0)t^2+(w_3-3w_2+3w_1-w_0)t^3.$$

We can identify to the standard form and solve the triangular system. This gives

$$w_0=a=x_0,\\w_1=\frac b3+a=\frac{x'_0}3+x_0,\\w_2=\frac c3+\frac{2b}3+a=-\frac{x'_1}3+x_1,\\w_3=d+c+b+a=x_1.$$