【问题标题】:Robotics - Recursive function for fractal.Robotics - 分形的递归函数。
【发布时间】:2014-11-14 17:44:06
【问题描述】:

我目前正在与 Myro/Calico 和 Robotics 合作。我正在尝试运行分形的递归函数。我正在使用 Python。

我一直在这里关注伪代码。 Fractal

到目前为止,我已经尝试在没有递归的情况下实现第一步。而且运行良好

# 1 foot per 2 seconds.  x * 2 = feet desired.  
def fractal(x):
    waitTime = x*2
    turnRight(1, 0.825) #90 degree turn
    forward(1, x/3) #move length/3 steps
    turnLeft(1, 0.55)#60 degree turn
    forward(1, x/3) #move length/3 steps
    turnRight(1, 1.1) #120 degree turn
    forward(1, x/3) #move length/3 steps
    turnLeft(1, 0.55) #60 degree turn
    forward(1, x/3) #move length/3 steps

虽然这可行,但我的目标是递归地执行此操作,但在每次迭代时制作一条较小的曲线。我试图这样做,但我的机器人没有按预期移动。

这是我的递归尝试

def fractal(x):
    waitTime = x*2
    if (x == 1):
        forward(x/3)
    else:
        (x-1)/3
    turnLeft(1,0.55) #60 degrees
    if (x == 1):
        forward(x/3)
    else:
        (x-1)/3
    turnRight(1, 1.1) #120 degree turn
    if (x == 1):
        forward(x/3)
    else:
        (x-1)/3
    turnLeft(1, 0.55)#60 degree turn
    if (x == 1):
        forward(x/3)
    else:
        (x-1)/3

我的机器人只是左右转动,但没有完全成型。没有递归的那个开始了分形。我只需要递归来遍历整个分形。

【问题讨论】:

  • 你怎么称呼分形?据我所知,这里没有递归。
  • 我正在使用分形(3)来指定我想要去的脚。您可以输入任何数字。我尝试在我链接的网站上遵循伪。我将如何进行递归?
  • 您知道,else: (x-1)/3 什么都不做。你的意思是forward((x-1)/3)x = (x-1)/3fractal((x-1)/3) 还是什么?
  • 哦..谢谢凯文。我的意思是用户输入的任何变量,比如 5,都会变成 (5-1)/3。那没有发生?我想我应该做 x = (x-1)/3?
  • 我不确定,但我认为fractal 需要有两个变量,而不仅仅是一个。您不能仅用一个变量轻松地表示分形深度曲线的宽度。如果我想要一个 100 像素宽的五次迭代 Koch 曲线怎么办?对 fractal 的调用会是什么样子?如果我想要一条 100 像素宽的七次迭代曲线,它会有什么不同?

标签: python recursion robotics myro calico-project


【解决方案1】:

我想这就是你想做的事

x = number of interations
l = lenth(wait time)
def fractal(x, l):
    if (x == 1):
        forward(l/3)
    else:
        fractal((x-1), l/3)
    turnLeft(1,0.55) #60 degrees
    if (x == 1):
        forward(l/3)
    else:
        fractal((x-1), l/3)
    turnRight(1, 1.1) #120 degree turn
    if (x == 1):
        forward(l/3)
    else:
        fractal((x-1), l/3)
    turnLeft(1, 0.55)#60 degree turn
    if (x == 1):
        forward(l/3)
    else:
        fractal((x-1), l/3)

【讨论】:

  • 没问题 递归是一个很难解决的问题。如果您对递归和编程感兴趣,最容易开始的方法之一就是斐波那契。
猜你喜欢
  • 2016-02-18
  • 1970-01-01
  • 2016-09-19
  • 2012-08-29
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
  • 2022-11-25
  • 1970-01-01
相关资源
最近更新 更多