Wrote this code after I was inspired by this webpage http://www.math.com/students/wonders/spirographs.html. Had some initial difficulties with memory but I discovered Psyco while reading "Dreaming in Code" (recommended by this community). It can speed up your code and really helped out with the rapid calculations and drawing for this program. http://www.voidspace.org.uk/python/modules.shtml#psyco.
If you are running this code on a desktop you may or may not need it. If the GUI runs slow or not at all then choose the command line. Also if anyone knows how to optimize this code please let me know because it has me scratching my head. Most of my research said to just chalk this up to the pros and cons of Python vs C.
Thanks for all the inspiration this community gives me. You all challenge me to push myself everyday.
Phil
#Spirograph Maker inspired by http://www.math.com/students/wonders/spirographs.html
from visual import *
from visual.controls import *
from string import *
iterations = input('How many iterations?')
class spiro():
def __init__(self,iterations):
self.R = 65.0 #Radius of Fixed Circle
self.r = 43.0 #Radius of Moving Circle
self.o = 75.0 #offset of pen point in moving circle
self.iterations = iterations
self.t = arange(0,self.iterations,1)
self.drawing = curve(x = ((self.R+self.r)*cos(self.t) - (self.r+self.o)*cos(((self.R+self.r)/self.r)*self.t)), y = ((self.R+self.r)*sin(self.t) - (self.r+self.o)*sin(((self.R+self.r)/self.r)*self.t)), color = color.green)
def fixedRadius(self,fRadius):
if GUIquestion == 1:
self.R = fRadius.value
else:
self.R = fRadius
self.redraw()
def rollingRadius(self,rRadius):
if GUIquestion == 1:
self.r = rRadius.value
else:
self.r = rRadius
self.redraw()
def penOffset(self,pOffset):
if GUIquestion == 1:
self.o = pOffset.value
else:
self.o = pOffset
self.redraw()
def redraw(self):
self.drawing.x = ((self.R+self.r)*cos(self.t) - (self.r+self.o)*cos(((self.R+self.r)/self.r)*self.t))
self.drawing.y = ((self.R+self.r)*sin(self.t) - (self.r+self.o)*sin(((self.R+self.r)/self.r)*self.t))
class GUI():
def __init__(self):
self.c = controls(x=0,y=0,width=400, height = 400, range=100)
self.s1 = slider(pos=(0,40),width=7, length=40, axis=(1,0,0),min=1,max=100,action=lambda:
Spirograph.fixedRadius(self.s1))
self.s2 = slider(pos=(0,0),width=7, length=40, axis=(1,0,0),min=1,max=100,action=lambda:
Spirograph.rollingRadius(self.s2))
self.s3 = slider(pos=(0,-40),width=7, length=40, axis=(1,0,0),min=1,max=100,action=lambda:
Spirograph.penOffset(self.s3))
self.fixedRadiusLabel = label(pos=self.s1.pos, display = self.c.display, text='Fixed Radius', line = 0,
xoffset=0, yoffset=20, height=10, border=0)
self.rollingRadiusLabel = label(pos=self.s2.pos, display = self.c.display, text='Rolling Radius', line = 0,
xoffset=0, yoffset=20, height=10, border=0)
self.penOffsetLabel = label(pos=self.s3.pos, display = self.c.display, text='Pre-Offset', line = 0,
xoffset=0, yoffset=20, height=10, border=0)
self.scene1 = display(title = 'Spirograph!', x=410, y=0, width=400, height = 400)
self.scene1.select()
memload = 0
memquestion = 0
GUIquestion = 0
while(memquestion == 0):
memload = upper(raw_input('(H)igh Memory GUI or (L)ow Memory Command Line:'))
if memload == 'H':
memquestion = 1
GUIquestion = 1
psycoquestion = upper(raw_input('Do you have Psyco (the memory accelerator installed)? Yes or No:'))
if (psycoquestion == 'Y') or (psycoquestion == 'YES'):
import psyco
psyco.full()
WYSIWYG = GUI()
Spirograph = spiro(iterations)
qbutton = 0
while(qbutton == 0):
WYSIWYG.c.interact()
if memload == 'L':
Spirograph = spiro(iterations)
command = ''
while(command !='Q'):
print('Adjust which factor of the spirograph?')
print('Fixed Radius =',Spirograph.R, ' Moving Radius=',Spirograph.r, ' Offset of Pen=', Spirograph.o)
command = upper(raw_input('(F)ixed Radius, (M)oving Radius, (O)ffset of the Pen, (Q)uit?'))
if command == 'I':
value = input('What is the new value?')
Spirograph.numIterations(value)
if command == 'F':
value = input('What is the new value?')
Spirograph.fixedRadius(value)
if command == 'M':
value = input('What is the new value?')
Spirograph.rollingRadius(value)
if command == 'O':
value = input('What is the new value?')
Spirograph.penOffset(value)
--
http://www.google.com/profiles/philhwagner
http://staff.hthcv.hightechhigh.org/~pwagner/ (My Digital Portfolio)