Chapter 9 フラクタル: 自然界に存在する幾何学

listing09-01.py
def hello():
    print("Hello World")
    hello()
listing09-02.py
def drawSquare(aTurtle, side):
    for i in range(4):
        aTurtle.forward(side)
        aTurtle.right(90)


def nestedBox(aTurtle, side):
    if side >= 1:  # check for base case: side < 1
        drawSquare(aTurtle, side)
        nestedBox(aTurtle, side - 5)
listing09-03.py
def countList(aList):
    if aList == []:  # base case, empty list
        return 0
    else:
        return 1 + countList(aList[1:])
listing09-04.py
def tree(t, trunkLength):
    if trunkLength < 5:  # check for base case
        return
    else:
        t.forward(trunkLength)
        t.right(30)
        tree(t, trunkLength - 15)
        t.left(60)
        tree(t, trunkLength - 15)
        t.right(30)
        t.backward(trunkLength)
session09-01.py
import turtle
from recursivetree import *

turtle.tracer(False)
t = turtle.Turtle()
t.up()
t.goto(0, -225)
t.down()
t.color("DeepPink", "DeepPink")
t.left(90)  # face up
t.speed(100)
tree(t, 115)
turtle.mainloop()
listing09-05.py
def drawTriangle(t, p1, p2, p3):
    t.up()
    t.goto(p1)
    t.down()
    t.goto(p2)
    t.goto(p3)
    t.goto(p1)


def midPoint(p1, p2):
    return ((p1[0] + p2[0]) / 2.0, (p1[1] + p2[1]) / 2.0)


def sierpinski(t, p1, p2, p3, depth):
    if depth > 0:
        sierpinski(t, p1, midPoint(p1, p2), midPoint(p1, p3), depth - 1)
        sierpinski(t, p2, midPoint(p2, p3), midPoint(p2, p1), depth - 1)
        sierpinski(t, p3, midPoint(p3, p1), midPoint(p3, p2), depth - 1)
    else:  # base case
        drawTriangle(t, p1, p2, p3)
session09-02.py
import turtle
from recursivetriangles import *

t = turtle.Turtle()
turtle.tracer(False)
t.color('DeepPink')
sierpinski(t, [-225, -250], [225, -250], [0, 225], 5)
t.hideturtle()
turtle.mainloop()
listing09-06.py
def drawLS(t, instructions, angle, distance):
    for cmd in instructions:
        if cmd == 'F':
            t.forward(distance)
        elif cmd == 'B':
            t.backward(distance)
        elif cmd == '+':
            t.right(angle)
        elif cmd == '-':
            t.left(angle)
        else:
            print('Error:', cmd, 'is an unkwoun command')
listing09-07.py
def applyProduction(axiom, rules, n):
    for i in range(n):
        newString = ""
        for ch in axiom:
            newString = newString + rules.get(ch, ch)
        axiom = newString
    return axiom
session09-03.py
from lsystemtree import *
axiom = 'A'
myRules = {'A': 'B', 'B': 'AB'}
for i in range(10):
    res = applyProduction(axiom, myRules, i)
    print("{0:2d} {1}".format(len(res), res))
listing09-08.py
def drawLS(aTurtle, instructions, angle, distance):
    stateSaver = []
    for cmd in instructions:
        if cmd == 'F':
            aTurtle.forward(distance)
        elif cmd == 'B':
            aTurtle.backward(distance)
        elif cmd == '+':
            aTurtle.right(angle)
        elif cmd == '-':
            aTurtle.left(angle)
        elif cmd == '[':
            pos = aTurtle.position()
            head = aTurtle.heading()
            stateSaver.append((pos, head))
        elif cmd == ']':
            pos, head = stateSaver.pop()
            aTurtle.up()
            aTurtle.setposition(pos)
            aTurtle.setheading(head)
            aTurtle.down()
listing09-09.py
def lSystem(axiom, rules, depth, initialPosition, heading, angle, length):
    aTurtle = turtle.Turtle()
    win = turtle.Screen()
    aTurtle.up()
    aTurtle.setposition(initialPosition)
    aTurtle.down()
    aTurtle.setheading(heading)
    newRules = applyProduction(axiom, rules, depth)
    drawLS(aTurtle, newRules, angle, length)
    win.exitonclick()
session09-04.py
from lsystemtree import *
myRules = {'X': 'F[-X]+X', 'F': 'FF'}
axiom = 'X'
lSystem(axiom, myRules, 7, (0, -200), 90, 30, 2)