3

如果您运行以下代码,它会创建我正在尝试创建的两个并排图。我需要弄清楚如何在第一次绘制递归方法后停止它。

import turtle

def drawTriangle(size):
    turtle.left(30)
    turtle.forward(size)
    turtle.right(120)
    turtle.forward(size)
    turtle.right(120)
    turtle.forward(size)
    turtle.right(120)
    turtle.up()
    turtle.forward(size)
    turtle.right(30)
    turtle.down()

def drawTriangles(size, depth):
    if (depth < 1):
        pass
    else:
        drawTriangle(size)
        drawTriangles(size / 2, depth - 1)
        turtle.up()
        turtle.left(30)
        turtle.forward(-size)
        turtle.right(120)
        turtle.forward(size * 2)
        turtle.left(90)
        turtle.down()
        drawTriangle(size)
        drawTriangles(size / 2, depth - 1)
        turtle.up()
        turtle.left(30)
        turtle.forward(-size)
        turtle.left(60)
        turtle.forward(size * 2)
        turtle.right(90)
        turtle.down()
turtle.home()
turtle.left(90)
turtle.down()
turtle.speed("normal")
drawTriangles(100, 4)
input("pause")
4

3 回答 3

1

首先不错的尝试。你真的很亲密,伙计。这似乎是一个有趣的项目。:)

我已经修复了您的代码并将其包含在下面。您需要做的就是添加一个特殊条件来检查您是否在“基础”三角形上。如果是,请返回!

import turtle

def drawTriangle(size):
    turtle.left(30)
    turtle.forward(size)
    turtle.right(120)
    turtle.forward(size)
    turtle.right(120)
    turtle.forward(size)
    turtle.right(120)
    turtle.up()
    turtle.forward(size)
    turtle.right(30)
    turtle.down()

def drawTriangles(size, depth, baseTriangle = False):
    if (depth < 1):
        pass
    else:
        drawTriangle(size)
        drawTriangles(size / 2, depth - 1)
        if baseTriangle:
            return
        turtle.up()
        turtle.left(30)
        turtle.forward(-size)
        turtle.right(120)
        turtle.forward(size * 2)
        turtle.left(90)
        turtle.down()
        drawTriangle(size)
        drawTriangles(size / 2, depth - 1)
        turtle.up()
        turtle.left(30)
        turtle.forward(-size)
        turtle.left(60)
        turtle.forward(size * 2)
        turtle.right(90)
        turtle.down()
turtle.home()
turtle.left(90)
turtle.down()
turtle.speed("normal")
drawTriangles(100, 4, baseTriangle = True)
input("pause")
于 2013-09-11T21:54:17.033 回答
0

这是我的解决方案。我整理了一些逻辑,因此,如果深度不为零,则绘制三角形并触发递归两次,绘制三角形的每个顶角一次。最后,乌龟被移回它开始的地方。

乌龟通常是“向上”的,只在 drawTriangle 函数中“向下”。drawTriangle 函数现在也将海龟返回到它开始的位置。

注意整数除法。在做任何其他事情之前,我已经将大小转换为浮点数。

import turtle

def drawTriangle(size):
    turtle.down()
    turtle.left(30)
    turtle.forward(size)
    turtle.right(120)
    turtle.forward(size)
    turtle.right(120)
    turtle.forward(size)
    turtle.right(150)
    turtle.up()

def drawTriangles(size, depth):
    size = float(size)
    if (depth > 0):
        drawTriangle(size)
        turtle.left(30)
        turtle.forward(size)
        turtle.right(30)
        drawTriangles(size / 2, depth - 1)
        turtle.right(90)
        turtle.forward(size)
        turtle.left(90)
        drawTriangles(size / 2, depth - 1)
        turtle.left(150)
        turtle.forward(size)
        turtle.right(150)

turtle.home()
turtle.left(90)
turtle.speed("normal")
drawTriangles(150.0, 5)
input("pause")
于 2013-09-11T22:09:45.733 回答
0

如果我猜的话,可能是因为您调用了以下两次(在 drawTriangles() 中),并且 size 变量的值在这两次调用之间没有改变。

drawTriangle(size)
drawTriangles(size / 2, depth - 1)
于 2013-09-11T20:53:36.573 回答