User:Thierry Dugnolle/Infinite Binary Tree

print("Binary tree")

from PIL import Image, ImageDraw
import math

imHeight = 400
imWidth=2* imHeight

def xTOi(x):
    return math.floor((x + 2.0) * imHeight / 2.0)


def yTOi(y):
    return math.floor((2.0-y) * imHeight / 2.0)

lineWidth=5
def draw2branches(xroot, yroot, length, depth):
    draw.line((xTOi(xroot), yTOi(yroot), xTOi(xroot - length), yTOi(yroot + length)), (255, 0, 0), lineWidth)
    draw.line((xTOi(xroot), yTOi(yroot), xTOi(xroot + length), yTOi(yroot + length)), "green", lineWidth)
    if depth > 1:
        draw2branches(xroot - length, yroot + length, length / 2.0, depth - 1)
        draw2branches(xroot + length, yroot + length, length / 2.0, depth - 1)

imNumber=200
for imnum in range(imNumber):
    im = Image.new("RGB", (imWidth, imHeight), "moccasin") 
    draw = ImageDraw.Draw(im)
    length = pow(2.0, imnum / imNumber)
    depth=10
    draw2branches(-2.0*length,2.0-2.0*length,length,depth)
    draw2branches(+2.0*length,2.0-2.0*length,length,depth)
    im.save("BT"+str(100+imnum)+".bmp")

print("Good bye")