jpayne@68: #!/usr/bin/env python3 jpayne@68: """ turtle-example-suite: jpayne@68: jpayne@68: xtx_lindenmayer_indian.py jpayne@68: jpayne@68: Each morning women in Tamil Nadu, in southern jpayne@68: India, place designs, created by using rice jpayne@68: flour and known as kolam on the thresholds of jpayne@68: their homes. jpayne@68: jpayne@68: These can be described by Lindenmayer systems, jpayne@68: which can easily be implemented with turtle jpayne@68: graphics and Python. jpayne@68: jpayne@68: Two examples are shown here: jpayne@68: (1) the snake kolam jpayne@68: (2) anklets of Krishna jpayne@68: jpayne@68: Taken from Marcia Ascher: Mathematics jpayne@68: Elsewhere, An Exploration of Ideas Across jpayne@68: Cultures jpayne@68: jpayne@68: """ jpayne@68: ################################ jpayne@68: # Mini Lindenmayer tool jpayne@68: ############################### jpayne@68: jpayne@68: from turtle import * jpayne@68: jpayne@68: def replace( seq, replacementRules, n ): jpayne@68: for i in range(n): jpayne@68: newseq = "" jpayne@68: for element in seq: jpayne@68: newseq = newseq + replacementRules.get(element,element) jpayne@68: seq = newseq jpayne@68: return seq jpayne@68: jpayne@68: def draw( commands, rules ): jpayne@68: for b in commands: jpayne@68: try: jpayne@68: rules[b]() jpayne@68: except TypeError: jpayne@68: try: jpayne@68: draw(rules[b], rules) jpayne@68: except: jpayne@68: pass jpayne@68: jpayne@68: jpayne@68: def main(): jpayne@68: ################################ jpayne@68: # Example 1: Snake kolam jpayne@68: ################################ jpayne@68: jpayne@68: jpayne@68: def r(): jpayne@68: right(45) jpayne@68: jpayne@68: def l(): jpayne@68: left(45) jpayne@68: jpayne@68: def f(): jpayne@68: forward(7.5) jpayne@68: jpayne@68: snake_rules = {"-":r, "+":l, "f":f, "b":"f+f+f--f--f+f+f"} jpayne@68: snake_replacementRules = {"b": "b+f+b--f--b+f+b"} jpayne@68: snake_start = "b--f--b--f" jpayne@68: jpayne@68: drawing = replace(snake_start, snake_replacementRules, 3) jpayne@68: jpayne@68: reset() jpayne@68: speed(3) jpayne@68: tracer(1,0) jpayne@68: ht() jpayne@68: up() jpayne@68: backward(195) jpayne@68: down() jpayne@68: draw(drawing, snake_rules) jpayne@68: jpayne@68: from time import sleep jpayne@68: sleep(3) jpayne@68: jpayne@68: ################################ jpayne@68: # Example 2: Anklets of Krishna jpayne@68: ################################ jpayne@68: jpayne@68: def A(): jpayne@68: color("red") jpayne@68: circle(10,90) jpayne@68: jpayne@68: def B(): jpayne@68: from math import sqrt jpayne@68: color("black") jpayne@68: l = 5/sqrt(2) jpayne@68: forward(l) jpayne@68: circle(l, 270) jpayne@68: forward(l) jpayne@68: jpayne@68: def F(): jpayne@68: color("green") jpayne@68: forward(10) jpayne@68: jpayne@68: krishna_rules = {"a":A, "b":B, "f":F} jpayne@68: krishna_replacementRules = {"a" : "afbfa", "b" : "afbfbfbfa" } jpayne@68: krishna_start = "fbfbfbfb" jpayne@68: jpayne@68: reset() jpayne@68: speed(0) jpayne@68: tracer(3,0) jpayne@68: ht() jpayne@68: left(45) jpayne@68: drawing = replace(krishna_start, krishna_replacementRules, 3) jpayne@68: draw(drawing, krishna_rules) jpayne@68: tracer(1) jpayne@68: return "Done!" jpayne@68: jpayne@68: if __name__=='__main__': jpayne@68: msg = main() jpayne@68: print(msg) jpayne@68: mainloop()