from __future__ import division from visual import * from random import random leaf_count = 0 berry_count = 0 def ends(divs_left): return 0.5 / divs_left > random() # end: An x,y,z position! # It'll be a tuple, (x, y, z) # divs_left is just a positive integer def subdivide(end, divs_left, bushiness=1): global leaf_count, berry_count if divs_left < 1: # Draw a leaf! if random() < 0.1: sphere(pos=end, color=(1, 0, 0), radius=1 + random()) berry_count += 1 else: sphere(pos=end, color=(0, 1, 0), radius=2 + 2 * random()) leaf_count += 1 else: # Branch 1 b1end = (end[0] + divs_left * 2 * bushiness, end[1] + 5 + 10 * random(), end[2] + 20 * (.5 - random() * bushiness)) if ends(divs_left): curve(pos=[end, b1end], radius=.5) subdivide(b1end, 0) else: curve(pos=[end, b1end], radius=divs_left/2) subdivide(b1end, divs_left-1) # branch 2 b2end = (end[0] - divs_left * 2 * bushiness, end[1] + 5 + 10 * random(), end[2] + 20 * (.5 - random() * bushiness)) if ends(divs_left): curve(pos=[end, b2end], radius=.5) subdivide(b2end, 0) else: curve(pos=[end, b2end], radius=divs_left/2) subdivide(b2end, divs_left-1) for i in range(-300, 300, 100): for j in range(-300, 300, 100): subdivide((i, 0, j), 8, 1 + random()) print "Leaves: ", leaf_count, "Berries: ", berry_count