""" fractal_tree.py Recursively draw a fractal tree with Python's turtle graphics, with slightly varying line width and colors. See ./tree.png for a screenshot. Tested with python 3.6.4 Jim Mahoney | cs.marlboro.college | April 2018 | MIT License """ import turtle, random def fraction(min=0.5, max=0.6): """ Return a random number between the given limits """ return min + (max-min) * random.random() def hue_twitch(hue): """ Return a slightly different color fraction (0.0 to 1.0) """ #print("hue = {}".format(hue)) return (hue + fraction(0.1, 0.2)) % 1 def newcolor(triple): """ Return a slightly modified color triple """ #print("triple={}".format(triple)) return (hue_twitch(triple[0]), hue_twitch(triple[1]), hue_twitch(triple[2])) # Create the turtle, point it upwards, and set its color to a triple. ted = turtle.Turtle() ted.left(90) ted.color((0, 0, 0)) def tree(length): """ Draw a fractal tree with a trunk of the given length. """ if length > 1: ted.width(length/32) ted.forward(length) ted.right(30) ted.pencolor( newcolor( ted.pencolor() ) ) tree(fraction() * length) ted.left(60) ted.pencolor( newcolor( ted.pencolor() ) ) tree(fraction() * length) ted.right(30) ted.back(length) if __name__ == '__main__': tree(128) ok = input("ok?")