Calculate Cubic Bezier Curve passing through 6 points

1k Views Asked by At

I know this question has been asked before, but the answers I have come across I need a little more help understanding how to implement. I came across this answer here, but the cited link does not appear to be valid anymore. Also searching elsewhere says it is not possible, or is very hard to find how to do this, or is so heavily filled with mathematical lingo, so please help!

https://math.stackexchange.com/q/302145

I have a cubic Bezier curve that passes through 6 points. The knowns are

t0 = 0, t5 = 1, P0 = xy0, P3 = xy5, xy0-xy5

Unknowns are

t1, t2, t3, t4, P1, P2

I know how to create the curve by merging two bezier curves each with 4 control points, but I only want one curve with 4 control points and I know it exists.

I have 8 unknown equations for t values and 2 control points. Can I solve this using matrices and how would I set it up?

C(t0) = po(1-t0)³ + p1(3*t0(1-t0)²) + p2(3*t0²(1-t0)) + p3(t0³) = x0, P0
C(t1) = po(1-t1)³ + p1(3*t1(1-t1)²) + p2(3*t1²(1-t1)) + p3(t1³) = x1
C(t2) = po(1-t2)³ + p1(3*t2(1-t2)²) + p2(3*t2²(1-t2)) + p3(t2³) = x2
C(t3) = po(1-t3)³ + p1(3*t3(1-t3)²) + p2(3*t3²(1-t3)) + p3(t3³) = x3
C(t4) = po(1-t4)³ + p1(3*t4(1-t4)²) + p2(3*t4²(1-t4)) + p3(t4³) = x4
C(t5) = po(1-t5)³ + p1(3*t5(1-t5)²) + p2(3*t5²(1-t5)) + p3(t5³) = x5, P3

I am implementing in code, so can use a solver, but need to know the beginning steps and logic and how I would go about solving in the right way.

Update 11/15

So I thought I would post my working code. I actually had 7 points, not 6. Its not the prettiest, as Im on time constraints, but it works. Thx Bubba. and the link is working for me now.

from scipy.optimize import fsolve

#define x0-x6 and y0-y6

def solveEquations(params):
    t1,t2,t3,t4,t5,xp1,yp1,xp2, yp2  = params
    t0 = 0
    t6 = 1

    return (   x0*(1-t1)**3 + xp1*(3*t1*(1-t1)**2) + xp2*(3*t1**2*(1-t1)) + x6*(t1**3) - x1,
               x0*(1-t2)**3 + xp1*(3*t2*(1-t2)**2) + xp2*(3*t2**2*(1-t2)) + x6*(t2**3) - x2,
               x0*(1-t3)**3 + xp1*(3*t3*(1-t3)**2) + xp2*(3*t3**2*(1-t3)) + x6*(t3**3) - x3,
               x0*(1-t4)**3 + xp1*(3*t4*(1-t4)**2) + xp2*(3*t4**2*(1-t4)) + x6*(t4**3) - x4,
               x0*(1-t5)**3 + xp1*(3*t5*(1-t5)**2) + xp2*(3*t5**2*(1-t5)) + x6*(t5**3) - x5,

               y0*(1-t1)**3 + yp1*(3*t1*(1-t1)**2) + yp2*(3*t1**2*(1-t1)) + y6*(t1**3) - y1,
               y0*(1-t2)**3 + yp1*(3*t2*(1-t2)**2) + yp2*(3*t2**2*(1-t2)) + y6*(t2**3) - y2,
               y0*(1-t3)**3 + yp1*(3*t3*(1-t3)**2) + yp2*(3*t3**2*(1-t3)) + y6*(t3**3) - y3,
               y0*(1-t4)**3 + yp1*(3*t4*(1-t4)**2) + yp2*(3*t4**2*(1-t4)) + y6*(t4**3) - y4,

        )
t1,t2,t3,t4,t5,xp1,yp1,xp2, yp2 = fsolve(solveEquations, (.2,.4,.6,.7,.9,9,5,5,-8))

print xp1, yp1, xp2, yp2
1

There are 1 best solutions below

1
On BEST ANSWER

It seems to me that your list of "knowns" might have typos.

Suppose we use $Q_0, \ldots, Q_5$ to denote the 6 points that the cubic curve must pass through. We need to find 4 control points $P_0, P_1, P_2, P_3$ and 6 parameter values $t_0, \ldots, t_5$ such that $C(t_i) = Q_i$ for $i=0,1,\ldots,5$.

We can assume that $t_0 =0$ and $t_5 = 1$. Then we know immediately that $P_0 = Q_0$ and $P_3 = Q_5$. So the remaining unknowns are $P_1$, $P_2$ and $t_1,t_2,t_3,t_4$. Is that correct? If so, that's $8$ unknowns, and we can calculate these (in principle) from the $8$ equations $C(t_i) = Q_i$ for $i=1,2,3,4$.

The answer you cited gives you two brute-force methods of solving this problem, which should be fairly easy to implement in code, assuming that you have good numerical methods already available. However, note that you need to solve non-linear equations or do non-linear optimization, so you need more than just a linear system solver. You ask "can I solve this with matrices". If I understand your question correctly, the answer is "no".

The smarter way to construct the curve is by using the approach given in the Kozak/Krajnc paper, but that requires more mathematical background than the brute-force methods.