Cheap way of calculating cubic bezier length

user2214913 picture user2214913 · Apr 3, 2015 · Viewed 16.6k times · Source

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 range of 50-100 ns (or less).

Does someone know anything like that? Maybe in two categories:

1) less error like 1% but more slow code. 2) more error like 20% but faster?

I scanned through google a bit but it doesn't find anything which looks like a nice solution. Only something like divide on N line segments and sum the N sqrt - too slow for more precision, and probably too inaccurate for 2 or 3 segments.

Is there anything better?

Answer

Nic picture Nic · Jun 16, 2016

Another option is to estimate the arc length as the average between the chord and the control net. In practice:

Bezier bezier = Bezier (p0, p1, p2, p3);

chord = (p3-p0).Length;
cont_net = (p0 - p1).Length + (p2 - p1).Length + (p3 - p2).Length;

app_arc_length = (cont_net + chord) / 2;

You can then recursively split your spline segment into two segments and calculate the arc length up to convergence. I tested myself and it actually converges pretty fast. I got the idea from this forum.