Chapter 9 フラクタル: 自然界に存在する幾何学¶
def hello():
print("Hello World")
hello()
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)
def countList(aList):
if aList == []: # base case, empty list
return 0
else:
return 1 + countList(aList[1:])
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)
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()
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)
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()
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')
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
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))
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()
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()
from lsystemtree import *
myRules = {'X': 'F[-X]+X', 'F': 'FF'}
axiom = 'X'
lSystem(axiom, myRules, 7, (0, -200), 90, 30, 2)