
public class BezierCurve
{
    private float[] m_controlPoints;
    private int m_N ;

    public BezierCurve(float[] theControlPoints)
    {
        m_controlPoints         = theControlPoints;
        m_N                     = m_controlPoints.length/3 - 1;
    }

    public float[] getPointsOnCurve(int theDivisions)
    {
        float[] points = new float[(theDivisions + 1) * 3];

        int j = 0;

        for (int i = 0; i <= (theDivisions); i++)
        {
            j = i * 3;

            float[] pointOnCurve = getPointOnCurve((float)(i)/(float)theDivisions);

            points[j]       = pointOnCurve[0];
            points[j + 1]   = pointOnCurve[1];
            points[j + 2]   = pointOnCurve[2];
        }

        return points;
    }

    public float[] getPointOnCurve(float theFrac)
    {
        float[] point = new float[3];

        int j = 0;

        double coeff;

        for (int k = 0; k <= m_N; k++)
        {
            j = k * 3;

            /*
                coeff = (N!/((N - K)! * K!))*(pow(U,K) * pow((1-U),(N-K)))
            */

            coeff = (GMath.factorial(m_N)/(GMath.factorial(k) *
                        GMath.factorial(m_N - k)));
            coeff = coeff * (Math.pow(theFrac, k) * Math.pow((1 -  theFrac),
                        (m_N - k)));

            point[0] = (float)(point[0] + coeff * m_controlPoints[j]);
            point[1] = (float)(point[1] + coeff * m_controlPoints[j + 1]);
            point[2] = (float)(point[2] + coeff * m_controlPoints[j + 2]);

        }
        return point;
    }
}