绘制带顺序三角形的Sierpinski递归问题

时间:2018-11-01 18:00:51

标签: python recursion turtle-graphics fractals

这是Sierpinski triangle的代码,我刚刚将度变量从原始文本更改为2(在sierpinsky()的初始main()调用中),我使用Python 3.5:

import turtle

def drawTriangle(points,color,myTurtle):
    myTurtle.fillcolor(color)
    myTurtle.up()
    myTurtle.goto(points[0][0],points[0][1])
    myTurtle.down()
    myTurtle.begin_fill()
    myTurtle.goto(points[1][0],points[1][1])
    myTurtle.goto(points[2][0],points[2][1])
    myTurtle.goto(points[0][0],points[0][1])
    myTurtle.end_fill()

def getMid(p1,p2):
    return ( (p1[0]+p2[0]) / 2, (p1[1] + p2[1]) / 2)

def sierpinski(points,degree,myTurtle):
    colormap = ['blue','red','green','white','yellow',
                'violet','orange']
    drawTriangle(points,colormap[degree],myTurtle)
    if degree > 0:
        sierpinski([points[0],
                        getMid(points[0], points[1]),
                        getMid(points[0], points[2])],
                   degree-1, myTurtle)
        sierpinski([points[1],
                        getMid(points[0], points[1]),
                        getMid(points[1], points[2])],
                   degree-1, myTurtle)
        sierpinski([points[2],
                        getMid(points[2], points[1]),
                        getMid(points[0], points[2])],
                   degree-1, myTurtle)

def main():
   myTurtle = turtle.Turtle()
   myWin = turtle.Screen()
   myPoints = [[-100,-50],[0,100],[100,-50]]
   sierpinski(myPoints,2,myTurtle)
   myWin.exitonclick()

main()

运行此代码时,我可以清楚地看到乌龟在第一个递归调用处在左侧绘制了第一个内部三角形:

sierpinski([points[0], getMid(points[0], points[1]), getMid(points[0], points[2])], 1, myTurtle)

,然后继续按该顺序绘制该三角形内的左,上和右三角形。到目前为止,一切都很好。

然后要绘制的下一个三角形是呼叫(大三角形)上方的三角形:

sierpinski([points[1], getMid(points[0], points[1]), getMid(points[1], points[2])], 1, myTurtle)

这是令人困惑的地方,我希望递归首先开始在该三角形内绘制左下三角形。但这不是,而是从绘制最上面的一个开始。

当我运行模仿上面代码中的递归的虚拟代码时:

def sierpinski(points,degree):
    print(points + " " + str(degree))
    if degree > 0:
        sierpinski("bottom left triangle", degree-1)
        sierpinski("top triangle", degree-1)
        sierpinski("bottom right triangle", degree-1)


sierpinski("start", 2)

输出为:

start 2

bottom left triangle 1

bottom left triangle 0

top triangle 0

bottom right triangle 0

top triangle 1

bottom left triangle 0

top triangle 0

bottom right triangle 0

bottom right triangle 1

bottom left triangle 0

top triangle 0

bottom right triangle 0

我们可以清楚地看到遵守绘制三角形的顺序。你们能告诉我我在想什么吗?

1 个答案:

答案 0 :(得分:0)

如果希望三角形从左下方顺时针绘制,则需要从左下方顺时针定义点。那不是你做的:

# clockwise from lower left:
[points[0], getMid(points[0], points[1]), getMid(points[0], points[2])]

# counter-clockwise from top:
[points[1], getMid(points[0], points[1]), getMid(points[1], points[2])]

# counter-clockwise from lower right:
[points[2], getMid(points[2], points[1]), getMid(points[0], points[2])]

这是我的修改,它从左下角顺时针绘制了所有三角形。我还添加了一些其他代码更改供您考虑:

from turtle import Screen, Turtle, Vec2D

COLORMAP = ['blue', 'red', 'green', 'white', 'yellow', 'violet', 'orange']

def drawTriangle(points, color, turtle):
    turtle.fillcolor(color)

    turtle.up()
    turtle.goto(points[0])
    turtle.down()

    turtle.begin_fill()
    turtle.goto(points[1])
    turtle.goto(points[2])
    turtle.goto(points[0])
    turtle.end_fill()

def getMid(p1, p2):
    return(p1 + p2) * 0.5

def sierpinski(points, degree, turtle):
    drawTriangle(points, COLORMAP[degree], turtle)

    if degree < 1:
        return

    sierpinski([points[0], getMid(points[0], points[1]), getMid(points[0], points[2])], degree - 1, turtle)
    sierpinski([getMid(points[0], points[1]), points[1], getMid(points[1], points[2])], degree - 1, turtle)
    sierpinski([getMid(points[0], points[2]), getMid(points[2], points[1]), points[2],], degree - 1, turtle)

def main():
    screen = Screen()
    yertle = Turtle()

    myPoints = [Vec2D(-100, -50), Vec2D(0, 100), Vec2D(100, -50)]
    sierpinski(myPoints, 2, yertle)

    yertle.hideturtle()
    screen.exitonclick()

main()