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