4
__version__ = "$Revision: 1.3 $"
5
__date__ = "$Date: 2004/08/12 19:19:02 $"
9
this is a direct port of the Java applet at
10
http://www.wordsmith.org/anu/java/spirograph.html
13
from PythonCard import clipboard, dialog, graphic, model
19
class Spirograph(model.Background):
21
def on_initialize(self, event):
23
comp = self.components
25
'stcFixedCircleRadius':comp.stcFixedCircleRadius.text,
26
'stcMovingCircleRadius':comp.stcMovingCircleRadius.text,
27
'stcMovingCircleOffset':comp.stcMovingCircleOffset.text,
28
'stcRevolutionsInRadians':comp.stcRevolutionsInRadians.text,
30
self.setSliderLabels()
31
if self.components.chkDarkCanvas.checked:
32
self.components.bufOff.backgroundColor = 'black'
33
self.components.bufOff.clear()
36
def doSpirograph(self):
37
comp = self.components
40
width, height = canvas.size
44
R = comp.sldFixedCircleRadius.value
45
r = comp.sldMovingCircleRadius.value
46
O = comp.sldMovingCircleOffset.value
47
revolutions = comp.sldRevolutionsInRadians.value
49
color = comp.btnColor.backgroundColor
50
canvas.foregroundColor = color
52
canvas.autoRefresh = 0
55
if comp.radDrawingStyle.stringSelection == 'Lines':
63
# avoid divide by zero errors
68
# avoid divide by zero errors
71
exprResult = (rSum * t) / r
72
lastX = rSum*math.cos(t) - O*math.cos(exprResult) + xOffset
73
lastY = rSum*math.sin(t) - O*math.sin(exprResult) + yOffset
76
while abs(t) <= revolutions:
77
exprResult = (rSum * t) / r
78
x = rSum*math.cos(t) - O*math.cos(exprResult) + xOffset
79
y = rSum*math.sin(t) - O*math.sin(exprResult) + yOffset
81
points.append((lastX, lastY, x, y))
89
canvas.drawLineList(points)
91
canvas.drawPointList(points)
92
canvas.autoRefresh = 1
95
def on_btnColor_mouseClick(self, event):
96
result = dialog.colorDialog(self)
98
self.components.bufOff.foregroundColor = result.color
99
event.target.backgroundColor = result.color
102
def on_select(self, event):
103
name = event.target.name
104
# only process Sliders
105
if name.startswith('sld'):
106
labelName = 'stc' + name[3:]
107
self.components[labelName].text = self.sliderLabels[labelName] + \
108
' ' + str(event.target.value)
111
def on_chkDarkCanvas_mouseClick(self, event):
112
if event.target.checked:
113
self.components.bufOff.backgroundColor = 'black'
115
self.components.bufOff.backgroundColor = 'white'
118
def setSliderLabels(self):
119
comp = self.components
120
for key in self.sliderLabels:
121
sliderName = 'sld' + key[3:]
122
comp[key].text = self.sliderLabels[key] + ' ' + str(comp[sliderName].value)
124
def on_btnRandom_mouseClick(self, event):
125
comp = self.components
126
comp.sldFixedCircleRadius.value = random.randint(1, 100)
127
comp.sldMovingCircleRadius.value = random.randint(-50, 50)
128
comp.sldMovingCircleOffset.value = random.randint(1, 100)
129
self.setSliderLabels()
133
wildcard = "All files (*.*)|*.*"
134
result = dialog.openFileDialog(None, "Import which file?", '', '', wildcard)
136
path = result.paths[0]
137
os.chdir(os.path.dirname(path))
141
filename = os.path.splitext(os.path.basename(path))[0]
142
if filename.startswith('spiro'):
143
items = filename[5:].split('_')
144
comp = self.components
145
comp.sldFixedCircleRadius.value = int(items[0])
146
comp.sldMovingCircleRadius.value = int(items[1])
147
comp.sldMovingCircleOffset.value = int(items[2])
148
comp.btnColor.backgroundColor = eval(items[3])
149
comp.chkDarkCanvas.checked = int(items[4])
151
comp.radDrawingStyle.stringSelection = 'Lines'
153
comp.radDrawingStyle.stringSelection = 'Points'
154
comp.sldRevolutionsInRadians.value = int(items[6])
155
self.setSliderLabels()
157
bmp = graphic.Bitmap(self.filename)
158
self.components.bufOff.drawBitmap(bmp, (0, 0))
162
def on_menuFileOpen_select(self, event):
165
def on_menuFileSaveAs_select(self, event):
166
if self.filename is None:
170
path, filename = os.path.split(self.filename)
172
comp = self.components
173
style = comp.radDrawingStyle.stringSelection[0]
174
filename = 'spiro' + str(comp.sldFixedCircleRadius.value) + '_' + \
175
str(comp.sldMovingCircleRadius.value) + '_' + \
176
str(comp.sldMovingCircleOffset.value) + '_' + \
177
str(comp.btnColor.backgroundColor) + '_' + \
178
str(comp.chkDarkCanvas.checked) + '_' + \
180
str(comp.sldRevolutionsInRadians.value) + \
183
wildcard = "All files (*.*)|*.*"
184
result = dialog.saveFileDialog(None, "Save As", path, filename, wildcard)
186
path = result.paths[0]
187
fileType = graphic.bitmapType(path)
189
bmp = self.components.bufOff.getBitmap()
190
bmp.SaveFile(path, fileType)
197
def on_menuEditCopy_select(self, event):
198
clipboard.setClipboard(self.components.bufOff.getBitmap())
200
def on_menuEditPaste_select(self, event):
201
bmp = clipboard.getClipboard()
202
if isinstance(bmp, wx.Bitmap):
203
self.components.bufOff.drawBitmap(bmp)
205
def on_editClear_command(self, event):
206
self.components.bufOff.clear()
209
if __name__ == '__main__':
210
app = model.Application(Spirograph)