annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/turtledemo/lindenmayer.py @ 68:5028fdace37b

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