Quote Originally Posted by Dickee
This is a long post, but I thought it would be wise to take the logic through the powers to explain the syntax more clearly.
Firstly, I'll post an example by ahab that will show you expansion of 2 binomials using the FOIL method from this thread:

Bezier curves - http://board.flashkit.com/board/show...threadid=58629

/////////////////////////////////

-- To get the Bernstein basis functions, 1 can be rewritten like this:

1 = t + [1 - t]

-- use this substitution to square 1, resulting in a quadratic equation:

1^2 = (t + [1 - t])(t + [1 - t])

-- FOIL out the 2 binomials -- (First, Outside, Inside, Last):

1 = t^2 + t(1 - t) + t(1 - t) + (1 - t)^2

-- combine like terms --

1 = t^2 + 2t(1 - t) + (1 - t)^2

/////////////////////////////////

Now, as the next link will advise, the FOIL method is a particular example of the Distributive Property for the expansion of polynomials:

http://mathforum.org/library/drmath/view/57480.html

/////////////////////////////////

Now we'll use the Distributive Property to expand the cube of 1, resulting in a cubic equation:

1^3 = (t + [1 - t])(t + [1 - t])(t + [1 - t])

First, expand the product of the first 2 factors, resulting in the same quadratic equation as in ahab's example above:

(t + [1 - t])(t + [1 - t]) = t^2 + t*[1-t] + [1-t]*t + [1-t]^2

Below is the same right operand as above, split into our basis functions, each found between ||-||:

= ~B0~||(t^2)|| + ~B1~||( t*[1-t] + [1-t]*t)|| + ~B2~||([1-t]^2)||

Then combine like terms and list:

B0(t) = t^2
B1(t) = 2*t*[1-t]
B2(t) = [1-t]^2

Now replace the first 2 factors with the above expansion, and expand the product of that expansion and the 3rd factor:

(t^2 + 2*(t*[1-t]) + [1-t]^2)(t+[1-t])
= t^3 + t^2*[1-t] + (2*(t*[1-t])*t) + (2*(t*[1-t])*[1-t]) + ([1-t]^2)*t + [1-t]^3

= ~B0~||(t^3)|| + ~B1~||(t^2*[1-t]) + (2*(t*[1-t])*t)|| + ~B2~||(2*(t*[1-t])*[1-t]) + (([1-t]^2)*t)|| + ~B3~||([1-t]^3)||

B0(t) = t^3
B1(t) = 3*t^2*[1-t]
B2(t) = 3*t*[1-t]^2
B3(t) = [1-t]^3

/////////////////////////////////

Now we'll take the equation to the 4th power, resulting in a quartic equation:

1^4 = (t + [1 - t])(t + [1 - t])(t + [1 - t])(t + [1 - t])

Using the cubic results, replace the first 3 factors, then find the product of that equation and the 4th factor:

((t^3) + (3*t^2*[1-t]) + (3*t*[1-t]^2) + [1-t]^3)(t+[1-t])
= (t^4) + (t^3*[1-t]) + ((3*t^2*[1-t])*t) + ((3*t^2*[1-t])*[1-t]) + ((3*t*[1-t]^2)*t) + ((3*t*[1-t]^2)*[1-t]) +( [1-t]^3*t) +( [1-t]^3*[1-t])

= ~B0~||(t^4)|| + ~B1~||(t^3*[1-t]) + ((3*t^2*[1-t])*t)|| + ~B2~||((3*t^2*[1-t])*[1-t]) + ((3*t*[1-t]^2)*t)|| + ~B3~||((3*t*[1-t]^2)*[1-t]) +( [1-t]^3*t)|| +~B4~||( [1-t]^3*[1-t])||

B0(t) = t^4
B1(t) = 4*t^3*[1-t]
B2(t) = 6*t^2*[1-t]^2
B3(t) = 4*t*[1-t]^3
B4(t) = [1-t]^4

/////////////////////////////////

Finally, we'll take the equation to the 5th power, resulting in a quintic equation:

1^5 = (t + [1 - t])(t + [1 - t])(t + [1 - t])(t + [1 - t])(t + [1 - t])

Again, we'll now use the quartic results to find the product of that equation and the 5th factor:

((t^4) + (4*t^3*[1-t]) + (6*t^2*[1-t]^2) + (4*t*[1-t]^3) + ([1-t]^4))(t+[1-t])
= (t^5) + (t^4*[1-t]) + ((4*t^3*[1-t])*t) + ((4*t^3*[1-t])*[1-t]) + ((6*t^2*[1-t]^2)*t) + ((6*t^2*[1-t]^2)*[1-t]) + ((4*t*[1-t]^3)*t) + ((4*t*[1-t]^3)*[1-t]) + ([1-t]^4*t) + ([1-t]^4*[1-t])

= ~B0~||(t^5)|| + ~B1~||(t^4*[1-t]) + ((4*t^3*[1-t])*t)|| + ~B2~||((4*t^3*[1-t])*[1-t]) + ((6*t^2*[1-t]^2)*t)|| + ~B3~||((6*t^2*[1-t]^2)*[1-t]) + ((4*t*[1-t]^3)*t) + ~B4~||((4*t*[1-t]^3)*[1-t]) + (([1-t]^4)*t) + ~B5~||(([1-t]^4)*[1-t])||

B0(t) = t^5
B1(t) = 5*t^4*[1-t]
B2(t) = 10*t^3*[1-t]^2
B3(t) = 10*t^2*[1-t]^3
B4(t) = 5*t*[1-t]^4
B5(t) = [1-t]^5

/////////////////////////////////

Of course, you can extrapolate this syntax to as many powers as required, although you may find that cpu usage may rise with the extra complexity. Each basis function represents the arc between 2 points, resulting in (n)arcs and (n+1)points. I'll post the main code for the quintic bezier below ... you can download the quadratic, cubic and quartic bezier examples by Den Ivanov, written for Flash5, from Flashkit's download area. I'll also provide links to the quintic swf/fla below (the code is still Flash5, although published in FlashMX) ... BTW, many thanks to Den for providing the original source code.

After completing this post, I read your new message that states you are working on a 3d version. owel, you may find this post useful ... Den also has a 3d bezier version which I haven't yet explored, posted at his site ... http://flash.onego.ru ...under 'my new Flash MX stuff'.

--edit--

I took a quick look at Den's 3dsplines code, and found that he's used 'curveTo' to draw the splines ... so the integrated quadratic 3-point method has been utilized.

http://members.shaw.ca/flashmath101/quinticBezier.swf
http://members.shaw.ca/flashmath101/quinticBezier.fla
Code:
// quinticBezier
// adapted from: Den Ivanov - http://flash.onego.ru

// Bernstein basis functions
function B1 (t) {
    return (t * t * t * t * t);
}
function B2 (t) {
    return (5 * t * t * t * t * (1 - t));
}
function B3 (t) {
    return (10 * t * t * t * (1 - t) * (1 - t));
}

function B4 (t) {
    return (10 * t * t * (1 - t) * (1 - t) * (1 - t));
}

function B5 (t) {
    return (5 * t * (1 - t) * (1 - t) * (1 - t) * (1 - t));
}

function B6 (t) {
    return ((1 - t) * (1 - t) * (1 - t) * (1 - t) * (1 - t));
}
 
// draw spline routine : called from RedrawClip
function DrawSpline () {
    count = 0;
    // change this for smooth line
    detailBias = 1/30;
    level = 1;
    do {
        x = cP1._x*B1(count)+cP2._x*B2(count)+cP3._x*B3(count)+cP4._x*B4(count)+cP5._x*B5(count)+cP6._x*B6(count);
        y = cP1._y*B1(count)+cP2._y*B2(count)+cP3._y*B3(count)+cP4._y*B4(count)+cP5._y*B5(count)+cP6._y*B6(count);
        if (level>1) {
            attachMovie("line", "line"+level, level+10);
            _root["line"+level]._x = x;
            _root["line"+level]._y = y;
            _root["line"+level]._rotation = Math.atan2(yold-y, xold-x)*180/(Math.PI);
            _root["line"+level]._xscale = _root["line"+level]._yscale = Math.sqrt((xold-x)*(xold-x)+(yold-y)*(yold-y));
        }
        yold = y;
        xold = x;
        ++level;
        count += detailBias;
    } while (count<=1);
}

// main init : put 6 control point to stage
for (i=1; i<=6; i++) {
    attachMovie("cdot", "cP"+i, i);
    _root["cP"+i]._x = random(540);
    _root["cP"+i]._y = random(400);
    _root["cP"+i].n = i;
}
stop();
Richard
[Edited by Dickee on 08-23-2002 at 02:21 PM]
Hello everyone,
i'm new to this forums and to actionscript3.
I saw that this code is actionscript 2. Can someone translate to Actionscript 3? Moreover, the vars are not declared, so i don't know what kind of vars are: Number, DOuble.... etc...

Thank you very nuch.