python - 如何在递归函数中将乌龟返回其原点
问题描述
我在编写一个将圆圈绘制到某个“深度”的递归函数时遇到了麻烦。
- 例如,在深度 1,程序应该绘制: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,因为我试图不使用它们
解决方案
Specific issues with your code:
turtle.mode('logo')
I understand the desire to work with North == 0 but in the case of this design, it's not to your advantage and I'd stay with the default orientation. This won't work:
turtle.up
It needs to be turtle.up()
. I don't see how you got your example output with this in the code as it should throw an error. This is OK:
turtle.seth(270)
as long as you're assuming a vertical orientation. But in general, if we want to draw at any angle, you can't use setheading()
as it's as absolute as turtle.goto()
or turtle.home()
. But this, seems strange:
turtle.seth(360)
vs. simply turtle.setheading(0)
. A key concept when doing a drawing like this is to return the turtle to where it started, either implicitly in the drawing command, or explicitly by undoing anything you did to position the turtle. Below's my complete rework of your code:
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()
推荐阅读
- performance - Flutter - 堆栈子级过多时性能下降(jank)
- python - 如何在小提琴图中添加新的四分位线?
- lua - 如何检查输入是否为数字
- angular - PrimeNG p-multiselect 与来自 api 的选项不会间歇性地显示选定的值
- postgresql - 邮递员。条件存储数据唯一性
- hyperlink - 使用链接在 DOORS 模块之间传输数据
- windows - 应用程序在打开时询问凭据
- c++ - 使用 imshow 时出现奇怪的错误
- javascript - Express:错误:无法在视图目录中查找视图“索引”
- go - 如何为 tls 客户端身份验证正确生成客户端证书