r/OpenPythonSCAD • u/gadget3D • Oct 23 '26
Fourier epicycles as real gears – a parametric drawing machine in PythonSCAD
Title: Fourier epicycles as real gears – a parametric drawing machine in PythonSCAD
You've probably seen those videos where a chain of rotating circles draws a horse, a portrait or a signature. The math behind it is the complex Fourier series, and the best interactive explanation I know is Jez Swanson's An Interactive Introduction to Fourier Transforms: https://www.jezzamon.com/fourier/ – you can even draw your own shape there and watch it being rebuilt from circles.
I wondered: can this be built as real geometry? Turns out yes, and PythonSCAD is a perfect fit for it.
https://imgur.com/a/KfwOoTa
The idea in three lines
Treat the drawing as a complex curve z(t) = x(t) + i·y(t). Its Fourier series is
z(t) = Σ c_k · e^(i·k·t)
Every term is an arm of length |c_k| that starts at angle arg(c_k) and rotates k times per revolution. Chain the arms tip to tip and the last tip traces the shape.
Why this is buildable
The frequencies k are integers – and integer speed ratios are exactly what gears do. Each arm sits on a small planetary stage:
- a sun fixed to the arm two levels below (the base for arm 2),
- a planet fixed to the new arm, riding at the tip of the arm in between (the carrier).
With carrier speed c, sun speed s and ratio ρ = Z_sun / Z_planet:
- direct gear mesh: planet = c + (c − s)·ρ → gives 2×, 3×, …
- open belt: planet = c − (c − s)·ρ → gives 0, −1×, −2×, … (negative frequencies are essential for closed shapes!)
The script picks mesh or belt automatically, computes integer tooth counts, aligns the teeth so they really mesh in every pose, and refuses impossible combinations.
The collision problem
My first version had the pen hanging down through the whole stack – straight through the base gear. The fix: in this stack every part lives in its own layer, and the last arm is always the outermost one. So the whole mechanism hangs upside down from a bridge, the last arm is the lowest, and the pen draws downwards onto paper, resting on it by gravity in a sleeve. The motor sits on top of the bridge and drives arm 1 via a pinion. The frame columns are placed outside the computed swept envelope of all parts.
https://imgur.com/a/yEhWmkb
What can 4 arms draw?
| Shape | K | L | PHI |
|---|---|---|---|
| Heart | -1, -3, 2, -2 | 37.5, 9, 7.5, 7.5 | 90, 270, 270, 270 |
| 3-star, sharp tips | 1, -2, 4, -5 | 36, 18, 9, 5 | 0, 0, 0, 0 |
| 5-star with loops | 1, -4, 6, -9 | 36, 16, 8, 4 | 0, 0, 0, 0 |
| 7-rosette with loops | 1, -6, 8, -13 | 36, 14, 8, 4 | 0, 0, 0, 0 |
The heart is a genuine Fourier approximation: the four largest terms of the classic heart curve.
https://imgur.com/a/vet3rgW
Rules of thumb for your own designs:
- Symmetry: if all k leave the same remainder mod m, the curve has m-fold rotational symmetry (1, −2, 4, −5 → 3-fold).
- Sharp tips: a cusp appears where the pen velocity Σ k·L·e^(i(kθ+φ)) vanishes – e.g. 1·36 = 2·18 in the 3-star.
- Loops: a high frequency whose k·L comes close to that of the base term makes loops.
- Calm shapes: let L fall off roughly like 1/|k| or faster.
- Keep gear/belt ratios below ~1:8.
The model
Set K, L, PHI at the top, THETA is the drive angle of the shown pose (nice as an add_parameter slider to crank the machine by hand). The console prints the transmission of every stage and the swept envelope.
# Fourier epicycle drawing machine - assembled demo model for PythonSCAD
#
# Arm i rotates with the absolute angle K[i]*THETA + PHI[i] and has length L[i].
# The pen at the tip of the last arm therefore traces
# z(t) = sum_i L[i] * exp(j*(K[i]*t + PHI[i]))
#
# Stack (mechanism coordinates, built bottom-up, then flipped upside down):
# base plate -> drive layer (drive gear on arm 1 + motor pinion)
# -> arm 1 -> gear layer 1 -> arm 2 -> gear layer 2 -> ... -> arm N
# Every layer only holds parts at its own height; the only part crossing layers
# would be the pen. Since arm N is the outermost layer, the pen side beyond it is
# collision free. The mechanism therefore hangs from a bridge, arm N is lowest,
# and the pen draws downward onto paper (gravity-loaded in a sleeve).
#
# Arm i (i >= 2) is driven by a planetary stage: a "sun" fixed to the body two
# levels up the chain (base for arm 2) and a "planet" fixed to arm i, linked by
# direct gear mesh (planet = c + (c-s)*rho) or by an open belt
# (planet = c - (c-s)*rho), with c = carrier speed, s = sun speed, rho = Zsun/Zplanet.
# ---------------- parameters ----------------
= # absolute frequency of each arm (integer multiples of the drive)
= # arm lengths = amplitudes [mm]
= # phase of each arm [deg]
= 0 # drive angle of the displayed pose [deg]
= 3 # arm plate thickness
= 5 # gear / pulley layer thickness
= 0.5 # vertical play between layers
= 4 # bridge plate thickness
= 4 # radius of the arm ends
= 1.5 # axle radius
= 0.3 # radial clearance of rotating parts on their axles
= 12 # minimum tooth count of any planetary gear
= 5 # preferred radius of the planet pulley (belt stages)
= 1.2 # belt thickness
= 36 # teeth of the drive gear on arm 1
= 12 # teeth of the motor pinion
= 1.5 # module of the drive pair
= 0 # direction of the motor seen from the center [deg]
= 12
= 25
= 15 # distance from the lowest mechanism part to the paper
= 8 # pen guide sleeve below arm N
= 6
= 0.3
= 4 # frame column radius
= 4 # clearance between swept envelope and columns
= 0.6 # half line width of the traced curve
= 2 # sampling step of the traced curve [deg]
= 48
=
# ---------------- helpers ----------------
"""Polar to cartesian, angle in degrees."""
return
"""Rotate around own z axis by a degrees, then move to xy / height z."""
return
"""Balanced union to keep the CSG tree shallow."""
=
return
return
"""Simple trapezoidal-tooth gear outline, tooth 0 centered on +x."""
= 2 * /
, = + , - 1.25 *
= 360 /
=
return
return
"""Rotation of gear 2 so that it meshes with gear 1 (rotated a_s).
beta = direction from gear 1 center to gear 2 center."""
= * / 360
= * 360 / # offset of the sun tooth nearest the contact line
return + 180 - * / - 180 /
"""Plain pulley with an off-center mark so its rotation is visible."""
return -
# ---------------- kinematics ----------------
=
assert == and == ,
= + # body 0 = base (speed 0)
= + # absolute angle of body j
= # Pos[j] = tip of arm j
=
= # max distance of Pos[j] from center
= + + 2 *
= + # drive layer (mechanism coords)
return + + + * # bottom of arm i
return + + # gear layer below arm j+1
= + + 1 # incl. axle cap at the last joint
return
"""Transmission driving arm i (i >= 2): ('mesh'|'belt', rho = Zsun/Zplanet)."""
, , = , ,
=
return
# ---------------- mechanism (built upright, flipped later) ----------------
= # (object, color) in mechanism coordinates
= # largest radius swept by any moving or fixed part
=
# drive: gear on arm 1 + motor pinion on the bridge
, = * / 2, * / 2
=
= -
# bridge plate (radius fixed later, stored as placeholder index)
=
# arms (hole at the pivot, axle for the next arm fused at the tip)
, = ,
=
= -
# axles: post at Pos[j] is fixed to body j and carries the sun of the next stage
=
= +
# planetary stages
= - 1 # gear layer below arm i
, =
, = ,
=
= # carrier direction (sun -> planet)
=
=
, = * , *
, = * / , * /
= 2 * /
=
=
, = ,
= f
=
= *
, = ,
, = ,
=
=
=
= f
= -
= -
# frame geometry from the swept envelope
= + +
= + + 3
= -
=
# ---------------- world: flip mechanism, add frame, paper, pen ----------------
= +
= + + # world height of mechanism z = 0
return - # mechanism z -> world z
=
# table, paper, columns
= 2 *
= 2 *
=
# pen: sleeve fixed to arm N, pen resting on the paper by gravity
=
= # lower face of arm N in world coords
=
= -
= \
|
# traced curve on the paper
=
= = 0
=
, = + , +
=
Next steps on my list: printable parts (bearings, belt tooth profiles) and an automatic collision check over the full revolution using PythonSCAD's intersection. Happy to hear ideas – and if you find a nice 4-arm parameter set, post it!