epstopng
[PyX/mjg.git] / examples / tree.py
blob82cf823f7f86261b9dc9749935c30c03ee878ac1
1 # fractal tree
2 # contributed by Gerhard Schmid and André Wobst
4 from pyx import *
6 # base tree length
7 l = 5
9 # base transformations for the left, center, and right part of the tree
10 ltrafo = trafo.rotate(65).scaled(0.4).translated(0, l * 2.0 / 3.0)
11 ctrafo = trafo.rotate(-4).scaled(0.75).translated(0, l)
12 rtrafo = trafo.mirror(90).rotated(-65).scaled(0.35).translated(0, l)
14 def tree(depth):
15 "return transformations for a recursive tree of given depth"
16 r = [trafo.rotate(5)]
17 if depth > 0:
18 subtree = tree(depth - 1)
19 r.extend([t*ltrafo for t in subtree])
20 r.extend([t*ctrafo for t in subtree])
21 r.extend([t*rtrafo for t in subtree])
22 return r
24 c = canvas.canvas()
25 for t in tree(7):
26 # apply the transformation to a "sub"-canvas and insert it into the "main" canvas
27 c.insert(canvas.canvas(t).stroke(path.line(0, 0, 0, l)))
28 c.writetofile("tree", paperformat="a4")