Trying to find length of a bezier curve with 4 points

前端 未结 2 717
生来不讨喜
生来不讨喜 2020-12-29 00:27

I\'ve found about a 1000 answers to this question, but none of them I can use, because I\'m using 4 control points with my curves.

That said, I stumbled on this guy

相关标签:
2条回答
  • 2020-12-29 01:15

    A 2-dimensional object, or a Point2D, is simply a Vector, and Vector Arithmetic is well-defined in mathematics. For example:

              k*(x,y) = (k*x, k*y)
               -(x,y) = (-1)*(x,y)
    (x1,y1) + (x2,y2) = (x1+x2, y1+y2)
    

    Those are all the formulas you need to calculate k1, k2, k3, and k4

    0 讨论(0)
  • 2020-12-29 01:19

    Here’s how to traverse your cubic bezier curve at uniform speed

    There isn't one simple formula for getting even length segments along a cubic bezier curve (meaning even arc-length segments). What's involved is calculating many points along the curve and then using interpolation to “nudge” each point into being roughly equidistant.

    I can get you nearly there without your having to get a PH.D in Mathematics.

    Start by using the common formula to calculate x/y points on the curve from t=0 to t=1 where t=0 represents the startpoint of the curve and t=1 represents the endpoint of the curve. This is the common formula:

    // calc the x/y point at t interval
    // t=0 at startPt, t=1 at endPt
    var x=CubicN(t,startPt.x,controlPt1.x,controlPt2.x,endPt.x);
    var y=CubicN(t,startPt.y,controlPt1.y,controlPt2.y,endPt.y);
    
    // cubic helper formula at t interval
    function CubicN(t, a,b,c,d) {
        var t2 = t * t;
        var t3 = t2 * t;
        return a + (-a * 3 + t * (3 * a - a * t)) * t
        + (3 * b + t * (-6 * b + b * 3 * t)) * t
        + (c * 3 - c * 3 * t) * t2
        + d * t3;
    }
    

    If you calculate enough intervals, say 100 intervals (t += .01 each loop) then you will get a very good approximation of the curve.

    That means if you connect the 100 points with lines, the result would look very much like a cubic bezier curve.

    But you're not done!

    The series of x/y points calculated above are not uniform in arc-distance from each other.

    Some neighboring points are close together and some neighboring points are farther apart.

    To calculate evenly distributed points:

    1. Connect all the points with lines (creating a polyline).
    2. Calculate the total distance (T) of that polyline.
    3. Divide (T) by the number of uniform segments you desire, getting the uniform segment length (SL)
    4. Finally, walk the polyline from start to end, calculating each point that is (SL) distance from the previous point.

    Result: you can use these equidistant points to traverse your curve.

    Additional Refinement: This should result in a visually smooth movement along your Bezier path. But if you desire even more smoothness, just calculate more than 100 points--more points == more smoothness.

    0 讨论(0)
提交回复
热议问题