Python Sierpinski Triangle

时间:2017-01-23 21:21:35

标签: python turtle-graphics

我正在尝试用绿色的小三角形制作一个Sierpinski三角形。 但是我的输出错误如图所示。

有人可以帮我纠正我的代码吗?

Sierpinski三角形(黑色)如下所示。我的截图位于代码下方。

Sierpinski triangle

import turtle

    def draw_triangle(some_turtle):
        #This for loop will create - Outer Triangle
        some_turtle.color("green")
        some_turtle.begin_fill()
        for i in range(1,4):
            some_turtle.forward(50)
            some_turtle.left(120)
            #This for loop will create - Inner Triangle
            for j in range(1,4):    
                some_turtle.forward(25)
                some_turtle.left(120)
                some_turtle.end_fill()

    def draw_art():     
        window = turtle.Screen()
        window.bgcolor("white")
        #Create the turtle Brad - Draws a Triangle
        brad = turtle.Turtle()
        brad.shape("arrow")
        brad.color("green")
        brad.speed(50)
        #This for loop will create - Inner & Outer Triangle At 120 deg
        for d in range(1,4):
            brad.left(120)
            #This for loop will create - Inner & Outer Triangle At distance 50 
            for c in range(1,5):
                draw_triangle(brad)
                brad.forward(50)
    window.exitonclick()
    draw_art()

Screenshot

unfilled S. triangle

enter image description here

3 个答案:

答案 0 :(得分:1)

您的begin_fillend_fill不在正确的位置。开始填充外部for循环的顶部,并在其底部填充。

换句话说,将它设为draw_triangle函数:

def draw_triangle(some_turtle):
    #This for loop will create - Outer Triangle
    some_turtle.color("green")
    for i in range(1,4):
        some_turtle.begin_fill()
        some_turtle.forward(50)
        some_turtle.left(120)
        #This for loop will create - Inner Triangle
        for j in range(1,4):    
            some_turtle.forward(25)
            some_turtle.left(120)
        some_turtle.end_fill()

请注意begin_fillend_fill

的展示位置的差异

答案 1 :(得分:1)

您发布的代码存在多个问题,除了begin_fill()end_fill()位于错误位置之外:您在exitonclick()之前致电draw_art(),因此您的计划不应该&# 39; t run at all;您从未定义的范围引用window;根据文档,speed()的论点没有多大意义。

以下返工解决了上述问题和一些风格调整:

import turtle

def draw_triangle(some_turtle):
    # This for loop will create - Outer Triangle
    for i in range(3):
        some_turtle.forward(50)
        some_turtle.left(120)
        # This for loop will create - Inner Triangle
        some_turtle.begin_fill()
        for j in range(3):
            some_turtle.forward(25)
            some_turtle.left(120)
        some_turtle.end_fill()

def draw_art():
    # Create the turtle Brad - Draws a Triangle
    brad = turtle.Turtle(shape="arrow")
    brad.color("green")
    brad.speed("fastest")

    # This for loop will create - Inner & Outer Triangle At 120 deg
    for d in range(3):
        brad.left(120)
        # This for loop will create - Inner & Outer Triangle At distance 50
        for c in range(4):
            draw_triangle(brad)
            brad.forward(50)

    brad.hideturtle()

window = turtle.Screen()
window.bgcolor("white")

draw_art()

window.exitonclick()

<强>输出

enter image description here

如果你仍然没有得到上面的图片,这里需要考虑的事情:当填充已经填充的区域时,乌龟/ tkinter填充逻辑在Unix和Windows系统之间会有所不同。您的基本算法会重绘并重新填充许多三角形。因此,如果您仍然遇到问题,请考虑将代码更改为仅绘制和填充每个三角形一次。像下面的返工一样,不会像你原来的那样复制角落的绘图:

def draw_triangle(some_turtle):
    # This for loop will create - Outer Triangle
    for i in range(3):
        # This for loop will create - Inner Triangle
        some_turtle.begin_fill()
        for j in range(3):
            some_turtle.forward(25)
            some_turtle.left(120)
        some_turtle.end_fill()
        some_turtle.forward(50)
        some_turtle.left(120)

def draw_art():
    # Create the turtle Brad - Draws a Triangle
    brad = turtle.Turtle(shape="arrow")
    brad.color("green")
    brad.speed("fastest")

    # This for loop will create - Inner & Outer Triangle At 120 deg
    for d in range(3):
        brad.left(120)
        brad.forward(50)
        # This for loop will create - Inner & Outer Triangle At distance 50
        for c in range(3):
            draw_triangle(brad)
            brad.forward(50)

    brad.hideturtle()

答案 2 :(得分:0)

import turtle
    t=turtle.Turtle()
    def f(a,b):
        #a=the distance that need to go, b how many triangle in side the large triangle
        if b==0:
            return 

        else:
            for i in range(3):
                t.forward(a)
                t.left(120)
                f(a/2,b-1)