so i was bored one night and made this little tree simulation thing. took like an hour, thought it was cool, then forgot about it for months in my trash folder.
later i was working on pathforge (interactive fiction tool) and spent days trying to figure out tree layouts for story nodes. nothing was working.
then i remembered that old file...
here's the core part:
class Branch:
def __init__(self, start, angle, length, depth):
self.start = start
self.angle = angle
self.length = 0
self.target_length = length
self.depth = depth
self.finished = False
self.children = []
self.static = False
def grow(self):
if self.length < self.target_length:
self.length += GROWTH_SPEED
else:
if not self.finished and self.depth < MAX_DEPTH:
self.finished = True
end = self.get_end()
num_branches = random.choice([2, 3])
for _ in range(num_branches):
delta = random.uniform(-BRANCH_ANGLE, BRANCH_ANGLE)
new_length = self.target_length * random.uniform(0.8, BRANCH_FACTOR)
if new_length >= MIN_BRANCH_LENGTH:
child = Branch(end, self.angle + delta, new_length, self.depth + 1)
self.children.append(child)
basically each branch grows and spawns kids when it's done. random angles make it look natural.
ended up using this for pathforge's tree layout and it works pretty well.
Top comments (0)