# fractalTree.py
from Processing import *

class FractalTree:
    def __init__(self, length, depth):
        self.length = length
        self.left, self.right = None, None

        # Not leaf. Grow further.
        if depth > 1:
            depth -= 1
            self.left  = FractalTree(0.6*length, depth)
            self.right = FractalTree(0.6*length, depth)

    # Recursively draw tree
    def draw(self, angle):
        stroke(0)
        line(0, 0, 0, self.length)
        if self.left != None and self.right != None:
            translate(0, self.length)
            pushMatrix()
            rotate( radians(angle) )
            self.left.draw(angle)
            popMatrix()
            pushMatrix()
            rotate( radians(-angle) )
            self.right.draw(angle)
            popMatrix()

    # Recursively count the depth of the tree
    def countBranches(self):
        if self.left == None or self.right == None:
            return 1
        else:
            countLeft = self.left.countBranches()
            countRight = self.right.countBranches()
            return 1 + countLeft + countRight

window(600, 600)
background(255)

f = FractalTree(-200, 10)

translate(300, 600)
f.draw(50)

print(f.countBranches())
