from math import sin, cos, tan, pi

def zero(x): return 0

def spirograph(R, r, O, t1, dt, S=0, f=zero, g=zero,):
    S = S or max(R+r, r+O)
    r = float(r)
    t=0
    n=1
    m=5
    while t < t1:
        t += dt
        x = (R+r)*cos(t) - (r+O)*cos(((R+r)/r)*t)
        y = (R+r)*sin(t) - (r+O)*sin(((R+r)/r)*t)
        z = (R+r)*sin(t*n/2*loop/maxloop)*cos(t*n/2*loop/maxloop) - (r+O)*sin(((R+r)/r)*t*m/2*loop/maxloop)*cos(((R+r)/r)*t*m/2*(loop/maxloop))
#rectangular spiro       print "G1 X%f Y%f Z%f" % (x/S + f(t/t1), y/S + g(t/t1), z/S/zscale)
        print "G1 X%f Y%f Z%f" % (x/S, y/S, z/S/zscale)

def x(t):
    t *= perimeter
    if t < width:
        return t
    if t < width+height:
        return width
    if t < width+height+width:
        return width - (t-(width+height))
    return 0

def y(t):
    t *= perimeter
    if t < width:
        return 0
    if t < width+height:
        return (t - width)
    if t < width+height+width:
        return height
    return height - (t -(width+height+width))

print "g0 x0 y0"
print "f100"
width=3
height=3
zscale=2.1
perimeter = (2*width + 2*height)
n=1
m=1
#spirograph(30,30.9,30, pi, pi/64, 20, x, y, )
loop=1.0
dloop=0.30 #fineness of spacing between passes
maxloop=84

while loop < maxloop:
    spirograph(3.0,3.09,(loop*dloop), pi, pi/64, 20, x, y, )
    loop += 1

 
print "m2"
