This site aims to be the ultimate resource for Adobe Illustrator(c) scripting. Here you will find regularly updated collection of scripts for all your creative needs.
If you find yourself doing the same task again and again, or you have an idea how to optimise your  workflow – get in touch, and we will provide solution.

Spirograph

This illustrator script will draw simple spirograph pattern using 2 circles as an input.

spirograph pattern generated with this script
Spirograph pattern generated with this script

A spirograph is a toy that uses straightforward mechanics to create a pleasing collection of curves that are formally referred to as roulette curves.

The inside edge of a large circle, which serves as the spirograph pattern’s outer border, is rolled by a small wheel without slipping. A pen is placed on the smaller wheel’s circumference or at another fixed location inside of it. The pen draws the roulette curve as the wheel spins.

The size ratio of the inner wheel to the outer circle and the location of the pen within the wheel are two factors that affect how the curve emerges. See how the curves change in shape by adjusting the settings listed above. These curves have a mathematical formula:

x(θ)=(R−r)cos(θ)+rpcos(R+rrθ)x(θ)=(R−r)cos⁡(θ)+rpcos⁡(R+rrθ)

y(θ)=(R−r)sin(θ)−rpsin(R+rrθ)y(θ)=(R−r)sin⁡(θ)−rpsin⁡(R+rrθ)

where:

R is the radius of the exterior circle,
r is the radius of the small rolling wheel,
p is the position of the pen within the small rolling wheel (0-1),

and

θ measures distance along the curve.