Quadratic Bézier Curve: Calculate Points

后端 未结 5 1578
别那么骄傲
别那么骄傲 2020-11-28 01:57

I\'d like to calculate a point on a quadratic curve. To use it with the canvas element of HTML5.

When I use the quadraticCurveTo() function in JavaScript, I have a s

5条回答
  •  甜味超标
    2020-11-28 02:35

    I created this demo :

    // x = a * (1-t)³ + b * 3 * (1-t)²t + c * 3 * (1-t)t² + d * t³
    //------------------------------------------------------------
    // x = a - 3at + 3at² - at³ 
    //       + 3bt - 6bt² + 3bt³
    //             + 3ct² - 3ct³
    //                    + dt³
    //--------------------------------
    // x = - at³  + 3bt³ - 3ct³ + dt³
    //     + 3at² - 6bt² + 3ct²
    //     - 3at + 3bt
    //     + a
    //--------------------------------
    // 0 = t³ (-a+3b-3c+d) +  => A
    //     t² (3a-6b+3c)   +  => B
    //     t  (-3a+3b)     +  => c
    //     a - x              => D
    //--------------------------------
    
    var A = d - 3*c + 3*b - a,
        B = 3*c - 6*b + 3*a,
        C = 3*b - 3*a,
        D = a-x;
    
    // So we need to solve At³ + Bt² + Ct + D = 0 
    

    Full example here

    may help someone.

提交回复
热议问题