Skip to main content

Posts

Showing posts with the label curve fitting

Smooth as...

For a current project I needed a polygon smoothing algorithm in Java that: generated a curve passing through all polygon vertices had some way of controlling tightness of fit to the polygon fell within the domain of my very limited mathematical grasp Luckily for me, Maxim Shemanarev had not only worked out a Bezier curve algorithm that satisfied all of the above, but he also published a beautifully clear explanation of how it works note 1 . Maxim also provided some illustrative C code, but my Java implementation below is an independent effort (ie. all mistakes are my fault). It uses geometry objects from the JTS library . Below is an example of the algorithm in action. The original polygon is in black. The smoothed polygon composed of fitted cubic Bezier segments is in blue. The red points and lines indicate positions of the Bezier control points calculated with Maxim's algorithm. There are a pair of control points for each vertex of the input polygon. The tightness of fit of th...

Cubic spline interpolation

I spent a while today casting around for a simple, fast and just-good-enough cubic interpolation algorithm to use in the GeoTools library where it is to serve as a 'filter function' that can be used when rendering maps. The ACM digitial library has a mind-boggling collection of references on interpolation methods, most of which were fascinating but also over the top for this application. In the end I put together the following code based on a Wikipedia article about cubic Hermite splines . Here it is... /** * Cubic hermite spline interpolation. This is adapted from the description of the * algorithm at: http://en.wikipedia.org/wiki/Cubic_Hermite_spline. * Tangent caculations are done with simple finite differencing in the interests * of speed. * <p> * The input arrays xi and yi contain the coordinates of four interpolation * points defining three segments with the middle segment containing the point * for which we seek an interpolated value. * * @param x x ordi...