How to calculate the cubic Bézier spline points?

2.2k Views Asked by At

I want to draw a Bézier curve.I have four points are p0,p1,p2,p3. Draw the curve from P0 to P1, it is the start and end point of the curve. How to calculate the intermediate point to draw curve? I need to find the point as mentioned in the below image.

Spline

I get the Bézier curve derivation from https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Cubic_B.C3.A9zier_curves this article. I can find the new point from this derivation. I don't know how to use the point to draw curve? I need four points to draw a curve P0,p00,p01,P1, I have P0 and P1. How can I find the p00 and p01 point using from this calculation. Please suggest me?

I tried this code

double px0 = 0, px1 = 0, px2 = 0, px3 = 0, py0 = 0, py1 = 0, py2 = 0, py3 = 0, px4 = 0, py4 = 0;

px0 = xValues[i - 1]; 
px1 = xValues[i]; 
px2 = xValues[i + 1]; 
px3 = xValues[i + 2]; 
px4 = xValues[i + 3];

py0 = yValues[i - 1]; 
py1 = yValues[i]; 
py2 = yValues[i + 1]; 
py3 = yValues[i + 2]; 
py4 = yValues[i + 3];

PointX.Add(xValues[i]+ (Math.Pow((1 - ten), 3) * px0
            + 3 * ten * Math.Pow((1 - ten), 2) * px1 
            + 3 * ten * ten * (1 - ten) * px2 
            + ten * ten * ten * px3)/2);
PointY.Add(yValues[i]+ (Math.Pow((1 - ten), 3) * py0
            + 3 * ten * Math.Pow((1 - ten), 2) * py1
            + 3 * ten * ten * (1 - ten) * py2 
            + ten * ten * ten * py3)/2);

Now I can get the new point(pointX and PointY). How to draw the curve using this points?

2

There are 2 best solutions below

0
On

That's not the way one normally uses Bézier curves, in fact it's quite odd way to use them.

The reason is that (cubic) Bézier curves are cubic curves to be constructed from knowing $p(0)$, $p'(0)$, $p'(1)$ and $p(1)$. The intermediate points only plays the role of indicating $p'(0)$ and $p'(1)$, it's not that the curve normally would pass through these intermediate points.

Note that the Bézier curve is basically just a cubic polynomial fulfilling the boundary conditions - and it's the only one that does it.

If you want a cubic polynomial that passes through select points you can do that as well, but the Bézier form is not very practical in doing so. Instead one would for example use Lagrange polynomials for that. We can for example construct a polynomial $p(t)=at^3+bt^2+ct+d$ such that $p(0)=p_0$, $p(1/3)=p_1$, $p(2/3)=p_2$ and $p(1)=p_3$.

Now if you must have it in the Bézier form you can expand the Bézier form and then matching the parameters so the polynomials match. You have:

$$p(t) = (1-t)^3q_0 + 3(1-t)^2tq_1 + 3(1-t)t^2q_2 + t^3q_3 \\ = (1-3t+3t^2-t^3)q_0 + (3t-6t^2+3t^3)q_1 + (3t^2-3t^3)q_2 + t^3q_3\\ = q_0 + (3q_1-3q_0)t + (3q_0-6q_1+3q_2)t^2 + (q_3-3q_2+3q_1-1)t^3$$

Now it's just a matter of solving for $q_j$. First we have that $q_0 = c$, then one inserts that in $3q_1-3q_0 = b$ and solve for $q_1$ and so on.

0
On

It's not at all clear what you're asking, so this is just a guess.

What I think is ... you have four points that the Bezier curve passes through, and you want to find its control points.

So, let $\mathbf{C}$ denote the curve, let $\mathbf{Q}_0$, $\mathbf{Q}_1$, $\mathbf{Q}_2$, $\mathbf{Q}_3$ be the known points that it passes through, and let $\mathbf{P}_0$, $\mathbf{P}_1$, $\mathbf{P}_2$, $\mathbf{P}_3$ be its control points, which we want to find.

Obviously $\mathbf{P}_0 = \mathbf{Q}_0 = \mathbf{C}(0)$ and $\mathbf{P}_3 = \mathbf{Q}_3 = \mathbf{C}(1)$.

I'm going to arrange things so that $\mathbf{C}\big(\tfrac13\big) = \mathbf{Q}_1$ and $\mathbf{C}\big(\tfrac23\big) = \mathbf{Q}_2$. The $\tfrac13$ and $\tfrac23$ are arbitrary choices, just for simplicity. After solving a couple of linear equations, you get \begin{align*} \mathbf{P}_{1} & = \tfrac16\big( -5\mathbf{Q}_0 + 18\mathbf{Q}_1 - 9 \mathbf{Q}_2 + 2 \mathbf{Q}_3 \big) \\ \mathbf{P}_{2} & = \tfrac16\big( \;\;\;\;2\mathbf{Q}_0 - 9 \mathbf{Q}_1 + 18\mathbf{Q}_2 - 5 \mathbf{Q}_3 \big) \end{align*}