Merge lp:~grantbow/python-snippets/pyturtle into lp:~jonobacon/python-snippets/trunk
- pyturtle
- Merge into trunk
Proposed by
Grant Bowman
Status: | Merged |
---|---|
Merged at revision: | not available |
Proposed branch: | lp:~grantbow/python-snippets/pyturtle |
Merge into: | lp:~jonobacon/python-snippets/trunk |
Diff against target: |
1630 lines (+1536/-0) 17 files modified
CATEGORIES (+1/-0) LICENSES (+1/-0) pyturtle/tdemo_I_dontlike_tiltdemo.py (+68/-0) pyturtle/tdemo_bytedesign.py (+172/-0) pyturtle/tdemo_chaos.py (+70/-0) pyturtle/tdemo_clock.py (+141/-0) pyturtle/tdemo_colormixer.py (+67/-0) pyturtle/tdemo_fractalcurves.py (+147/-0) pyturtle/tdemo_lindenmayer_indian.py (+129/-0) pyturtle/tdemo_minimal_hanoi.py (+85/-0) pyturtle/tdemo_paint.py (+59/-0) pyturtle/tdemo_peace.py (+75/-0) pyturtle/tdemo_penrose.py (+191/-0) pyturtle/tdemo_planet_and_moon.py (+123/-0) pyturtle/tdemo_tree.py (+73/-0) pyturtle/tdemo_wikipedia.py (+75/-0) pyturtle/tdemo_yinyang.py (+59/-0) |
To merge this branch: | bzr merge lp:~grantbow/python-snippets/pyturtle |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Jono Bacon | Pending | ||
Review via email: mp+23740@code.launchpad.net |
Commit message
Description of the change
I was pleasantly surprised to find these work with only headers being added! I will delete the project if this branch is acceptable.
I would prefer to see a new panel open instead of a box pop over with the graphics but I think these are good enough to publish now. I hope you agree.
To post a comment you must log in.
Revision history for this message
Jono Bacon (jonobacon) wrote : | # |
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'CATEGORIES' |
2 | --- CATEGORIES 2010-04-10 20:36:15 +0000 |
3 | +++ CATEGORIES 2010-04-20 04:21:15 +0000 |
4 | @@ -34,6 +34,7 @@ |
5 | PyGTKSourceView PyGTKSourceView widget and framework examples. |
6 | Python Core Examples that demonstrate core features in the Python language. |
7 | Python VTE Snippets using the VTE widget for GTK. |
8 | + PyTurtle Turtle Python module examples. |
9 | PyKDE4 KDE Python bindings. |
10 | PyQt4 PyQt4 widget examples |
11 | Patterns Design pattern examples. |
12 | |
13 | === modified file 'LICENSES' |
14 | --- LICENSES 2010-01-08 21:33:54 +0000 |
15 | +++ LICENSES 2010-04-20 04:21:15 +0000 |
16 | @@ -8,3 +8,4 @@ |
17 | GPL3 |
18 | BSD |
19 | MIT |
20 | + PSF |
21 | |
22 | === added directory 'pyturtle' |
23 | === added file 'pyturtle/tdemo_I_dontlike_tiltdemo.py' |
24 | --- pyturtle/tdemo_I_dontlike_tiltdemo.py 1970-01-01 00:00:00 +0000 |
25 | +++ pyturtle/tdemo_I_dontlike_tiltdemo.py 2010-04-20 04:21:15 +0000 |
26 | @@ -0,0 +1,68 @@ |
27 | +#!/usr/bin/env python |
28 | +# |
29 | +# [SNIPPET_NAME: Elipses] |
30 | +# [SNIPPET_CATEGORIES: PyTurtle] |
31 | +# [SNIPPET_DESCRIPTION: Use turtle to draw elipses.] |
32 | +# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html] |
33 | +# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>] |
34 | +# [SNIPPET_LICENSE: PSF] |
35 | +# Code authorship from http://python.org/download/releases/2.6.4/ |
36 | + |
37 | + |
38 | +""" turtle-example-suite: |
39 | + |
40 | + tdemo-I_dont_like_tiltdemo.py |
41 | + |
42 | +Demostrates |
43 | + (a) use of a tilted ellipse as |
44 | + turtle shape |
45 | + (b) stamping that shape |
46 | + |
47 | +We can remove it, if you don't like it. |
48 | + Without using reset() ;-) |
49 | + --------------------------------------- |
50 | +""" |
51 | +from turtle import * |
52 | +import time |
53 | + |
54 | +def main(): |
55 | + reset() |
56 | + shape("circle") |
57 | + resizemode("user") |
58 | + |
59 | + pu(); bk(24*18/6.283); rt(90); pd() |
60 | + tilt(45) |
61 | + |
62 | + pu() |
63 | + |
64 | + turtlesize(16,10,5) |
65 | + color("red", "violet") |
66 | + for i in range(18): |
67 | + fd(24) |
68 | + lt(20) |
69 | + stamp() |
70 | + color("red", "") |
71 | + for i in range(18): |
72 | + fd(24) |
73 | + lt(20) |
74 | + stamp() |
75 | + |
76 | + tilt(-15) |
77 | + turtlesize(3, 1, 4) |
78 | + color("blue", "yellow") |
79 | + for i in range(17): |
80 | + fd(24) |
81 | + lt(20) |
82 | + if i%2 == 0: |
83 | + stamp() |
84 | + time.sleep(1) |
85 | + while undobufferentries(): |
86 | + undo() |
87 | + ht() |
88 | + write("OK, OVER!", align="center", font=("Courier", 18, "bold")) |
89 | + return "Done!" |
90 | + |
91 | +if __name__=="__main__": |
92 | + msg = main() |
93 | + print msg |
94 | + mainloop() |
95 | |
96 | === added file 'pyturtle/tdemo_bytedesign.py' |
97 | --- pyturtle/tdemo_bytedesign.py 1970-01-01 00:00:00 +0000 |
98 | +++ pyturtle/tdemo_bytedesign.py 2010-04-20 04:21:15 +0000 |
99 | @@ -0,0 +1,172 @@ |
100 | +#!/usr/bin/env python |
101 | +# |
102 | +# [SNIPPET_NAME: Byte Design] |
103 | +# [SNIPPET_CATEGORIES: PyTurtle] |
104 | +# [SNIPPET_DESCRIPTION: Use turtle to draw a design.] |
105 | +# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html] |
106 | +# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>] |
107 | +# [SNIPPET_LICENSE: PSF] |
108 | +# Code authorship from http://python.org/download/releases/2.6.4/ |
109 | + |
110 | + |
111 | +""" turtle-example-suite: |
112 | + |
113 | + tdemo_bytedesign.py |
114 | + |
115 | +An example adapted from the example-suite |
116 | +of PythonCard's turtle graphcis. |
117 | + |
118 | +It's based on an article in BYTE magazine |
119 | +Problem Solving with Logo: Using Turtle |
120 | +Graphics to Redraw a Design |
121 | +November 1982, p. 118 - 134 |
122 | + |
123 | +------------------------------------------- |
124 | + |
125 | +Due to the statement |
126 | + |
127 | +t.delay(0) |
128 | + |
129 | +in line 152, which sets the animation delay |
130 | +to 0, this animation runs in "line per line" |
131 | +mode as fast as possible. |
132 | +""" |
133 | + |
134 | +import math |
135 | +from turtle import Turtle, mainloop |
136 | +from time import clock |
137 | + |
138 | +# wrapper for any additional drawing routines |
139 | +# that need to know about each other |
140 | +class Designer(Turtle): |
141 | + |
142 | + def design(self, homePos, scale): |
143 | + self.up() |
144 | + for i in range(5): |
145 | + self.forward(64.65 * scale) |
146 | + self.down() |
147 | + self.wheel(self.position(), scale) |
148 | + self.up() |
149 | + self.backward(64.65 * scale) |
150 | + self.right(72) |
151 | + self.up() |
152 | + self.goto(homePos) |
153 | + self.right(36) |
154 | + self.forward(24.5 * scale) |
155 | + self.right(198) |
156 | + self.down() |
157 | + self.centerpiece(46 * scale, 143.4, scale) |
158 | + self.tracer(True) |
159 | + |
160 | + def wheel(self, initpos, scale): |
161 | + self.right(54) |
162 | + for i in range(4): |
163 | + self.pentpiece(initpos, scale) |
164 | + self.down() |
165 | + self.left(36) |
166 | + for i in range(5): |
167 | + self.tripiece(initpos, scale) |
168 | + self.left(36) |
169 | + for i in range(5): |
170 | + self.down() |
171 | + self.right(72) |
172 | + self.forward(28 * scale) |
173 | + self.up() |
174 | + self.backward(28 * scale) |
175 | + self.left(54) |
176 | + self.getscreen().update() |
177 | + |
178 | + def tripiece(self, initpos, scale): |
179 | + oldh = self.heading() |
180 | + self.down() |
181 | + self.backward(2.5 * scale) |
182 | + self.tripolyr(31.5 * scale, scale) |
183 | + self.up() |
184 | + self.goto(initpos) |
185 | + self.setheading(oldh) |
186 | + self.down() |
187 | + self.backward(2.5 * scale) |
188 | + self.tripolyl(31.5 * scale, scale) |
189 | + self.up() |
190 | + self.goto(initpos) |
191 | + self.setheading(oldh) |
192 | + self.left(72) |
193 | + self.getscreen().update() |
194 | + |
195 | + def pentpiece(self, initpos, scale): |
196 | + oldh = self.heading() |
197 | + self.up() |
198 | + self.forward(29 * scale) |
199 | + self.down() |
200 | + for i in range(5): |
201 | + self.forward(18 * scale) |
202 | + self.right(72) |
203 | + self.pentr(18 * scale, 75, scale) |
204 | + self.up() |
205 | + self.goto(initpos) |
206 | + self.setheading(oldh) |
207 | + self.forward(29 * scale) |
208 | + self.down() |
209 | + for i in range(5): |
210 | + self.forward(18 * scale) |
211 | + self.right(72) |
212 | + self.pentl(18 * scale, 75, scale) |
213 | + self.up() |
214 | + self.goto(initpos) |
215 | + self.setheading(oldh) |
216 | + self.left(72) |
217 | + self.getscreen().update() |
218 | + |
219 | + def pentl(self, side, ang, scale): |
220 | + if side < (2 * scale): return |
221 | + self.forward(side) |
222 | + self.left(ang) |
223 | + self.pentl(side - (.38 * scale), ang, scale) |
224 | + |
225 | + def pentr(self, side, ang, scale): |
226 | + if side < (2 * scale): return |
227 | + self.forward(side) |
228 | + self.right(ang) |
229 | + self.pentr(side - (.38 * scale), ang, scale) |
230 | + |
231 | + def tripolyr(self, side, scale): |
232 | + if side < (4 * scale): return |
233 | + self.forward(side) |
234 | + self.right(111) |
235 | + self.forward(side / 1.78) |
236 | + self.right(111) |
237 | + self.forward(side / 1.3) |
238 | + self.right(146) |
239 | + self.tripolyr(side * .75, scale) |
240 | + |
241 | + def tripolyl(self, side, scale): |
242 | + if side < (4 * scale): return |
243 | + self.forward(side) |
244 | + self.left(111) |
245 | + self.forward(side / 1.78) |
246 | + self.left(111) |
247 | + self.forward(side / 1.3) |
248 | + self.left(146) |
249 | + self.tripolyl(side * .75, scale) |
250 | + |
251 | + def centerpiece(self, s, a, scale): |
252 | + self.forward(s); self.left(a) |
253 | + if s < (7.5 * scale): |
254 | + return |
255 | + self.centerpiece(s - (1.2 * scale), a, scale) |
256 | + |
257 | +def main(): |
258 | + t = Designer() |
259 | + t.speed(0) |
260 | + t.hideturtle() |
261 | + t.getscreen().delay(0) |
262 | + t.tracer(0) |
263 | + at = clock() |
264 | + t.design(t.position(), 2) |
265 | + et = clock() |
266 | + return "runtime: %.2f sec." % (et-at) |
267 | + |
268 | +if __name__ == '__main__': |
269 | + msg = main() |
270 | + print msg |
271 | + mainloop() |
272 | |
273 | === added file 'pyturtle/tdemo_chaos.py' |
274 | --- pyturtle/tdemo_chaos.py 1970-01-01 00:00:00 +0000 |
275 | +++ pyturtle/tdemo_chaos.py 2010-04-20 04:21:15 +0000 |
276 | @@ -0,0 +1,70 @@ |
277 | +#!/usr/bin/env python |
278 | +# |
279 | +# [SNIPPET_NAME: Chaos] |
280 | +# [SNIPPET_CATEGORIES: PyTurtle] |
281 | +# [SNIPPET_DESCRIPTION: Use turtle to graph chaos.] |
282 | +# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html] |
283 | +# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>] |
284 | +# [SNIPPET_LICENSE: PSF] |
285 | +# Code authorship from http://python.org/download/releases/2.6.4/ |
286 | + |
287 | + |
288 | +# File: tdemo_chaos.py |
289 | +# Author: Gregor Lingl |
290 | +# Date: 2009-06-24 |
291 | + |
292 | +# A demonstration of chaos |
293 | + |
294 | +from turtle import * |
295 | + |
296 | +N = 80 |
297 | + |
298 | +def f(x): |
299 | + return 3.9*x*(1-x) |
300 | + |
301 | +def g(x): |
302 | + return 3.9*(x-x**2) |
303 | + |
304 | +def h(x): |
305 | + return 3.9*x-3.9*x*x |
306 | + |
307 | +def jumpto(x, y): |
308 | + penup(); goto(x,y) |
309 | + |
310 | +def line(x1, y1, x2, y2): |
311 | + jumpto(x1, y1) |
312 | + pendown() |
313 | + goto(x2, y2) |
314 | + |
315 | +def coosys(): |
316 | + line(-1, 0, N+1, 0) |
317 | + line(0, -0.1, 0, 1.1) |
318 | + |
319 | +def plot(fun, start, colour): |
320 | + pencolor(colour) |
321 | + x = start |
322 | + jumpto(0, x) |
323 | + pendown() |
324 | + dot(5) |
325 | + for i in range(N): |
326 | + x=fun(x) |
327 | + goto(i+1,x) |
328 | + dot(5) |
329 | + |
330 | +def main(): |
331 | + reset() |
332 | + setworldcoordinates(-1.0,-0.1, N+1, 1.1) |
333 | + speed(0) |
334 | + hideturtle() |
335 | + coosys() |
336 | + plot(f, 0.35, "blue") |
337 | + plot(g, 0.35, "green") |
338 | + plot(h, 0.35, "red") |
339 | + # Now zoom in: |
340 | + for s in range(100): |
341 | + setworldcoordinates(0.5*s,-0.1, N+1, 1.1) |
342 | + return "Done!" |
343 | + |
344 | +if __name__ == "__main__": |
345 | + main() |
346 | + mainloop() |
347 | |
348 | === added file 'pyturtle/tdemo_clock.py' |
349 | --- pyturtle/tdemo_clock.py 1970-01-01 00:00:00 +0000 |
350 | +++ pyturtle/tdemo_clock.py 2010-04-20 04:21:15 +0000 |
351 | @@ -0,0 +1,141 @@ |
352 | +#!/usr/bin/env python |
353 | +# |
354 | +# [SNIPPET_NAME: tClock] |
355 | +# [SNIPPET_CATEGORIES: PyTurtle] |
356 | +# [SNIPPET_DESCRIPTION: Use turtle to draw a clock.] |
357 | +# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html] |
358 | +# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>] |
359 | +# [SNIPPET_LICENSE: PSF] |
360 | +# Code authorship from http://python.org/download/releases/2.6.4/ |
361 | + |
362 | + |
363 | +""" turtle-example-suite: |
364 | + |
365 | + tdemo_clock.py |
366 | + |
367 | +Enhanced clock-program, showing date |
368 | +and time |
369 | + ------------------------------------ |
370 | + Press STOP to exit the program! |
371 | + ------------------------------------ |
372 | +""" |
373 | +from turtle import * |
374 | +from datetime import datetime |
375 | + |
376 | +mode("logo") |
377 | + |
378 | +def jump(distanz, winkel=0): |
379 | + penup() |
380 | + right(winkel) |
381 | + forward(distanz) |
382 | + left(winkel) |
383 | + pendown() |
384 | + |
385 | +def hand(laenge, spitze): |
386 | + fd(laenge*1.15) |
387 | + rt(90) |
388 | + fd(spitze/2.0) |
389 | + lt(120) |
390 | + fd(spitze) |
391 | + lt(120) |
392 | + fd(spitze) |
393 | + lt(120) |
394 | + fd(spitze/2.0) |
395 | + |
396 | +def make_hand_shape(name, laenge, spitze): |
397 | + reset() |
398 | + jump(-laenge*0.15) |
399 | + begin_poly() |
400 | + hand(laenge, spitze) |
401 | + end_poly() |
402 | + hand_form = get_poly() |
403 | + register_shape(name, hand_form) |
404 | + |
405 | + |
406 | +def clockface(radius): |
407 | + reset() |
408 | + pensize(7) |
409 | + for i in range(60): |
410 | + jump(radius) |
411 | + if i % 5 == 0: |
412 | + fd(25) |
413 | + jump(-radius-25) |
414 | + else: |
415 | + dot(3) |
416 | + jump(-radius) |
417 | + rt(6) |
418 | + |
419 | +def setup(): |
420 | + global second_hand, minute_hand, hour_hand, writer |
421 | + mode("logo") |
422 | + make_hand_shape("second_hand", 125, 25) |
423 | + make_hand_shape("minute_hand", 130, 25) |
424 | + make_hand_shape("hour_hand", 90, 25) |
425 | + clockface(160) |
426 | + second_hand = Turtle() |
427 | + second_hand.shape("second_hand") |
428 | + second_hand.color("gray20", "gray80") |
429 | + minute_hand = Turtle() |
430 | + minute_hand.shape("minute_hand") |
431 | + minute_hand.color("blue1", "red1") |
432 | + hour_hand = Turtle() |
433 | + hour_hand.shape("hour_hand") |
434 | + hour_hand.color("blue3", "red3") |
435 | + for hand in second_hand, minute_hand, hour_hand: |
436 | + hand.resizemode("user") |
437 | + hand.shapesize(1, 1, 3) |
438 | + hand.speed(0) |
439 | + ht() |
440 | + writer = Turtle() |
441 | + #writer.mode("logo") |
442 | + writer.ht() |
443 | + writer.pu() |
444 | + writer.bk(85) |
445 | + |
446 | + |
447 | +def wochentag(t): |
448 | + wochentag = ["Monday", "Tuesday", "Wednesday", |
449 | + "Thursday", "Friday", "Saturday", "Sunday"] |
450 | + return wochentag[t.weekday()] |
451 | + |
452 | +def datum(z): |
453 | + monat = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "June", |
454 | + "July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."] |
455 | + j = z.year |
456 | + m = monat[z.month - 1] |
457 | + t = z.day |
458 | + return "%s %d %d" % (m, t, j) |
459 | + |
460 | +def tick(): |
461 | + t = datetime.today() |
462 | + sekunde = t.second + t.microsecond*0.000001 |
463 | + minute = t.minute + sekunde/60.0 |
464 | + stunde = t.hour + minute/60.0 |
465 | + tracer(False) |
466 | + writer.clear() |
467 | + writer.home() |
468 | + writer.forward(65) |
469 | + writer.write(wochentag(t), |
470 | + align="center", font=("Courier", 14, "bold")) |
471 | + writer.back(150) |
472 | + writer.write(datum(t), |
473 | + align="center", font=("Courier", 14, "bold")) |
474 | + writer.forward(85) |
475 | + tracer(True) |
476 | + second_hand.setheading(6*sekunde) |
477 | + minute_hand.setheading(6*minute) |
478 | + hour_hand.setheading(30*stunde) |
479 | + tracer(True) |
480 | + ontimer(tick, 100) |
481 | + |
482 | +def main(): |
483 | + tracer(False) |
484 | + setup() |
485 | + tracer(True) |
486 | + tick() |
487 | + return "EVENTLOOP" |
488 | + |
489 | +if __name__ == "__main__": |
490 | + msg = main() |
491 | + print msg |
492 | + mainloop() |
493 | |
494 | === added file 'pyturtle/tdemo_colormixer.py' |
495 | --- pyturtle/tdemo_colormixer.py 1970-01-01 00:00:00 +0000 |
496 | +++ pyturtle/tdemo_colormixer.py 2010-04-20 04:21:15 +0000 |
497 | @@ -0,0 +1,67 @@ |
498 | +#!/usr/bin/env python |
499 | +# |
500 | +# [SNIPPET_NAME: Color Mixer] |
501 | +# [SNIPPET_CATEGORIES: PyTurtle] |
502 | +# [SNIPPET_DESCRIPTION: Use three sliders to change the color of the screen.] |
503 | +# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html] |
504 | +# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>] |
505 | +# [SNIPPET_LICENSE: PSF] |
506 | +# Code authorship from http://python.org/download/releases/2.6.4/ |
507 | + |
508 | + |
509 | +from turtle import Screen, Turtle, mainloop |
510 | + |
511 | +class ColorTurtle(Turtle): |
512 | + |
513 | + def __init__(self, x, y): |
514 | + Turtle.__init__(self) |
515 | + self.shape("turtle") |
516 | + self.resizemode("user") |
517 | + self.shapesize(3,3,5) |
518 | + self.pensize(10) |
519 | + self._color = [0,0,0] |
520 | + self.x = x |
521 | + self._color[x] = y |
522 | + self.color(self._color) |
523 | + self.speed(0) |
524 | + self.left(90) |
525 | + self.pu() |
526 | + self.goto(x,0) |
527 | + self.pd() |
528 | + self.sety(1) |
529 | + self.pu() |
530 | + self.sety(y) |
531 | + self.pencolor("gray25") |
532 | + self.ondrag(self.shift) |
533 | + |
534 | + def shift(self, x, y): |
535 | + self.sety(max(0,min(y,1))) |
536 | + self._color[self.x] = self.ycor() |
537 | + self.fillcolor(self._color) |
538 | + setbgcolor() |
539 | + |
540 | +def setbgcolor(): |
541 | + screen.bgcolor(red.ycor(), green.ycor(), blue.ycor()) |
542 | + |
543 | +def main(): |
544 | + global screen, red, green, blue |
545 | + screen = Screen() |
546 | + screen.delay(0) |
547 | + screen.setworldcoordinates(-1, -0.3, 3, 1.3) |
548 | + |
549 | + red = ColorTurtle(0, .5) |
550 | + green = ColorTurtle(1, .5) |
551 | + blue = ColorTurtle(2, .5) |
552 | + setbgcolor() |
553 | + |
554 | + writer = Turtle() |
555 | + writer.ht() |
556 | + writer.pu() |
557 | + writer.goto(1,1.15) |
558 | + writer.write("DRAG!",align="center",font=("Arial",30,("bold","italic"))) |
559 | + return "EVENTLOOP" |
560 | + |
561 | +if __name__ == "__main__": |
562 | + msg = main() |
563 | + print msg |
564 | + mainloop() |
565 | |
566 | === added file 'pyturtle/tdemo_fractalcurves.py' |
567 | --- pyturtle/tdemo_fractalcurves.py 1970-01-01 00:00:00 +0000 |
568 | +++ pyturtle/tdemo_fractalcurves.py 2010-04-20 04:21:15 +0000 |
569 | @@ -0,0 +1,147 @@ |
570 | +#!/usr/bin/env python |
571 | +# |
572 | +# [SNIPPET_NAME: Fractal Curves] |
573 | +# [SNIPPET_CATEGORIES: PyTurtle] |
574 | +# [SNIPPET_DESCRIPTION: Use turtle to draw two fractals.] |
575 | +# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html] |
576 | +# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>] |
577 | +# [SNIPPET_LICENSE: PSF] |
578 | +# Code authorship from http://python.org/download/releases/2.6.4/ |
579 | + |
580 | + |
581 | +""" turtle-example-suite: |
582 | + |
583 | + tdemo_fractalCurves.py |
584 | + |
585 | +This program draws two fractal-curve-designs: |
586 | +(1) A hilbert curve (in a box) |
587 | +(2) A combination of Koch-curves. |
588 | + |
589 | +The CurvesTurtle class and the fractal-curve- |
590 | +methods are taken from the PythonCard example |
591 | +scripts for turtle-graphics. |
592 | +""" |
593 | +from turtle import * |
594 | +from time import sleep, clock |
595 | + |
596 | +class CurvesTurtle(Pen): |
597 | + # example derived from |
598 | + # Turtle Geometry: The Computer as a Medium for Exploring Mathematics |
599 | + # by Harold Abelson and Andrea diSessa |
600 | + # p. 96-98 |
601 | + def hilbert(self, size, level, parity): |
602 | + if level == 0: |
603 | + return |
604 | + # rotate and draw first subcurve with opposite parity to big curve |
605 | + self.left(parity * 90) |
606 | + self.hilbert(size, level - 1, -parity) |
607 | + # interface to and draw second subcurve with same parity as big curve |
608 | + self.forward(size) |
609 | + self.right(parity * 90) |
610 | + self.hilbert(size, level - 1, parity) |
611 | + # third subcurve |
612 | + self.forward(size) |
613 | + self.hilbert(size, level - 1, parity) |
614 | + # fourth subcurve |
615 | + self.right(parity * 90) |
616 | + self.forward(size) |
617 | + self.hilbert(size, level - 1, -parity) |
618 | + # a final turn is needed to make the turtle |
619 | + # end up facing outward from the large square |
620 | + self.left(parity * 90) |
621 | + |
622 | + # Visual Modeling with Logo: A Structural Approach to Seeing |
623 | + # by James Clayson |
624 | + # Koch curve, after Helge von Koch who introduced this geometric figure in 1904 |
625 | + # p. 146 |
626 | + def fractalgon(self, n, rad, lev, dir): |
627 | + import math |
628 | + |
629 | + # if dir = 1 turn outward |
630 | + # if dir = -1 turn inward |
631 | + edge = 2 * rad * math.sin(math.pi / n) |
632 | + self.pu() |
633 | + self.fd(rad) |
634 | + self.pd() |
635 | + self.rt(180 - (90 * (n - 2) / n)) |
636 | + for i in range(n): |
637 | + self.fractal(edge, lev, dir) |
638 | + self.rt(360 / n) |
639 | + self.lt(180 - (90 * (n - 2) / n)) |
640 | + self.pu() |
641 | + self.bk(rad) |
642 | + self.pd() |
643 | + |
644 | + # p. 146 |
645 | + def fractal(self, dist, depth, dir): |
646 | + if depth < 1: |
647 | + self.fd(dist) |
648 | + return |
649 | + self.fractal(dist / 3, depth - 1, dir) |
650 | + self.lt(60 * dir) |
651 | + self.fractal(dist / 3, depth - 1, dir) |
652 | + self.rt(120 * dir) |
653 | + self.fractal(dist / 3, depth - 1, dir) |
654 | + self.lt(60 * dir) |
655 | + self.fractal(dist / 3, depth - 1, dir) |
656 | + |
657 | +def main(): |
658 | + ft = CurvesTurtle() |
659 | + |
660 | + ft.reset() |
661 | + ft.speed(0) |
662 | + ft.ht() |
663 | + ft.tracer(1,0) |
664 | + ft.pu() |
665 | + |
666 | + size = 6 |
667 | + ft.setpos(-33*size, -32*size) |
668 | + ft.pd() |
669 | + |
670 | + ta=clock() |
671 | + ft.fillcolor("red") |
672 | + ft.fill(True) |
673 | + ft.fd(size) |
674 | + |
675 | + ft.hilbert(size, 6, 1) |
676 | + |
677 | + # frame |
678 | + ft.fd(size) |
679 | + for i in range(3): |
680 | + ft.lt(90) |
681 | + ft.fd(size*(64+i%2)) |
682 | + ft.pu() |
683 | + for i in range(2): |
684 | + ft.fd(size) |
685 | + ft.rt(90) |
686 | + ft.pd() |
687 | + for i in range(4): |
688 | + ft.fd(size*(66+i%2)) |
689 | + ft.rt(90) |
690 | + ft.fill(False) |
691 | + tb=clock() |
692 | + res = "Hilbert: %.2fsec. " % (tb-ta) |
693 | + |
694 | + sleep(3) |
695 | + |
696 | + ft.reset() |
697 | + ft.speed(0) |
698 | + ft.ht() |
699 | + ft.tracer(1,0) |
700 | + |
701 | + ta=clock() |
702 | + ft.color("black", "blue") |
703 | + ft.fill(True) |
704 | + ft.fractalgon(3, 250, 4, 1) |
705 | + ft.fill(True) |
706 | + ft.color("red") |
707 | + ft.fractalgon(3, 200, 4, -1) |
708 | + ft.fill(False) |
709 | + tb=clock() |
710 | + res += "Koch: %.2fsec." % (tb-ta) |
711 | + return res |
712 | + |
713 | +if __name__ == '__main__': |
714 | + msg = main() |
715 | + print msg |
716 | + mainloop() |
717 | |
718 | === added file 'pyturtle/tdemo_lindenmayer_indian.py' |
719 | --- pyturtle/tdemo_lindenmayer_indian.py 1970-01-01 00:00:00 +0000 |
720 | +++ pyturtle/tdemo_lindenmayer_indian.py 2010-04-20 04:21:15 +0000 |
721 | @@ -0,0 +1,129 @@ |
722 | +#!/usr/bin/env python |
723 | +# |
724 | +# [SNIPPET_NAME: Lindenmayer] |
725 | +# [SNIPPET_CATEGORIES: PyTurtle] |
726 | +# [SNIPPET_DESCRIPTION: Use turtle to draw two Lindenmayer systems.] |
727 | +# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html] |
728 | +# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>] |
729 | +# [SNIPPET_LICENSE: PSF] |
730 | +# Code authorship from http://python.org/download/releases/2.6.4/ |
731 | + |
732 | + |
733 | +""" turtle-example-suite: |
734 | + |
735 | + xtx_lindenmayer_indian.py |
736 | + |
737 | +Each morning women in Tamil Nadu, in southern |
738 | +India, place designs, created by using rice |
739 | +flour and known as kolam on the thresholds of |
740 | +their homes. |
741 | + |
742 | +These can be described by Lindenmayer systems, |
743 | +which can easily be implemented with turtle |
744 | +graphics and Python. |
745 | + |
746 | +Two examples are shown here: |
747 | +(1) the snake kolam |
748 | +(2) anklets of Krishna |
749 | + |
750 | +Taken from Marcia Ascher: Mathematics |
751 | +Elsewhere, An Exploration of Ideas Across |
752 | +Cultures |
753 | + |
754 | +""" |
755 | +################################ |
756 | +# Mini Lindenmayer tool |
757 | +############################### |
758 | + |
759 | +from turtle import * |
760 | + |
761 | +def replace( seq, replacementRules, n ): |
762 | + for i in range(n): |
763 | + newseq = "" |
764 | + for element in seq: |
765 | + newseq = newseq + replacementRules.get(element,element) |
766 | + seq = newseq |
767 | + return seq |
768 | + |
769 | +def draw( commands, rules ): |
770 | + for b in commands: |
771 | + try: |
772 | + rules[b]() |
773 | + except TypeError: |
774 | + try: |
775 | + draw(rules[b], rules) |
776 | + except: |
777 | + pass |
778 | + |
779 | + |
780 | +def main(): |
781 | + ################################ |
782 | + # Example 1: Snake kolam |
783 | + ################################ |
784 | + |
785 | + |
786 | + def r(): |
787 | + right(45) |
788 | + |
789 | + def l(): |
790 | + left(45) |
791 | + |
792 | + def f(): |
793 | + forward(7.5) |
794 | + |
795 | + snake_rules = {"-":r, "+":l, "f":f, "b":"f+f+f--f--f+f+f"} |
796 | + snake_replacementRules = {"b": "b+f+b--f--b+f+b"} |
797 | + snake_start = "b--f--b--f" |
798 | + |
799 | + drawing = replace(snake_start, snake_replacementRules, 3) |
800 | + |
801 | + reset() |
802 | + speed(3) |
803 | + tracer(1,0) |
804 | + ht() |
805 | + up() |
806 | + backward(195) |
807 | + down() |
808 | + draw(drawing, snake_rules) |
809 | + |
810 | + from time import sleep |
811 | + sleep(3) |
812 | + |
813 | + ################################ |
814 | + # Example 2: Anklets of Krishna |
815 | + ################################ |
816 | + |
817 | + def A(): |
818 | + color("red") |
819 | + circle(10,90) |
820 | + |
821 | + def B(): |
822 | + from math import sqrt |
823 | + color("black") |
824 | + l = 5/sqrt(2) |
825 | + forward(l) |
826 | + circle(l, 270) |
827 | + forward(l) |
828 | + |
829 | + def F(): |
830 | + color("green") |
831 | + forward(10) |
832 | + |
833 | + krishna_rules = {"a":A, "b":B, "f":F} |
834 | + krishna_replacementRules = {"a" : "afbfa", "b" : "afbfbfbfa" } |
835 | + krishna_start = "fbfbfbfb" |
836 | + |
837 | + reset() |
838 | + speed(0) |
839 | + tracer(3,0) |
840 | + ht() |
841 | + left(45) |
842 | + drawing = replace(krishna_start, krishna_replacementRules, 3) |
843 | + draw(drawing, krishna_rules) |
844 | + tracer(1) |
845 | + return "Done!" |
846 | + |
847 | +if __name__=='__main__': |
848 | + msg = main() |
849 | + print msg |
850 | + mainloop() |
851 | |
852 | === added file 'pyturtle/tdemo_minimal_hanoi.py' |
853 | --- pyturtle/tdemo_minimal_hanoi.py 1970-01-01 00:00:00 +0000 |
854 | +++ pyturtle/tdemo_minimal_hanoi.py 2010-04-20 04:21:15 +0000 |
855 | @@ -0,0 +1,85 @@ |
856 | +#!/usr/bin/env python |
857 | +# |
858 | +# [SNIPPET_NAME: Hanoi] |
859 | +# [SNIPPET_CATEGORIES: PyTurtle] |
860 | +# [SNIPPET_DESCRIPTION: Turtle plays Towers of Hanoi.] |
861 | +# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html] |
862 | +# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>] |
863 | +# [SNIPPET_LICENSE: PSF] |
864 | +# Code authorship from http://python.org/download/releases/2.6.4/ |
865 | + |
866 | +""" turtle-example-suite: |
867 | + |
868 | + tdemo_minimal_hanoi.py |
869 | + |
870 | +A minimal 'Towers of Hanoi' animation: |
871 | +A tower of 6 discs is transferred from the |
872 | +left to the right peg. |
873 | + |
874 | +An imho quite elegant and concise |
875 | +implementation using a tower class, which |
876 | +is derived from the built-in type list. |
877 | + |
878 | +Discs are turtles with shape "square", but |
879 | +stretched to rectangles by shapesize() |
880 | + --------------------------------------- |
881 | + To exit press STOP button |
882 | + --------------------------------------- |
883 | +""" |
884 | +from turtle import * |
885 | + |
886 | +class Disc(Turtle): |
887 | + def __init__(self, n): |
888 | + Turtle.__init__(self, shape="square", visible=False) |
889 | + self.pu() |
890 | + self.shapesize(1.5, n*1.5, 2) # square-->rectangle |
891 | + self.fillcolor(n/6., 0, 1-n/6.) |
892 | + self.st() |
893 | + |
894 | +class Tower(list): |
895 | + "Hanoi tower, a subclass of built-in type list" |
896 | + def __init__(self, x): |
897 | + "create an empty tower. x is x-position of peg" |
898 | + self.x = x |
899 | + def push(self, d): |
900 | + d.setx(self.x) |
901 | + d.sety(-150+34*len(self)) |
902 | + self.append(d) |
903 | + def pop(self): |
904 | + d = list.pop(self) |
905 | + d.sety(150) |
906 | + return d |
907 | + |
908 | +def hanoi(n, from_, with_, to_): |
909 | + if n > 0: |
910 | + hanoi(n-1, from_, to_, with_) |
911 | + to_.push(from_.pop()) |
912 | + hanoi(n-1, with_, from_, to_) |
913 | + |
914 | +def play(): |
915 | + onkey(None,"space") |
916 | + clear() |
917 | + hanoi(6, t1, t2, t3) |
918 | + write("press STOP button to exit", |
919 | + align="center", font=("Courier", 16, "bold")) |
920 | + |
921 | +def main(): |
922 | + global t1, t2, t3 |
923 | + ht(); penup(); goto(0, -225) # writer turtle |
924 | + t1 = Tower(-250) |
925 | + t2 = Tower(0) |
926 | + t3 = Tower(250) |
927 | + # make tower of 6 discs |
928 | + for i in range(6,0,-1): |
929 | + t1.push(Disc(i)) |
930 | + # prepare spartanic user interface ;-) |
931 | + write("press spacebar to start game", |
932 | + align="center", font=("Courier", 16, "bold")) |
933 | + onkey(play, "space") |
934 | + listen() |
935 | + return "EVENTLOOP" |
936 | + |
937 | +if __name__=="__main__": |
938 | + msg = main() |
939 | + print msg |
940 | + mainloop() |
941 | |
942 | === added file 'pyturtle/tdemo_paint.py' |
943 | --- pyturtle/tdemo_paint.py 1970-01-01 00:00:00 +0000 |
944 | +++ pyturtle/tdemo_paint.py 2010-04-20 04:21:15 +0000 |
945 | @@ -0,0 +1,59 @@ |
946 | +#!/usr/bin/env python |
947 | +# |
948 | +# [SNIPPET_NAME: tPaint] |
949 | +# [SNIPPET_CATEGORIES: PyTurtle] |
950 | +# [SNIPPET_DESCRIPTION: Use turtle to draw lines interactively.] |
951 | +# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html] |
952 | +# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>] |
953 | +# [SNIPPET_LICENSE: PSF] |
954 | +# Code authorship from http://python.org/download/releases/2.6.4/ |
955 | + |
956 | +""" turtle-example-suite: |
957 | + |
958 | + tdemo_paint.py |
959 | + |
960 | +A simple eventdriven paint program |
961 | + |
962 | +- use left mouse button to move turtle |
963 | +- middle mouse button to change color |
964 | +- right mouse button do turn filling on/off |
965 | + ------------------------------------------- |
966 | + Play around by clicking into the canvas |
967 | + using all three mouse buttons. |
968 | + ------------------------------------------- |
969 | + To exit press STOP button |
970 | + ------------------------------------------- |
971 | +""" |
972 | +from turtle import * |
973 | + |
974 | +def switchupdown(x=0, y=0): |
975 | + if pen()["pendown"]: |
976 | + end_fill() |
977 | + up() |
978 | + else: |
979 | + down() |
980 | + begin_fill() |
981 | + |
982 | +def changecolor(x=0, y=0): |
983 | + global colors |
984 | + colors = colors[1:]+colors[:1] |
985 | + color(colors[0]) |
986 | + |
987 | +def main(): |
988 | + global colors |
989 | + shape("circle") |
990 | + resizemode("user") |
991 | + shapesize(.5) |
992 | + width(3) |
993 | + colors=["red", "green", "blue", "yellow"] |
994 | + color(colors[0]) |
995 | + switchupdown() |
996 | + onscreenclick(goto,1) |
997 | + onscreenclick(changecolor,2) |
998 | + onscreenclick(switchupdown,3) |
999 | + return "EVENTLOOP" |
1000 | + |
1001 | +if __name__ == "__main__": |
1002 | + msg = main() |
1003 | + print msg |
1004 | + mainloop() |
1005 | |
1006 | === added file 'pyturtle/tdemo_peace.py' |
1007 | --- pyturtle/tdemo_peace.py 1970-01-01 00:00:00 +0000 |
1008 | +++ pyturtle/tdemo_peace.py 2010-04-20 04:21:15 +0000 |
1009 | @@ -0,0 +1,75 @@ |
1010 | +#!/usr/bin/env python |
1011 | +# |
1012 | +# [SNIPPET_NAME: Peace] |
1013 | +# [SNIPPET_CATEGORIES: PyTurtle] |
1014 | +# [SNIPPET_DESCRIPTION: Use turtle to draw a rainbow and peace sign.] |
1015 | +# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html] |
1016 | +# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>] |
1017 | +# [SNIPPET_LICENSE: PSF] |
1018 | +# Code authorship from http://python.org/download/releases/2.6.4/ |
1019 | + |
1020 | + |
1021 | +""" turtle-example-suite: |
1022 | + |
1023 | + tdemo_peace.py |
1024 | + |
1025 | +A very simple drawing suitable as a beginner's |
1026 | +programming example. |
1027 | + |
1028 | +Uses only commands, which are also available in |
1029 | +old turtle.py. |
1030 | + |
1031 | +Intentionally no variables are used except for the |
1032 | +colorloop: |
1033 | +""" |
1034 | + |
1035 | +from turtle import * |
1036 | + |
1037 | +def main(): |
1038 | + peacecolors = ("red3", "orange", "yellow", |
1039 | + "seagreen4", "orchid4", |
1040 | + "royalblue1", "dodgerblue4") |
1041 | + |
1042 | + reset() |
1043 | + s = Screen() |
1044 | + up() |
1045 | + goto(-320,-195) |
1046 | + width(70) |
1047 | + |
1048 | + for pcolor in peacecolors: |
1049 | + color(pcolor) |
1050 | + down() |
1051 | + forward(640) |
1052 | + up() |
1053 | + backward(640) |
1054 | + left(90) |
1055 | + forward(66) |
1056 | + right(90) |
1057 | + |
1058 | + width(25) |
1059 | + color("white") |
1060 | + goto(0,-170) |
1061 | + down() |
1062 | + |
1063 | + circle(170) |
1064 | + left(90) |
1065 | + forward(340) |
1066 | + up() |
1067 | + left(180) |
1068 | + forward(170) |
1069 | + right(45) |
1070 | + down() |
1071 | + forward(170) |
1072 | + up() |
1073 | + backward(170) |
1074 | + left(90) |
1075 | + down() |
1076 | + forward(170) |
1077 | + up() |
1078 | + |
1079 | + goto(0,300) # vanish if hideturtle() is not available ;-) |
1080 | + return "Done!!" |
1081 | + |
1082 | +if __name__ == "__main__": |
1083 | + main() |
1084 | + mainloop() |
1085 | |
1086 | === added file 'pyturtle/tdemo_penrose.py' |
1087 | --- pyturtle/tdemo_penrose.py 1970-01-01 00:00:00 +0000 |
1088 | +++ pyturtle/tdemo_penrose.py 2010-04-20 04:21:15 +0000 |
1089 | @@ -0,0 +1,191 @@ |
1090 | +#!/usr/bin/env python |
1091 | +# |
1092 | +# [SNIPPET_NAME: Penrose] |
1093 | +# [SNIPPET_CATEGORIES: PyTurtle] |
1094 | +# [SNIPPET_DESCRIPTION: Use turtle to draw two Penrose tilings.] |
1095 | +# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html] |
1096 | +# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>] |
1097 | +# [SNIPPET_LICENSE: PSF] |
1098 | +# Code authorship from http://python.org/download/releases/2.6.4/ |
1099 | + |
1100 | + |
1101 | +""" xturtle-example-suite: |
1102 | + |
1103 | + xtx_kites_and_darts.py |
1104 | + |
1105 | +Constructs two aperiodic penrose-tilings, |
1106 | +consisting of kites and darts, by the method |
1107 | +of inflation in six steps. |
1108 | + |
1109 | +Starting points are the patterns "sun" |
1110 | +consisting of five kites and "star" |
1111 | +consisting of five darts. |
1112 | + |
1113 | +For more information see: |
1114 | + http://en.wikipedia.org/wiki/Penrose_tiling |
1115 | + ------------------------------------------- |
1116 | +""" |
1117 | +from turtle import * |
1118 | +from math import cos, pi |
1119 | +from time import clock, sleep |
1120 | + |
1121 | +f = (5**0.5-1)/2.0 # (sqrt(5)-1)/2 -- golden ratio |
1122 | +d = 2 * cos(3*pi/10) |
1123 | + |
1124 | +def kite(l): |
1125 | + fl = f * l |
1126 | + lt(36) |
1127 | + fd(l) |
1128 | + rt(108) |
1129 | + fd(fl) |
1130 | + rt(36) |
1131 | + fd(fl) |
1132 | + rt(108) |
1133 | + fd(l) |
1134 | + rt(144) |
1135 | + |
1136 | +def dart(l): |
1137 | + fl = f * l |
1138 | + lt(36) |
1139 | + fd(l) |
1140 | + rt(144) |
1141 | + fd(fl) |
1142 | + lt(36) |
1143 | + fd(fl) |
1144 | + rt(144) |
1145 | + fd(l) |
1146 | + rt(144) |
1147 | + |
1148 | +def inflatekite(l, n): |
1149 | + if n == 0: |
1150 | + px, py = pos() |
1151 | + h, x, y = int(heading()), round(px,3), round(py,3) |
1152 | + tiledict[(h,x,y)] = True |
1153 | + return |
1154 | + fl = f * l |
1155 | + lt(36) |
1156 | + inflatedart(fl, n-1) |
1157 | + fd(l) |
1158 | + rt(144) |
1159 | + inflatekite(fl, n-1) |
1160 | + lt(18) |
1161 | + fd(l*d) |
1162 | + rt(162) |
1163 | + inflatekite(fl, n-1) |
1164 | + lt(36) |
1165 | + fd(l) |
1166 | + rt(180) |
1167 | + inflatedart(fl, n-1) |
1168 | + lt(36) |
1169 | + |
1170 | +def inflatedart(l, n): |
1171 | + if n == 0: |
1172 | + px, py = pos() |
1173 | + h, x, y = int(heading()), round(px,3), round(py,3) |
1174 | + tiledict[(h,x,y)] = False |
1175 | + return |
1176 | + fl = f * l |
1177 | + inflatekite(fl, n-1) |
1178 | + lt(36) |
1179 | + fd(l) |
1180 | + rt(180) |
1181 | + inflatedart(fl, n-1) |
1182 | + lt(54) |
1183 | + fd(l*d) |
1184 | + rt(126) |
1185 | + inflatedart(fl, n-1) |
1186 | + fd(l) |
1187 | + rt(144) |
1188 | + |
1189 | +def draw(l, n, th=2): |
1190 | + clear() |
1191 | + l = l * f**n |
1192 | + shapesize(l/100.0, l/100.0, th) |
1193 | + for k in tiledict: |
1194 | + h, x, y = k |
1195 | + setpos(x, y) |
1196 | + setheading(h) |
1197 | + if tiledict[k]: |
1198 | + shape("kite") |
1199 | + color("black", (0, 0.75, 0)) |
1200 | + else: |
1201 | + shape("dart") |
1202 | + color("black", (0.75, 0, 0)) |
1203 | + stamp() |
1204 | + |
1205 | +def sun(l, n): |
1206 | + for i in range(5): |
1207 | + inflatekite(l, n) |
1208 | + lt(72) |
1209 | + |
1210 | +def star(l,n): |
1211 | + for i in range(5): |
1212 | + inflatedart(l, n) |
1213 | + lt(72) |
1214 | + |
1215 | +def makeshapes(): |
1216 | + tracer(0) |
1217 | + begin_poly() |
1218 | + kite(100) |
1219 | + end_poly() |
1220 | + register_shape("kite", get_poly()) |
1221 | + begin_poly() |
1222 | + dart(100) |
1223 | + end_poly() |
1224 | + register_shape("dart", get_poly()) |
1225 | + tracer(1) |
1226 | + |
1227 | +def start(): |
1228 | + reset() |
1229 | + ht() |
1230 | + pu() |
1231 | + makeshapes() |
1232 | + resizemode("user") |
1233 | + |
1234 | +def test(l=200, n=4, fun=sun, startpos=(0,0), th=2): |
1235 | + global tiledict |
1236 | + goto(startpos) |
1237 | + setheading(0) |
1238 | + tiledict = {} |
1239 | + a = clock() |
1240 | + tracer(0) |
1241 | + fun(l, n) |
1242 | + b = clock() |
1243 | + draw(l, n, th) |
1244 | + tracer(1) |
1245 | + c = clock() |
1246 | + print "Calculation: %7.4f s" % (b - a) |
1247 | + print "Drawing: %7.4f s" % (c - b) |
1248 | + print "Together: %7.4f s" % (c - a) |
1249 | + nk = len([x for x in tiledict if tiledict[x]]) |
1250 | + nd = len([x for x in tiledict if not tiledict[x]]) |
1251 | + print "%d kites and %d darts = %d pieces." % (nk, nd, nk+nd) |
1252 | + |
1253 | +def demo(fun=sun): |
1254 | + start() |
1255 | + for i in range(8): |
1256 | + a = clock() |
1257 | + test(300, i, fun) |
1258 | + b = clock() |
1259 | + t = b - a |
1260 | + if t < 2: |
1261 | + sleep(2 - t) |
1262 | + |
1263 | +def main(): |
1264 | + #title("Penrose-tiling with kites and darts.") |
1265 | + mode("logo") |
1266 | + bgcolor(0.3, 0.3, 0) |
1267 | + demo(sun) |
1268 | + sleep(2) |
1269 | + demo(star) |
1270 | + pencolor("black") |
1271 | + goto(0,-200) |
1272 | + pencolor(0.7,0.7,1) |
1273 | + write("Please wait...", |
1274 | + align="center", font=('Arial Black', 36, 'bold')) |
1275 | + test(600, 8, startpos=(70, 117)) |
1276 | + return "Done" |
1277 | + |
1278 | +if __name__ == "__main__": |
1279 | + msg = main() |
1280 | + mainloop() |
1281 | |
1282 | === added file 'pyturtle/tdemo_planet_and_moon.py' |
1283 | --- pyturtle/tdemo_planet_and_moon.py 1970-01-01 00:00:00 +0000 |
1284 | +++ pyturtle/tdemo_planet_and_moon.py 2010-04-20 04:21:15 +0000 |
1285 | @@ -0,0 +1,123 @@ |
1286 | +#!/usr/bin/env python |
1287 | +# |
1288 | +# [SNIPPET_NAME: tGravity] |
1289 | +# [SNIPPET_CATEGORIES: PyTurtle] |
1290 | +# [SNIPPET_DESCRIPTION: Use turtle to animate a moon and planet around a star.] |
1291 | +# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html] |
1292 | +# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>] |
1293 | +# [SNIPPET_LICENSE: PSF] |
1294 | +# Code authorship from http://python.org/download/releases/2.6.4/ |
1295 | + |
1296 | + |
1297 | +""" turtle-example-suite: |
1298 | + |
1299 | + tdemo_planets_and_moon.py |
1300 | + |
1301 | +Gravitational system simulation using the |
1302 | +approximation method from Feynman-lectures, |
1303 | +p.9-8, using turtlegraphics. |
1304 | + |
1305 | +Example: heavy central body, light planet, |
1306 | +very light moon! |
1307 | +Planet has a circular orbit, moon a stable |
1308 | +orbit around the planet. |
1309 | + |
1310 | +You can hold the movement temporarily by pressing |
1311 | +the left mouse button with mouse over the |
1312 | +scrollbar of the canvas. |
1313 | + |
1314 | +""" |
1315 | +from turtle import Shape, Turtle, mainloop, Vec2D as Vec |
1316 | +from time import sleep |
1317 | + |
1318 | +G = 8 |
1319 | + |
1320 | +class GravSys(object): |
1321 | + def __init__(self): |
1322 | + self.planets = [] |
1323 | + self.t = 0 |
1324 | + self.dt = 0.01 |
1325 | + def init(self): |
1326 | + for p in self.planets: |
1327 | + p.init() |
1328 | + def start(self): |
1329 | + for i in range(10000): |
1330 | + self.t += self.dt |
1331 | + for p in self.planets: |
1332 | + p.step() |
1333 | + |
1334 | +class Star(Turtle): |
1335 | + def __init__(self, m, x, v, gravSys, shape): |
1336 | + Turtle.__init__(self, shape=shape) |
1337 | + self.penup() |
1338 | + self.m = m |
1339 | + self.setpos(x) |
1340 | + self.v = v |
1341 | + gravSys.planets.append(self) |
1342 | + self.gravSys = gravSys |
1343 | + self.resizemode("user") |
1344 | + self.pendown() |
1345 | + def init(self): |
1346 | + dt = self.gravSys.dt |
1347 | + self.a = self.acc() |
1348 | + self.v = self.v + 0.5*dt*self.a |
1349 | + def acc(self): |
1350 | + a = Vec(0,0) |
1351 | + for planet in self.gravSys.planets: |
1352 | + if planet != self: |
1353 | + v = planet.pos()-self.pos() |
1354 | + a += (G*planet.m/abs(v)**3)*v |
1355 | + return a |
1356 | + def step(self): |
1357 | + dt = self.gravSys.dt |
1358 | + self.setpos(self.pos() + dt*self.v) |
1359 | + if self.gravSys.planets.index(self) != 0: |
1360 | + self.setheading(self.towards(self.gravSys.planets[0])) |
1361 | + self.a = self.acc() |
1362 | + self.v = self.v + dt*self.a |
1363 | + |
1364 | +## create compound yellow/blue turtleshape for planets |
1365 | + |
1366 | +def main(): |
1367 | + s = Turtle() |
1368 | + s.reset() |
1369 | + s.tracer(0,0) |
1370 | + s.ht() |
1371 | + s.pu() |
1372 | + s.fd(6) |
1373 | + s.lt(90) |
1374 | + s.begin_poly() |
1375 | + s.circle(6, 180) |
1376 | + s.end_poly() |
1377 | + m1 = s.get_poly() |
1378 | + s.begin_poly() |
1379 | + s.circle(6,180) |
1380 | + s.end_poly() |
1381 | + m2 = s.get_poly() |
1382 | + |
1383 | + planetshape = Shape("compound") |
1384 | + planetshape.addcomponent(m1,"orange") |
1385 | + planetshape.addcomponent(m2,"blue") |
1386 | + s.getscreen().register_shape("planet", planetshape) |
1387 | + s.tracer(1,0) |
1388 | + |
1389 | + ## setup gravitational system |
1390 | + gs = GravSys() |
1391 | + sun = Star(1000000, Vec(0,0), Vec(0,-2.5), gs, "circle") |
1392 | + sun.color("yellow") |
1393 | + sun.shapesize(1.8) |
1394 | + sun.pu() |
1395 | + earth = Star(12500, Vec(210,0), Vec(0,195), gs, "planet") |
1396 | + earth.pencolor("green") |
1397 | + earth.shapesize(0.8) |
1398 | + moon = Star(1, Vec(220,0), Vec(0,295), gs, "planet") |
1399 | + moon.pencolor("blue") |
1400 | + moon.shapesize(0.5) |
1401 | + gs.init() |
1402 | + gs.start() |
1403 | + return "Done!" |
1404 | + |
1405 | +if __name__ == '__main__': |
1406 | + msg = main() |
1407 | + print msg |
1408 | + mainloop() |
1409 | |
1410 | === added file 'pyturtle/tdemo_tree.py' |
1411 | --- pyturtle/tdemo_tree.py 1970-01-01 00:00:00 +0000 |
1412 | +++ pyturtle/tdemo_tree.py 2010-04-20 04:21:15 +0000 |
1413 | @@ -0,0 +1,73 @@ |
1414 | +#!/usr/bin/env python |
1415 | +# |
1416 | +# [SNIPPET_NAME: Breadth First Tree] |
1417 | +# [SNIPPET_CATEGORIES: PyTurtle] |
1418 | +# [SNIPPET_DESCRIPTION: Use turtle to draw a tree.] |
1419 | +# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html] |
1420 | +# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>] |
1421 | +# [SNIPPET_LICENSE: PSF] |
1422 | +# Code authorship from http://python.org/download/releases/2.6.4/ |
1423 | + |
1424 | + |
1425 | +""" turtle-example-suite: |
1426 | + |
1427 | + tdemo_tree.py |
1428 | + |
1429 | +Displays a 'breadth-first-tree' - in contrast |
1430 | +to the classical Logo tree drawing programs, |
1431 | +which use a depth-first-algorithm. |
1432 | + |
1433 | +Uses: |
1434 | +(1) a tree-generator, where the drawing is |
1435 | +quasi the side-effect, whereas the generator |
1436 | +always yields None. |
1437 | +(2) Turtle-cloning: At each branching point the |
1438 | +current pen is cloned. So in the end there |
1439 | +are 1024 turtles. |
1440 | +""" |
1441 | +from turtle import Turtle, mainloop |
1442 | +from time import clock |
1443 | + |
1444 | +def tree(plist, l, a, f): |
1445 | + """ plist is list of pens |
1446 | + l is length of branch |
1447 | + a is half of the angle between 2 branches |
1448 | + f is factor by which branch is shortened |
1449 | + from level to level.""" |
1450 | + if l > 3: |
1451 | + lst = [] |
1452 | + for p in plist: |
1453 | + p.forward(l) |
1454 | + q = p.clone() |
1455 | + p.left(a) |
1456 | + q.right(a) |
1457 | + lst.append(p) |
1458 | + lst.append(q) |
1459 | + for x in tree(lst, l*f, a, f): |
1460 | + yield None |
1461 | + |
1462 | +def maketree(): |
1463 | + p = Turtle() |
1464 | + p.setundobuffer(None) |
1465 | + p.hideturtle() |
1466 | + p.speed(0) |
1467 | + p.tracer(30,0) |
1468 | + p.left(90) |
1469 | + p.penup() |
1470 | + p.forward(-210) |
1471 | + p.pendown() |
1472 | + t = tree([p], 200, 65, 0.6375) |
1473 | + for x in t: |
1474 | + pass |
1475 | + print len(p.getscreen().turtles()) |
1476 | + |
1477 | +def main(): |
1478 | + a=clock() |
1479 | + maketree() |
1480 | + b=clock() |
1481 | + return "done: %.2f sec." % (b-a) |
1482 | + |
1483 | +if __name__ == "__main__": |
1484 | + msg = main() |
1485 | + print msg |
1486 | + mainloop() |
1487 | |
1488 | === added file 'pyturtle/tdemo_wikipedia.py' |
1489 | --- pyturtle/tdemo_wikipedia.py 1970-01-01 00:00:00 +0000 |
1490 | +++ pyturtle/tdemo_wikipedia.py 2010-04-20 04:21:15 +0000 |
1491 | @@ -0,0 +1,75 @@ |
1492 | +#!/usr/bin/env python |
1493 | +# |
1494 | +# [SNIPPET_NAME: tCircles] |
1495 | +# [SNIPPET_CATEGORIES: PyTurtle] |
1496 | +# [SNIPPET_DESCRIPTION: Use turtle to draw a circular design.] |
1497 | +# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html] |
1498 | +# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>] |
1499 | +# [SNIPPET_LICENSE: PSF] |
1500 | + |
1501 | + |
1502 | +""" turtle-example-suite: |
1503 | + |
1504 | + tdemo_wikipedia3.py |
1505 | + |
1506 | +This example is |
1507 | +inspired by the Wikipedia article on turtle |
1508 | +graphics. (See example wikipedia1 for URLs) |
1509 | + |
1510 | +First we create (ne-1) (i.e. 35 in this |
1511 | +example) copies of our first turtle p. |
1512 | +Then we let them perform their steps in |
1513 | +parallel. |
1514 | + |
1515 | +Followed by a complete undo(). |
1516 | +""" |
1517 | +from turtle import Screen, Turtle, mainloop |
1518 | +from time import clock, sleep |
1519 | + |
1520 | +def mn_eck(p, ne,sz): |
1521 | + turtlelist = [p] |
1522 | + #create ne-1 additional turtles |
1523 | + for i in range(1,ne): |
1524 | + q = p.clone() |
1525 | + q.rt(360.0/ne) |
1526 | + turtlelist.append(q) |
1527 | + p = q |
1528 | + for i in range(ne): |
1529 | + c = abs(ne/2.0-i)/(ne*.7) |
1530 | + # let those ne turtles make a step |
1531 | + # in parallel: |
1532 | + for t in turtlelist: |
1533 | + t.rt(360./ne) |
1534 | + t.pencolor(1-c,0,c) |
1535 | + t.fd(sz) |
1536 | + |
1537 | +def main(): |
1538 | + s = Screen() |
1539 | + s.bgcolor("black") |
1540 | + p=Turtle() |
1541 | + p.speed(0) |
1542 | + p.hideturtle() |
1543 | + p.pencolor("red") |
1544 | + p.pensize(3) |
1545 | + |
1546 | + s.tracer(36,0) |
1547 | + |
1548 | + at = clock() |
1549 | + mn_eck(p, 36, 19) |
1550 | + et = clock() |
1551 | + z1 = et-at |
1552 | + |
1553 | + sleep(1) |
1554 | + |
1555 | + at = clock() |
1556 | + while any([t.undobufferentries() for t in s.turtles()]): |
1557 | + for t in s.turtles(): |
1558 | + t.undo() |
1559 | + et = clock() |
1560 | + return "Laufzeit: %.3f sec" % (z1+et-at) |
1561 | + |
1562 | + |
1563 | +if __name__ == '__main__': |
1564 | + msg = main() |
1565 | + print msg |
1566 | + mainloop() |
1567 | |
1568 | === added file 'pyturtle/tdemo_yinyang.py' |
1569 | --- pyturtle/tdemo_yinyang.py 1970-01-01 00:00:00 +0000 |
1570 | +++ pyturtle/tdemo_yinyang.py 2010-04-20 04:21:15 +0000 |
1571 | @@ -0,0 +1,59 @@ |
1572 | +#!/usr/bin/env python |
1573 | +# |
1574 | +# [SNIPPET_NAME: Yin Yang] |
1575 | +# [SNIPPET_CATEGORIES: PyTurtle] |
1576 | +# [SNIPPET_DESCRIPTION: Use turtle to draw a yin yang.] |
1577 | +# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html] |
1578 | +# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>] |
1579 | +# [SNIPPET_LICENSE: PSF] |
1580 | +# Code authorship from http://python.org/download/releases/2.6.4/ |
1581 | + |
1582 | + |
1583 | +""" turtle-example-suite: |
1584 | + |
1585 | + tdemo_yinyang.py |
1586 | + |
1587 | +Another drawing suitable as a beginner's |
1588 | +programming example. |
1589 | + |
1590 | +The small circles are drawn by the circle |
1591 | +command. |
1592 | + |
1593 | +""" |
1594 | + |
1595 | +from turtle import * |
1596 | + |
1597 | +def yin(radius, color1, color2): |
1598 | + width(3) |
1599 | + color("black") |
1600 | + fill(True) |
1601 | + circle(radius/2., 180) |
1602 | + circle(radius, 180) |
1603 | + left(180) |
1604 | + circle(-radius/2., 180) |
1605 | + color(color1) |
1606 | + fill(True) |
1607 | + color(color2) |
1608 | + left(90) |
1609 | + up() |
1610 | + forward(radius*0.375) |
1611 | + right(90) |
1612 | + down() |
1613 | + circle(radius*0.125) |
1614 | + left(90) |
1615 | + fill(False) |
1616 | + up() |
1617 | + backward(radius*0.375) |
1618 | + down() |
1619 | + left(90) |
1620 | + |
1621 | +def main(): |
1622 | + reset() |
1623 | + yin(200, "white", "black") |
1624 | + yin(200, "black", "white") |
1625 | + ht() |
1626 | + return "Done!" |
1627 | + |
1628 | +if __name__ == '__main__': |
1629 | + main() |
1630 | + mainloop() |
These look great, Grant! Merged. :-)