annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/turtledemo/lindenmayer.py @ 69:33d812a61356

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