In my previous post “Calculating The Length of a Bezier Curve. Part I” I computed the length of a curve using a brute force method which in reality was really bad. But due to the time constraints I was forced to use it and made the grave error of posting it. It has been bugging me ever since, as I knew I had done a poor job. Here is a more efficient way to compute the length of a Bezier curve.

Recall the Bezier form is as follows:

Which in turn, for a cubic Bezier curve, it expands into

Here is some code that could help you calculate the length of the curve.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
bezLength( BezierSegment c ) local inc = 10 local length = 0.0 local t= 0 local pt = { x, y } local prevPt = { x, y } for i = 0, inc do t = i / steps local t1 = 1.0 - t; local t1_3 = t1*t1*t1 local t1_3a = (3*t)*(t1*t1) local t1_3b = (3*(t*t))*t1; local t1_3c = (t * t * t ) pt.x = (c[1].x * t1_3) + (t1_3a * c[2].x) + (t1_3b * c[3]).x + (t1_3c * c[4].x) pt.y = (c[1].y * t1_3) + (t1_3a * c[2].y) + (t1_3b * c[3].y) + (t1_3c * c[4].y) if i > 0 then local x = pt.x – prevPt.x local y = pt.y – prevPt.y length = length + math.sqrt(x*x+y*y) end prevPt.x = pt.x prevPt.y = pt.y end return |

<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 18px; line-height: 1.5; background-color: #ffffff;">This is pretty much the same code as my previous post but it allows for just a call to calculate the length of the curve.</span>

I think there’s a mistake on line 20. It looks like you should be assigning to pt.y, but instead you are reassigning pt.x