iosuibezierpathbeziercamediatiming

Calculating polynomial coefficients for unitized cubic bezier curve


I'm analyzing this piece of code by Apple:

http://opensource.apple.com/source/WebCore/WebCore-955.66/platform/graphics/UnitBezier.h

The part I specifically don't understand is this segment, where they calculate the polynomial coefficients for the curve (they assume P0 and P1 are (0,0) and (1,1), respectively):

UnitBezier(double p1x, double p1y, double p2x, double p2y)
        {
            // Calculate the polynomial coefficients, implicit first and last control points are (0,0) and (1,1).
            cx = 3.0 * p1x;
            bx = 3.0 * (p2x - p1x) - cx;
            ax = 1.0 - cx -bx;

            cy = 3.0 * p1y;
            by = 3.0 * (p2y - p1y) - cy;
            ay = 1.0 - cy - by;
        }

I assume they are using the cubic bezier function:

enter image description here

Can someone walk through the steps they are using to simplify this equation to get these coefficients? I'm assuming that since P0 is (0,0) that the first part can be removed, and since P3 is (1,1) the last part becomes t^3, so that leaves:

3 * (1-t)^2 * t * P1 + 3 * (1-t) * t^2 * P2 + t^3

How are they simplifying that to calculate the polynomial coefficients ax, bx and cx? Or am I way off base here?


Solution

  • After a lot of futzing around, I figured out what's going on.

    Multiplying out that polynomial above will simplify to:

    3(p1x)t -6(p1x)t^2 +3(p1x)t^3 +3(p2x)t^2 -3(p2x)t^3 +t^3
    

    We can group this based on the powers of 't':

    (3(p1x) - 3(p2x) + 1) * t^3
    +
    (3(p2x) - 6(p1x)) * t^2
    +
    (3(p1x)) * t
    

    So basically,

    cx = 3*p1x (this one is easy)
    
    bx = (3(p2x) - 6(p1x))
       = (3(p2x) - 3(p1x) - 3(p1x)
       = 3(p2x - p1x) - cx
    
    ax = 3(p1x) - 3(p2x) + 1
       = 1 - 3(p1x) - 3(p2x) + 6(p1x)
       = 1 - 3(p1x) - 3(p2x) + 3(p1x) + 3(p1x)
       = 1 - 3(p1x) - (3(p2x) - 3(p1x) - 3(p1x))
       = 1 - 3(p1x) - (3(p2x - p1x) - 3(p1x))
       = 1 - cx - (3(p2x - p1x) - cx)
       = 1 - cx - bx