Cardinal Splines in Java

Ever wanted to draw a curve passing through different points?

Cardinal Spline

Java2D has bezier curves implemented as QuadCurve2D and CubicCurve2D. The problem is that you must specify some control points, and you don't know how to compute those to make the curve "smooth".

The solution is to use the so called Cardinal Splines, a special type of spline that passes through a set of points and, more importantly, that maintains the continuity of the slopes of the curve at those points (thus making transition "smooth" at the joint points).

The .NET framework seems to have a special function for this, but Java2D has not.

I've built a quick and dirty implementation of Cardinal Spline that you can use in your Java2D software. It generates a GeneralPath that represents the curve through an array of (at least 3) points.

You can run a demo with Java Web Start (JDK 1.5+).

If you improve it in any way I'd appreciate you sending me the improvements.

Enjoy, Antonio

blog comments powered by Disqus