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