Cheap way of calculating cubic bezier length

后端 未结 6 2046
遇见更好的自我
遇见更好的自我 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

    in my case a fast and valid approach is this. (Rewritten in c# for Unity3d)

    public static float BezierSingleLength(Vector3[] points){
        var p0 = points[0] - points[1];
        var p1 = points[2] - points[1];
        var p2 = new Vector3();
        var p3 = points[3]-points[2];
    
        var l0 = p0.magnitude;
        var l1 = p1.magnitude;
        var l3 = p3.magnitude;
        if(l0 > 0) p0 /= l0;
        if(l1 > 0) p1 /= l1;
        if(l3 > 0) p3 /= l3;
    
        p2 = -p1;
        var a = Mathf.Abs(Vector3.Dot(p0,p1)) + Mathf.Abs(Vector3.Dot(p2,p3));
        if(a > 1.98f || l0 + l1 + l3 < (4 - a)*8) return l0+l1+l3;
    
        var bl = new Vector3[4];
        var br = new Vector3[4];
    
        bl[0] = points[0];
        bl[1] = (points[0]+points[1]) * 0.5f;
    
        var mid = (points[1]+points[2]) * 0.5f;
    
        bl[2] = (bl[1]+mid) * 0.5f;
        br[3] = points[3];
        br[2] = (points[2]+points[3]) * 0.5f;
        br[1] = (br[2]+mid) * 0.5f;
        br[0] = (br[1]+bl[2]) * 0.5f;
        bl[3] = br[0];
    
        return BezierSingleLength(bl) + BezierSingleLength(br);
    }
    

提交回复
热议问题