Lシステムを使ってPythonでバーンズリーシダを作ろうとしています。しかし、私は自分が望む結果を得ることができません。これが私の最終目標私が手に入れたいもの
しかし、代わりに、コードを実行すると、これが得られます。
これが私のコードです:`
import turtle
def drawLS(aTurtle, instructions, angle, distance):
stateSaver = []
print(instructions)
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 applyProduction(axiom, rules, n):
for i in range(n):
newString = ""
for ch in axiom:
newString = newString + rules.get(ch, ch)
axiom = newString
return axiom
def lSystem(axiom,rules,depth,initialPosition,heading,angle,length):
aTurtle = turtle.Turtle()
win = turtle.Screen()
aTurtle.up()
aTurtle.setposition(initialPosition)
aTurtle.left(90)
aTurtle.down()
aTurtle.setheading(heading)
aTurtle.speed(0)
newRules = applyProduction(axiom, rules, depth)
drawLS(aTurtle, newRules, angle, length)
win.exitonclick()
myRules={'X':'F+[[X]-X]+X','F':'FF'}
axiom='X'
lSystem(axiom, myRules, 7, (-150,-25), 360, 25, 2)
`