Search code examples
pythonrecursioncanvasdraw

Creating a Sierpinski triangle


I'm attempting to create a Sierpinski triangle with the ezgraphics library using recursion. I imagine that there are several things that I have done wrong but I'm stuck as I am relatively new to coding. I'm not looking for someone to complete the code for me, just suggestions as to what to try next.

Here is the library I'm using: http://www.ezgraphics.org/ReferenceGuide/ReferenceGuide

And here is my rough code so far:

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)

Solution

  • You should draw only for subDiv == 0 and for other values you should calculate middle points and run again with three new triangles.

    You have to use () to divide sum - (points[0] + points[2]) /2. Without () you divide second point before adding points.


    BTW

    Shorter

     canvas.drawPolygon(points)
    

    You can assing points to variables which means something to make code more readable

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

    and then you can calculate middle 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
    

    before you create new triangles

     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]
    

    Result:

    enter image description here


    Full code:

    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()