Welcome, guest | Sign In | My Account | Store | Cart

This simple program can be used to compute and display a 2D fractal tree.

Python, 116 lines
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116``` ```#/usr/bin/env python import Tkinter, sys from math import sqrt, pi, atan2 from cmath import exp from optparse import OptionParser def get_parent_indices(i, j, nbran): return (i-1, j//nbran) class fractal_tree: def __init__(self, ratio=0.8, angle=pi/2., ngen=3, nbran=2): """ ratio: ratio of branch length with respect to previous generation angle: fan angle, how wide spread the branches will be (0

A fractal tree is constructed iteratively by growing new branches, which have a specific length ratio with respect to the parent branch (scaling factor). The fan angle, the number of new branches and number of generations are user controlled. The number of branches grows exponentially. Use ngen~10 for best results. The displayed tree exhibits self-similarity, i.e. the same patterns at different scales.

The code uses a dictionary to store the node coordinates as complex numbers and Tkinter for graphics. It is a good example of how Python's powerful data structures, combined with a vast body of modules can be used to write concise and user friendly code. The code was tested with Python 2.3 and 2.4.

You may also want to check out a similar recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/347736.

It is well known that when the fan angle is 120 deg, the branches will asymptotically touch if one sets the scaling factor to 0.6180339887498949, that is the inverse of the golden ratio. To see this, type

python fractal_tree.py -a 120 -r 0.6180339887498949 -n 15

 Created by Alexander Pletzer on Sun, 17 Apr 2005 (PSF)