【问题标题】:How to return turtle to its origin in recursion function如何在递归函数中将乌龟返回其原点
【发布时间】:2018-09-13 02:32:42
【问题描述】:

我在编写一个将圆圈绘制到某个“深度”的递归函数时遇到问题。

  • 例如,在深度一,程序应该绘制这个:depth 1
  • 在深度二,程序应该绘制这个:depth 2
  • 在深度 3 处,程序应绘制如下:depth 3
  • 作为参考,我的程序绘制了这个:myTurtleDrawing
import turtle

# These are basic instructions that do not affect the drawing if changed, only the appearance of the entities within the window
turtle.mode('logo')
turtle.speed(1)
turtle.shape('classic')
turtle.title("Circle")


def recCircle(d, r):
    if d == 0:
        pass
    if d == 1:
        print("Drawing to the depth of: ", d)
        turtle.down()
        turtle.circle(r)
        turtle.up()
    else:
        print("Drawing to the depth of: ", d)
        turtle.circle(r)
        turtle.seth(90)
        turtle.down()
        recCircle(d - 1, (r / 2))  # Draw the leftmost circle
        turtle.seth(360)
        turtle.up
        turtle.seth(270)
        turtle.forward(2 * r)
        turtle.down()
        recCircle(d - 1, - r / 2)  # Draw the rightmost circle
        turtle.up()
        turtle.seth(360)
        turtle.forward(2*r)


def main():
    d = 3                   #depth of recursion
    r = 100                 #radius of circle
    recCircle(d, r)
    turtle.done()


main()

我认为问题出在第 20 行左右

turtle.circle(r)

我不知道如何将海龟返回到相同的位置和方向。

turtle.home 或 turtle.goto,因为我试图不使用它们

【问题讨论】:

    标签: python python-3.x recursion turtle-graphics


    【解决方案1】:

    您的代码的具体问题:

    turtle.mode('logo')
    

    我理解使用 North == 0 的愿望,但在这种设计的情况下,这对您不利,我会保持默认方向。这行不通:

    turtle.up
    

    必须是turtle.up()。我看不出您是如何在代码中获得示例输出的,因为它应该会引发错误。没关系:

    turtle.seth(270)
    

    只要您假设垂直方向。但一般来说,如果我们要从任何角度进行绘制,都不能使用setheading(),因为它与turtle.goto()turtle.home() 一样绝对。但这似乎很奇怪:

        turtle.seth(360)
    

    对比只需turtle.setheading(0)。进行这样的绘图时的一个关键概念是将海龟返回到它开始的位置,或者在绘图命令中隐含地,或者通过撤消您为定位海龟所做的任何事情来显式地返回。以下是我对您的代码的完整修改:

    from turtle import Screen, Turtle
    
    def recCircle(depth, radius):
        if depth == 0:
            return
    
        print("Drawing to the depth of: ", depth)
        turtle.pendown()
        turtle.circle(radius)
        turtle.penup()
    
        if depth > 1:
            length = 11 * radius / 8  # no specific ratio provided, so eyeballed
    
            turtle.left(45)
            turtle.forward(length)
            turtle.right(45)
            recCircle(depth - 1, radius / 2)  # Draw the leftmost circle
            turtle.backward((2 * length ** 2) ** 0.5)
            recCircle(depth - 1, radius / 2)  # Draw the rightmost circle
            turtle.right(45)
            turtle.forward(length)
            turtle.left(45)
    
    screen = Screen()
    screen.title("Circle")
    
    turtle = Turtle('classic')
    turtle.speed('fast')
    
    depth = 3  # depth of recursion
    radius = 100  # radius of circle
    
    recCircle(depth, radius)
    
    screen.exitonclick()
    

    【讨论】:

    • 谢谢你。我有另一个问题。当我运行我的新递归函数时,乌龟开始从右侧绘制圆,这使得“递归”圆都在基圆的左侧。任何想法为什么? Function
    • @NateMendes,我猜你还在使用mode('logo'),它在标题为 0 时从右侧开始圆圈。正常模式在标题为 0 时从底部开始圆圈。
    猜你喜欢
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多