Sunday, 2 February 2014

Turtle Graphics remind me of Logo

I remember learning Logo in primary school...

(http://www.annehelmond.nl/2007/11/06/a-discussion-with-alexander-galloway-on-protocol-university-of-amsterdam/)

It was pretty similar to what a python turtle can do. Well, it was a programming language designed for kids, though.

I edited the code so that the turtle draws something else similar like:
My code reads as follows:
import turtle
import copy
COLORS = ('red', 'green', 'blue', 'yellow', 'orange')

def tree_burst(level, base, turtle):
    """Draw a symmetrical ternary tree of height level with initial edges
    of length base.
    """
    if not level: # Pythonese for level == 0
        #turtle.forward(base)
        pass
    else:
        turtle_list = []
        for h in range(5):
            turtle_list.append(turtle.clone())
            turtle_list[h].color(COLORS[h])
            turtle_list[h].setheading(72 * h)
            turtle_list[h].forward(base)
            tree_burst(level - 1, base / 2, turtle_list[h])

if __name__ == '__main__':
    T = turtle.Turtle()
    T.color('red')
    T.speed('slow')
#    T.hideturtle()
    tree_burst(3, 128, T)

I added two more items to the 'color' tuple so that there are five items which last until the index 4 is reached. I also adjusted the angle by which the turtle turns before drawing each section. Playing around with it actually helps understanding how the code actually works.

No comments:

Post a Comment