0
$\begingroup$

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?

$\endgroup$
0
$\begingroup$

\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

enter image description here

\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_1&=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}

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ 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 ! $\endgroup$ – newWeb Jul 27 at 16:25
  • $\begingroup$ @newWeb: My pleasure, and welcome to Math.SE. If you've found the answer useful, you can mark it as "accepted". $\endgroup$ – g.kov Jul 27 at 16:56
  • $\begingroup$ @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. $\endgroup$ – g.kov Jul 27 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.