创建谢尔宾斯基三角形

Creating a Sierpinski triangle

我正在尝试使用递归的 ezgraphics 库创建一个 Sierpinski 三角形。我想我做错了几件事,但由于我对编码还比较陌生,所以我被困住了。我不是在找人为我完成代码,只是在寻找下一步尝试的建议。

这是我正在使用的库:http://www.ezgraphics.org/ReferenceGuide/ReferenceGuide

到目前为止,这是我的粗略代码:

from ezgraphics import GraphicsWindow

def main():
    winSpec = int(input("enter the size of the window: "))
    subDiv = int(input("Enter number of subdivisions: "))
    win = GraphicsWindow(winSize,winSize)
    canvas = win.canvas()
    canvas.setColor( "blue" )
    triangle1 = [winSpec/2, 0, 0, winSpec, winSpec, winSpec]
    drawGasket(triangle1,subDiv,canvas)
    #canvas.drawPolygon(triangle1[0],triangle1[1],triangle1[2],triangle1[3],triangle1[4],triangle1[5])  
    win.wait()

def drawGasket(points,subDiv,canvas):    
    print(points)
    canvas.drawPolygon(points[0],points[1],points[2],points[3],points[4],points[5])  
    if subDiv > 0:
        print("hi")
        points2 = [points[0] + points[2] /2,points[1] + points[3] /2, points[0] + points[4] /2,points[1] + points[5] /2, points[2] + points[4] /2,points[3] + points[5] /2]
        drawGasket(points2, subDiv - 1, canvas)

您应该只为 subDiv == 0 绘制,对于其他值,您应该计算中间点并 运行 再次使用三个新三角形。

您必须使用 () 来除和 - (points[0] + points[2]) /2。如果没有 (),则在添加点之前先除第二个点。


顺便说一句

更短

 canvas.drawPolygon(points)

您可以将点分配给变量,这意味着可以使代码更具可读性

 x1, y1, x2, y2, x3, y3 = points

然后你可以计算中间点

 middle_1_2_x = (x1+x2)/2
 middle_1_2_y = (y1+y2)/2

 middle_2_3_x = (x2+x3)/2
 middle_2_3_y = (y2+y3)/2

 middle_1_3_x = (x1+x3)/2
 middle_1_3_y = (y1+y3)/2

在创建新三角形之前

 triangle1 = [x1, y1, middle_1_2_x, middle_1_2_y, middle_1_3_x, middle_1_3_y]
 triangle2 = [middle_1_2_x, middle_1_2_y, x2, y2, middle_2_3_x, middle_2_3_y]
 triangle3 = [middle_1_3_x, middle_1_3_y, middle_2_3_x, middle_2_3_y, x3, y3]

结果:


完整代码:

from ezgraphics import GraphicsWindow

def main():
    #winSpec = int(input("enter the size of the window: "))
    #subDiv = int(input("Enter number of subdivisions: "))

    winSpec = 600
    subDiv = 2

    win = GraphicsWindow(winSpec, winSpec)
    canvas = win.canvas()
    canvas.setColor("blue")

    triangle = [winSpec/2, 0, 0, winSpec, winSpec, winSpec]
    print('start:', triangle)
    drawGasket(triangle, subDiv, canvas)

    win.wait()

def drawGasket(triangle, subDiv, canvas):    
    if subDiv == 0:
        print(' draw:', triangle)
        canvas.drawPolygon(triangle)
    else:
        x1, y1, x2, y2, x3, y3 = triangle

        middle_1_2_x = (x1+x2)/2
        middle_1_2_y = (y1+y2)/2
        print('middle 1-2:', middle_1_2_x, middle_1_2_y)

        middle_2_3_x = (x2+x3)/2
        middle_2_3_y = (y2+y3)/2
        print('middle 2-3:', middle_2_3_x, middle_2_3_y)

        middle_1_3_x = (x1+x3)/2
        middle_1_3_y = (y1+y3)/2
        print('middle 1-3:', middle_1_3_x, middle_1_3_y)

        triangle1 = [x1, y1, middle_1_2_x, middle_1_2_y, middle_1_3_x, middle_1_3_y]
        drawGasket(triangle1, subDiv-1, canvas)

        triangle2 = [middle_1_2_x, middle_1_2_y, x2, y2, middle_2_3_x, middle_2_3_y]
        drawGasket(triangle2, subDiv-1, canvas)

        triangle3 = [middle_1_3_x, middle_1_3_y, middle_2_3_x, middle_2_3_y, x3, y3]
        drawGasket(triangle3, subDiv-1, canvas)

main()