rendering spirals

August 26, 2010

After posting this Stack Overflow question, I learned that rendering spirals was more complicated than I first thought. The best approach seemed to be to use Bezier splines, specifically cubic Beziers, since those are the variety usually found in graphics APIs. There is some calculus involved in approximating a simple circle with Beziers, described in this PDF. I studied this and made some edits to the Bezier spline Wikipedia article.

Applying the method in that PDF, I would need to re-derive this approximation formula for an Archimedean spiral rather than a circle. I read a tutorial on using wxMaxima, then created a worksheet to chug through the derivation. The spiral may be composed of an arbitrary number of small, non-circular arcs. Specifying a cubic Bezier curve requires four points (xi, yi). The eight values may be obtained by solving a system of eight simultaneous linear equations: the starting and ending point of the curve (2 equations for each x and y component); the slope at the starting point and end point (2 equations); and some other point along the curve (2 equations). For the last point, the angular midpoint was selected. A common linear algebra package was used to solve the equations.

The control points must be recomputed for each segment in order to draw a full spiral. It is not correct to simply reuse a scaled and rotated version of a single segment, since for an Archimedian spiral (with constant separation distance between spiral turns) the curvature decreases as the radius increases.

Logarithmic spirals would potentially allow the reuse of segments, but with the Archimedian spiral framework written, it is easy enough to plug in an exponential function in place of the linear polar function.

I created a Python GUI app to explore the parameters. The files can be downloaded here. It requires the Ubuntu package “python-scipy” to run.

Here is the result as a Live Wallpaper for Android. I sold it for $0.99 for a while, but it earned little enough that I decided to release it as open source.

Advertisements