使用海龟图形恢复 l 系统代码中记录的状态

发布于 2024-11-14 18:11:27 字数 1000 浏览 2 评论 0原文

我正在使用海龟图形来重现 l-系统(TurtleWorld 库)。我尝试应用的规则在不涉及返回到以前保存的状态时效果很好,但是每当有 [ 和 ] (参见下面的规则)时,事情就会中断,海龟只会绘制随机的 bs。

基本上,我认为检查“]”出现位置的 IF 语句就是代码中断的地方。 (另外,我知道它目前还没有优化,为了清楚起见,我已经编写了一个可靠的 IF...)

编辑:新代码 - 这整个计算角度的事情是不必要的,因为我们有 get_heading(),它告诉我们我们所处的角度。

import turtle

turtle.down()

n = 'F'

s1 = 'F'
s2 = 'FF-[-F+F+F]+[+F-F-F]'
#s3 = 'F'
#s4 = 'FF'

steps = 5

for i in range(steps):
    n = n.replace(s1,s2)
    #n = n.replace(s3,s4)

a = 25

x = []
y = []
angle = []

for i in n:
    if i == 'F':
        turtle.forward(2)
    if i == '+':
        turtle.left(a)
    if i == '-':
        turtle.right(a)
    if i=='[':
        x.append(turtle.xcor())
        y.append(turtle.ycor())
        angle.append(turtle.heading())
    if i==']':
        turtle.pu()
        turtle.setpos(x[len(x)-1],y[len(y)-1])
        turtle.right(turtle.heading())
        turtle.setheading(angle[len(angle)-1])
        x.pop()
        y.pop()
        angle.pop()
        turtle.pd()

I'm using turtle graphics to reproduce l-systems (TurtleWorld library). The rules I have tried to apply work well when they don't involve going back to a previous saved state, but whenever there is a [ and ] (see rule below), things break and the turtle just draws random bs.

Basically, the IF statement that checks where ']'is present, is where the code breaks, I think. (Also, I know it's not optimized for the moment, I have written a solid IF for clarity's sake...)

EDIT : New code - this whole calculating angle thing was not necessary, as we have get_heading(), which informs us of the angle we're oriented into.

import turtle

turtle.down()

n = 'F'

s1 = 'F'
s2 = 'FF-[-F+F+F]+[+F-F-F]'
#s3 = 'F'
#s4 = 'FF'

steps = 5

for i in range(steps):
    n = n.replace(s1,s2)
    #n = n.replace(s3,s4)

a = 25

x = []
y = []
angle = []

for i in n:
    if i == 'F':
        turtle.forward(2)
    if i == '+':
        turtle.left(a)
    if i == '-':
        turtle.right(a)
    if i=='[':
        x.append(turtle.xcor())
        y.append(turtle.ycor())
        angle.append(turtle.heading())
    if i==']':
        turtle.pu()
        turtle.setpos(x[len(x)-1],y[len(y)-1])
        turtle.right(turtle.heading())
        turtle.setheading(angle[len(angle)-1])
        x.pop()
        y.pop()
        angle.pop()
        turtle.pd()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私野 2024-11-21 18:11:27

一些想法:

  • 您永远不会在 ] 处理程序中将 angle 设置为新角度 (newa)。
  • 根据评论,您的条件是错误的,如果角度为正,newa>0会将其向左转。
  • 您确定 rt 可以很好地处理负角度吗?
  • 如果您使用 pop 代替,并推送一个元组或类似的状态,您可以大大简化代码。
  • 索引 -1 等于 len(lst) - 1

pop-建议示例:

>>> state = []
>>> angle = 90
>>> posx = 10
>>> posy = 15
>>> state.append((angle, posx, posy))
>>> angle = 40
>>> angle, posx, posy = state.pop()
>>> angle
90

A couple of ideas:

  • You never set angle to the new angle (newa) in your ]-handler.
  • Your conditional is wrong according to the comment, newa>0 would turn it left if the angle is positive.
  • Are you sure rt handles negative angles well?
  • You could simplify your code a lot if you use pop instead, and pushed a tuple or somesuch of the state.
  • An index of -1 is equal to len(lst) - 1.

Example of the pop-suggestion:

>>> state = []
>>> angle = 90
>>> posx = 10
>>> posy = 15
>>> state.append((angle, posx, posy))
>>> angle = 40
>>> angle, posx, posy = state.pop()
>>> angle
90
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文