Recursive Tree example



  • 0_1515499067926_tree.jpg

    # define a tree function
    def tree(h, levels):
        """This function is recursive: it calls itself."""
        line((0, 0), (0, h))  # draw a branch
        if levels <= 1:
            return  # we've drawn all levels, stop
        # branch into two random angles
        for angle in [random() * 30, random() * -30]:
            # use "with savedState()" to keep the transformations local
            with savedState():
                # move the origin to the tip of the branch
                translate(0, h)
                rotate(angle)  # wibble wobble
                scale(0.65 + 0.25 * random())  # next branch will be smaller
                tree(h, levels - 1) # draw another tree at the tip of the branch
    
    # set the canvas size
    size(600, 600)
    # move the origin to the bottom middle of the canvas,
    # this is where the base of the tree will be
    translate(width() / 2, 30)
    
    # set some paint properties
    fill(None)
    stroke(0)
    strokeWidth(10)
    lineCap("round")
    
    # draw a tree!
    tree(100, 10)  # be careful with higher level values, it may take forever to complete!
    


  • Fun fact: this script will also be able to draw a beach ball if to tell it to draw branches 25 levels deep!



  • @roel But if you hit command-period frantically enough, it will still terminate!



  • 0_1515594698280_Bildschirmfoto 2018-01-10 um 15.31.22.png
    This is 20 levels deep. It took a while 🙂


Log in to reply
 

Looks like your connection to DrawBot Forum was lost, please wait while we try to reconnect.