Cheap way of calculating cubic bezier length

后端 未结 6 2057
遇见更好的自我
遇见更好的自我 2021-02-04 03:12

An analytical solution for cubic bezier length seems not to exist, but it does not mean that coding a cheap solution does not exist. By cheap I mean something like in the rang

6条回答
  •  终归单人心
    2021-02-04 03:36

    I worked out the closed form expression of length for a 3 point Bezier (below). I've not attempted to work out a closed form for 4+ points. This would most likely be difficult or complicated to represent and handle. However, a numerical approximation technique such as a Runge-Kutta integration algorithm (see my Q&A here for details) would work quite well by integrating using the arc length formula.

    Here is some Java code for the arc length of a 3 point Bezier, with points a,b, and c.

        v.x = 2*(b.x - a.x);
        v.y = 2*(b.y - a.y);
        w.x = c.x - 2*b.x + a.x;
        w.y = c.y - 2*b.y + a.y;
    
        uu = 4*(w.x*w.x + w.y*w.y);
    
        if(uu < 0.00001)
        {
            return (float) Math.sqrt((c.x - a.x)*(c.x - a.x) + (c.y - a.y)*(c.y - a.y));
        }
    
        vv = 4*(v.x*w.x + v.y*w.y);
        ww = v.x*v.x + v.y*v.y;
    
        t1 = (float) (2*Math.sqrt(uu*(uu + vv + ww)));
        t2 = 2*uu+vv;
        t3 = vv*vv - 4*uu*ww;
        t4 = (float) (2*Math.sqrt(uu*ww));
    
        return (float) ((t1*t2 - t3*Math.log(t2+t1) -(vv*t4 - t3*Math.log(vv+t4))) / (8*Math.pow(uu, 1.5)));
    

提交回复
热议问题