Quadratic Bezier curve length post

01/14/2013 with tags : future, time, math, bezier

Quadratic Bezier curves are defined by second order polynominal, and can be written as

Quadratic Bezier Curve

where t is real parameter with values in range [0,1]. P's are respectively curve starting point, anchor point and the end point. Derivative of the quadratic Bezier curve can be written as

Quadratic Bezier Curve derivative

Length of any parametric (in general length of any well defined curve) curve can be computated using curve length integral. In case of 2nd order Bezier curve, using its derivatives, this integral can be written as

Quadratic Bezier Curve integral

To simplify this integral we can make some substitutions. In this case it we will look like this

Next after doing some algebra and grouping elements in order to parameter t we will do another substitutions (to make this integral easier)

Finally we get simplified inegral, that can be written in form

Integral to calculate

This integral can be 'easily' simplified and calculated using relation

integral

but we need to do some more substitutions

After doing elementary algebra we finaly get our expression. To calculate length of quadratic Bezier curve with this expression all we need are coordinates of end points and control point. We dont need to use iterative methods anymore. quadratic bezier curve

Accuracy of this evaluation

To check accuracy we will evalute length of quadratic bezier curve using previously calculated expression and approximation algorithm. Used algorithm approximates a Bezier curve with a set of line segments and calculates curve length as a sum over lengths of all that line segments. In this case we will use a series of quadratic bezier curves. All curves have the same end points. For each curve control point is taken from a set of points equally spaced on a circle, with given radius and centered in the middle between end points. Presented plot shows comparison of curve length calculated using both (our expression and line approximation) methods for a set of Bezier quadric curves. Green line shows results from approximation method, curve lengths calculated using our expression are drawn with red cross

accuracy

Implementation

Implementation in c language can look as follows

float blen(v* p0, v* p1, v* p2)
{
 v a,b;
 a.x = p0->x - 2*p1->x + p2->x;
 a.y = p0->y - 2*p1->y + p2->y;
 b.x = 2*p1->x - 2*p0->x;
 b.y = 2*p1->y - 2*p0->y;
 float A = 4*(a.x*a.x + a.y*a.y);
 float B = 4*(a.x*b.x + a.y*b.y);
 float C = b.x*b.x + b.y*b.y;

 float Sabc = 2*sqrt(A+B+C);
 float A_2 = sqrt(A);
 float A_32 = 2*A*A_2;
 float C_2 = 2*sqrt(C);
 float BA = B/A_2;

 return ( A_32*Sabc + 
          A_2*B*(Sabc-C_2) + 
          (4*C*A-B*B)*log( (2*A_2+BA+Sabc)/(BA+C_2) ) 
        )/(4*A_32);
};