mathgraphics2dcubic-bezier

How do i verify the gradient at midpoint coordinate which i calculate by using cubic bezire curve equation


A cubic curve is defined by point (1,1);(2,3);(4,4) and (6,1) cal the parametric mid point of the curve and verify that its gradient dy/dx is 1/7 at this point

I successfully calculate the mid point by using the parametric value as 0.5 in my cubic parametric equation i finally got the mid point value as (3.1,2.8) How do i verify the gradient dy/dx at that midpoint which 1/7


Solution

  • any cubic curve is defined as:

    p(t) = a0 + a1*t + a2*t^2 + a3*t^3;
    

    where a0,a1,a2,a3 are coefficients (vectors) computed from control points p0,p1,p2,p3 (vectors) and t is (scalar) parameter on interval <0.0,1.0> for cubic Bezier its:

    a0=                           (    p0);
    a1=                  (3.0*p1)-(3.0*p0);
    a2=         (3.0*p2)-(6.0*p1)+(3.0*p0);
    a3=(    p3)-(3.0*p2)+(3.0*p1)-(    p0);
    

    Now the gradient of p(t) is equal to 1st derivation p'(t) by parameter t so:

    p(t) = a0 + a1*t +     a2*t^2 +     a3*t^3;
    p'(t) =     a1   + 2.0*a2*t   + 3.0*a3*t^2;
    

    so simply given any parameter t in 2D your gradient (slope) would be:

    gradient(t) = p'(t).y / p'(t).x
    
                  a1.y + 2.0*a2.y*t + 3.0*a3.y*t^2
    gradient(t) = --------------------------------
                  a1.x + 2.0*a2.x*t + 3.0*a3.x*t^2
    

    In case you have access to p(t) and do not want to compute a0,a1,a2,a3 You can do numericall derivation of p(t) like this:

    P'(t) = ~ ( P(t+epsilon) - P(t-epsilon) )/epsilon
    

    where epsilon is some small value (like 0.001 beware too big value will lower precision and too small will lead to zero result) So:

                   ( p(t+epsilon).y - p(t-epsilon).y )/epsilon
    gradient(t) = ---------------------------------------------
                   ( p(t+epsilon).x - p(t-epsilon).x )/epsilon
    
                   p(t+epsilon).y - p(t-epsilon).y
    gradient(t) = --------------------------------
                   p(t+epsilon).x - p(t-epsilon).x
    

    Now using both methods with: t=0.5, epsilon=0.001, p0(1,1), p1(2,3), p2(4,4), p3(6,1) lead to:

                      1/7 = 0.142857142857143 // reference
    gradient(0.5        ) = 0.1418971         // using algebraic derivation
    gradient(0.5+/-0.001) = 0.1428701         // using numerical derivation
    

    Note that algebraic approach is safe (an should be more precise) and numeric depends on correct setting of epsilon value...