User:Thierry Dugnolle/The whole infinite tree

print("Binary tree")

from PIL import Image, ImageDraw
import math

imHeight = 400
imWidth = 2 * imHeight

# In real coordinates the screen is a rectangle of width 4 and of height 2.
# The point (0,0) is at the bottom of the screen and at the middle of its width.
# The vertical axis is oriented upward, the horizontal one, rightward.
# The two functions xTOi and yTOi convert real coordinates to pixel coordinates.
def xTOi(x):
    return math.floor((x + 2.0) * imHeight / 2.0)
def yTOi(y):
    return math.floor((2.0 - y) * imHeight / 2.0)

# xRoot and yRoot are the real coordinates of the root of the tree or of one of its subtrees.
# length is both the horizontal and vertical deviation of the two first lines from the root.
# depth is the number of steps to draw a tree or a subtree.
lineWidth = 5
branchColor="black"
def drawBranches(xRoot, yRoot, length, depth):
    draw.line((xTOi(xRoot), yTOi(yRoot), xTOi(xRoot - length), yTOi(yRoot + length)), branchColor, lineWidth)
    draw.line((xTOi(xRoot), yTOi(yRoot), xTOi(xRoot + length), yTOi(yRoot + length)), branchColor, lineWidth)
    if depth > 1:
        drawBranches(xRoot - length, yRoot + length, length / 2.0, depth - 1)
        drawBranches(xRoot + length, yRoot + length, length / 2.0, depth - 1)

im = Image.new("RGB", (imWidth, imHeight), "white")
draw = ImageDraw.Draw(im)

drawBranches(0.0, 0.0, 1.0, 10)

im.save("Tree.gif")

print("Good bye")