I'm learning about Bezier curves and would like to parameterize the equations for distance using an estimation method. So far, my code seems to work for single points (EG Bezier(start=0, mid=1, end=5, nPoints=6) yields [0 1 2 3 4 5]). However, when I attempt to apply this to multi-dimensional curves, my results are not as expected.

C# code (executed in Unity for visualization). The function (should) get a point on the curve (defined by the points pts) at a length l% of the length.

Vector3 BezierL(Vector3[] pts, float l)
    int i;
    float[] tVals = new float[n];
    Vector3[] points = new Vector3[n];
    float[] cumDist = new float[n];
        tVals[i] = i / (float)(n - 1);
        points[i] = Bezier(pts, tVals[i]);
        cumDist[i] = cumDist[i - 1] + 
            (points[i] - points[i - 1]).magnitude;
    // Interpolate to estimate t
    float targetLen = l * cumDist[n - 1];
    int ind = Array.BinarySearch(cumDist, targetLen);
    if (ind < 0)
        ind = ~ind;
    float t = Mathf.Lerp(tVals[ind - 1], tVals[ind],
        (targetLen - cumDist[ind - 1]) / (cumDist[ind] - cumDist[ind - 1]));
    return Bezier(pts, t);

where Bezier(Vector3[] pts, t) gets a point on the curve defined by pts at time t. For whatever reason, this partially works in that all points are equally spaced, but some points are stacked at the initial point rather than being distributed along the curve.

This was my reference for developing this algorithm, so I'm unsure if my implementation is incorrect, or if it only applies to lower-dimensional curves.

Thanks in advance!

  • 1
    It seems that the points[0] is never computed. – fang Jun 24 '18 at 22:43

Oof how embarrassing, I just forgot to compute the 0th point!

  • 1
    This should be a comment, not an answer. Especially given what the solution was, this was basically a non-question that was solved by "an extra pair of eyes" rather than a genuine programming problem, and it kind of has its own dedicated closing option. It's probably worth deleting this question again. – Mike 'Pomax' Kamermans Jun 29 '18 at 22:08

Your Answer

