ยปCore Development>Code coverage>Lib/turtledemo/lindenmayer.py

# Python code coverage for Lib/turtledemo/lindenmayer.py

#countcontent
1n/a#!/usr/bin/env python3
2n/a""" turtle-example-suite:
3n/a
4n/a xtx_lindenmayer_indian.py
5n/a
6n/aEach morning women in Tamil Nadu, in southern
7n/aIndia, place designs, created by using rice
8n/aflour and known as kolam on the thresholds of
9n/atheir homes.
10n/a
11n/aThese can be described by Lindenmayer systems,
12n/awhich can easily be implemented with turtle
13n/agraphics and Python.
14n/a
15n/aTwo examples are shown here:
16n/a(1) the snake kolam
17n/a(2) anklets of Krishna
18n/a
19n/aTaken from Marcia Ascher: Mathematics
20n/aElsewhere, An Exploration of Ideas Across
21n/aCultures
22n/a
23n/a"""
24n/a################################
25n/a# Mini Lindenmayer tool
26n/a###############################
27n/a
28n/afrom turtle import *
29n/a
30n/adef replace( seq, replacementRules, n ):
31n/a for i in range(n):
32n/a newseq = ""
33n/a for element in seq:
34n/a newseq = newseq + replacementRules.get(element,element)
35n/a seq = newseq
36n/a return seq
37n/a
39n/a for b in commands:
40n/a try:
41n/a rules[b]()
42n/a except TypeError:
43n/a try:
44n/a draw(rules[b], rules)
45n/a except:
46n/a pass
47n/a
48n/a
50n/a ################################
51n/a # Example 1: Snake kolam
52n/a ################################
53n/a
54n/a
55n/a def r():
56n/a right(45)
57n/a
58n/a def l():
59n/a left(45)
60n/a
61n/a def f():
62n/a forward(7.5)
63n/a
64n/a snake_rules = {"-":r, "+":l, "f":f, "b":"f+f+f--f--f+f+f"}
65n/a snake_replacementRules = {"b": "b+f+b--f--b+f+b"}
66n/a snake_start = "b--f--b--f"
67n/a
68n/a drawing = replace(snake_start, snake_replacementRules, 3)
69n/a
70n/a reset()
71n/a speed(3)
72n/a tracer(1,0)
73n/a ht()
74n/a up()
75n/a backward(195)
76n/a down()
77n/a draw(drawing, snake_rules)
78n/a
79n/a from time import sleep
80n/a sleep(3)
81n/a
82n/a ################################
83n/a # Example 2: Anklets of Krishna
84n/a ################################
85n/a
86n/a def A():
87n/a color("red")
88n/a circle(10,90)
89n/a
90n/a def B():
91n/a from math import sqrt
92n/a color("black")
93n/a l = 5/sqrt(2)
94n/a forward(l)
95n/a circle(l, 270)
96n/a forward(l)
97n/a
98n/a def F():
99n/a color("green")
100n/a forward(10)
101n/a
102n/a krishna_rules = {"a":A, "b":B, "f":F}
103n/a krishna_replacementRules = {"a" : "afbfa", "b" : "afbfbfbfa" }
104n/a krishna_start = "fbfbfbfb"
105n/a
106n/a reset()
107n/a speed(0)
108n/a tracer(3,0)
109n/a ht()
110n/a left(45)
111n/a drawing = replace(krishna_start, krishna_replacementRules, 3)
112n/a draw(drawing, krishna_rules)
113n/a tracer(1)
114n/a return "Done!"
115n/a
116n/aif __name__=='__main__':
117n/a msg = main()
118n/a print(msg)
119n/a mainloop()