Cheap way of calculating cubic bezier length

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

    public float FastArcLength()
    {
        float arcLength = 0.0f;
        ArcLengthUtil(cp0.position, cp1.position, cp2.position, cp3.position, 5, ref arcLength);
        return arcLength;
    }
    
    private void ArcLengthUtil(Vector3 A, Vector3 B, Vector3 C, Vector3 D, uint subdiv, ref float L)
    {
        if (subdiv > 0)
        {
            Vector3 a = A + (B - A) * 0.5f;
            Vector3 b = B + (C - B) * 0.5f;
            Vector3 c = C + (D - C) * 0.5f;
            Vector3 d = a + (b - a) * 0.5f;
            Vector3 e = b + (c - b) * 0.5f;
            Vector3 f = d + (e - d) * 0.5f;
    
            // left branch
            ArcLengthUtil(A, a, d, f, subdiv - 1, ref L);
            // right branch
            ArcLengthUtil(f, e, c, D, subdiv - 1, ref L);
        }
        else
        {
            float controlNetLength = (B-A).magnitude + (C - B).magnitude + (D - C).magnitude;
            float chordLength = (D - A).magnitude;
            L += (chordLength + controlNetLength) / 2.0f;
        }
    }
    

提交回复
热议问题