Blame Lib/turtledemo/lindenmayer.py

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