Skip to content

Instantly share code, notes, and snippets.

@foota
Created January 25, 2014 13:44
Show Gist options
  • Save foota/8616573 to your computer and use it in GitHub Desktop.
Save foota/8616573 to your computer and use it in GitHub Desktop.
Spirograph in Python with matplotlib.
#!/usr/bin/env python
import sys, os
from pylab import *
gcd = lambda x, y: y and gcd(y, x % y) or x
fx = lambda t, rc, rm, rd: (rc - rm) * cos(t * pi) + rd * cos(t * pi * (rc - rm) / rm)
fy = lambda t, rc, rm, rd: (rc - rm) * sin(t * pi) - rd * sin(t * pi * (rc - rm) / rm)
p = lambda x, rc, rm, rd: plot(fx(x, rc, rm, rd), fy(x, rc, rm, rd))
def spirograph(rc, rm, rd, div=1000):
p(arange(0.0, 2.0 * rm / gcd(rc, rm), 1.0 / div), rc, rm, rd)
axis((min(axis()[::2]), max(axis()[1::2])) * 2)
show()
def main(args):
if len(args) < 4:
print >>sys.stderr, "Usage: %s rc rm rd [div=1000]" % os.path.basename(args[0])
print >>sys.stderr, " Ex. %s 31 16 11" % os.path.basename(args[0])
sys.exit(1)
wm = matplotlib.pyplot.get_current_fig_manager()
wm.window.wm_geometry("500x500+50+50")
spirograph(*map(int, args[1:]))
if __name__ == "__main__": main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment