Sweeping mathematical curves in OpenSCAD

From Wikistix

Searching around, I didn't see a whole heap of guidance here, but after some thought and experimentation, I found a perfectly workable solution.

OpenSCAD doesn't have a sweep function or operator; however, the hull transformation can be used for this purpose. My first attempt was the "trefoil knot", a relatively simple trigonometric curve represented by the parametric equations:

[math]\displaystyle{ \begin{align} x & = \cos t + 2\cos 2t\\ y & = \sin t - 2\sin 2t\\ z & = -\sin 3t\\ \end{align} }[/math]

The OpenSCAD solution was to break the curve into short steps, and "sweep" a hull between spheres translated to successive points on the curve. The rendering is very clean, and generates a clean stl file read for slicing and 3D-printing.

Trefoil knot rendered in OpenSCAD
// Trefoil knot
// stix@stix.id.au 2022-08-30

// 10 for testing, 50 for final - renders in ~8m
stepsize = 180 / $fn;

function trefoil(t) = [
	cos(t) + 2 * cos(2 * t),
	sin(t) - 2 * sin(2 * t),
	-sin(3 * t)

union() {
	for(a = [0 : stepsize : 360]) {
		hull() {
			translate(trefoil(a)) sphere(1);
			translate(trefoil(a + stepsize)) sphere(1);

Misinformation found herein copyright Paul Ripke (aka “stix”) stixpjr@gmail.com.