As the title of this blog indicates, I'm a fan of linearity. But sometimes a little non-linearity makes things more interesting. A convenient way to generate non-linear curved lines is to use Bezier Curves. Bezier Curves are curves defined by polynomials. Bezier curves can be defined for polynomials of any degree, but a popular choice is to use cubic Bezier curves defined by polynomials of degree 3. These are relatively easy to implement, visually pleasing, and versatile since they can model ogee or sigmoid ("S"-shaped) curves.
A single cubic Bezier curve is specified by four points: two endpoints forming the baseline, and two control points. The curve shape lies within the quadrilateral convex hull of these points.
Note: the images in this post are created using the JTS TestBuilder.
A sequence of Bezier curves can be chained together to form a curved path of any required shape. There are several ways to join composite Bezier curves. The simplest join constraint is C0-continuity: the curves touch at endpoints, but the join may be a sharp angle. C1-continuity (differentiable) makes the join smooth. This requires the control vectors at a join point to be collinear and opposite. If the control vectors are of different lengths there will be a different radius of curvature on either side. The most visually appealing join is provided by C2-continuity (twice-differentiable), where the curvature is identical on both sides of the join. To provide this the control vectors at a vertex must be collinear, opposite and have the same length.
There are two ways of specifying the control points needed to define the curve:
Alpha (Curvedness) Parameter
The easiest way to define the shape of a curve is via the parameter alpha, which indicates the "curvedness". This value is used to automatically generate the control points at each vertex of the baseline. A value of 1 creates a roughly circular curve at right angles. Higher values of alpha make the result more curved; lower values (down to 0) make the curve flatter.
Alpha is used to determine the length of the control vectors at each vertex. The control vectors on either side of the vertex are collinear and of equal length, which provides C2-continuity. The angle of the control vectors is perpendicular to the bisector of the vertex angle, to make the curve symmetrical.
Explicit Control Points
Alternatively, the Bezier curve control points can be provided explicitly. This gives complete control over the shape of the generated curve. Two control points are required for each line segment of the baseline geometry, in the same order. A convenient way to provide these is as a LineString (or MultiLineString for composite geometries) containing the required number of vertices.
When using this approach only C0-continuity is provided automatically. The caller must enforce C1 or C2-continuity via suitable positioning of the control points.
Bezier Curve defined by control points showing C0 and C1 continuity
- Allow specifying the number of vertices used to approximate each curve
- Add a function to return the constructed control vectors (e.g. for display and analysis purposes)
- Make specifying explicit control points easier by generating C2-continuous control vectors from a single control point at each vertex