我正在尝试使用 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)
`