The most straightforward way to accomplish this in iOS is using a string of smooth curves defined in a UIBezierPath, however it is up to the developer to construct the cubic bezier curves in the path in such a way that they are smooth and pass exactly through the data.The linear segments are very easy to add via addLineToPoint:, but what about a curved shape.
![]() In the picture below, each point is a 2D (x,y) point in a Euclidean space. C1s first control point corresponds to the start of the UIBezierPath at P0(77.5, 36.5), its first control point at (67.78, 56.83) corresponds to P1, and so on. It follows that for a set of N points, we can create N-1 curves that pass through every point. For the transition between two adjacent curves to be smooth, the adjacent control points (P2 in the first curve, and P1 in the second) must be at least co-linear, and ideally we would also like them to have the same length. ![]() These curves are defined by the set of interpolating points and both are readily converted to a set of piecewise cubic Bezier curves meaning that given N fitting points, we can create the control points for N-1 cubic Bezier curves that match the Hermite or Catmull-Rom splines. Bezier Curve App Code Linked BelowComputing the corresponding cubic Bezier control points for a Hermite curve is very simple (see code linked below in the example project), but they exhibit problems such as extremely high curvature kinks and loops when points are irregularly spaced. Curve B, however, has kinks and self-intersections due to the irregularly-spaced points. Like Hermite curves, Catmull-Rom curves will pass through the interpolation points and generate smooth results, but they also provide additional control a scalar alpha value (between 0.0 and 1.0) that controls the tangent magnitudes. For details, see this excellent paper, titled On the Parameterization of Catmull-Rom Curves, which discusses the effect of alpha. One caveat is that the computation for the piecewise cubic Bezier curves representing the Catmull-Rom curve at a given interpolation point Pn takes into account the points Pn-1, Pn, Pn1 and Pn2, so the cubic Bezier curves generated will not pass through the first and last fit points. Additional points can be added, or the curve can be created as a closed loop. Additionally, the Catmull-Rom method is passed an alpha value between 0.0 and 1.0. The Hermite interpolation method uses tangents calculated with the finite differences method. The app lets a user play with the values and see their effect dynamicall. Ultimately, choosing a technique for fitting is less of a science than an art, since the quality of the result is often subjective. I have found the Catmull-Rom results are reasonable for a vast array of data sets. For example if I draw on curve and fill color and want to select other portion and fill with color. Fill out this form and well get back to you within two business days.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |