简体   繁体   中英

Calculate the arclength, curve length of a cubic bezier curve. Why is not working?

I'm calculating the arclength (length of a cubic bezier curve) with this algorithm

    function getArcLength(path) {

        var STEPS = 1000; // > precision
        var t = 1 / STEPS;
        var aX=0;
        var aY=0;
        var bX=0, bY=0;
        var dX=0, dY=0;
        var dS = 0;
        var sumArc = 0;
        var j = 0;

        for (var i=0; i<STEPS; j = j + t) {
            aX = bezierPoint(j, path[0], path[2], path[4], path[6]);
            aY = bezierPoint(j, path[1], path[3], path[5], path[7]);
            dX = aX - bX;
            dY = aY - bY;
            // deltaS. Pitagora
            dS = Math.sqrt((dX * dX) + (dY * dY));
            sumArc = sumArc + dS;
            bX = aX;
            bY = aY;
            i++;
        }

        return sumArc;
    }

But what I get is something like 915. But the curve is 480 and no more. (I know for sure this because the curve is almost a line) The path array has this values: 498 51 500 52 500 53 500 530

The bezierPoint function is:

        function bezierPoint(t, o1, c1, c2, e1) {
        var C1 = (e1 - (3.0 * c2) + (3.0 * c1) - o1);
        var C2 = ((3.0 * c2) - (6.0 * c1) + (3.0 * o1));
        var C3 = ((3.0 * c1) - (3.0 * o1));
        var C4 = (o1);

        return ((C1*t*t*t) + (C2*t*t) + (C3*t) + C4)
    }

What I'm doing wrong?

Because bX and bY are initialized to 0, the first segment when i = 0 measures the distance from the origin to the start of the path. This adds an extra sqrt(498^2+51^2) to the length. If you initialize bX = path[0] and bY = path[1] , I think it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM