comment style; line length adjustment; 8 bit clean comments
[PyX/mjg.git] / examples / tree.py
blobcd4aea840c08a26c27129de1857d7e3b53e9e024
1 # fractal tree
2 # contributed by Gerhard Schmid and André Wobst
4 # extend module search path - not needed when PyX is installed properly
5 import sys; sys.path[:0] = [".."]
7 from pyx import *
9 # base tree length
10 l = 5
12 # base transformations for the left, center, and right part of the tree
13 ltrafo = trafo.rotate(65).scaled(0.4).translated(0, l * 2.0 / 3.0)
14 ctrafo = trafo.rotate(-4).scaled(0.75).translated(0, l)
15 rtrafo = trafo.mirror(90).rotated(-65).scaled(0.35).translated(0, l)
17 def tree(depth):
18 "return transformations for a recursive tree of given depth"
19 r = [trafo.rotate(5)]
20 if depth > 0:
21 subtree = tree(depth - 1)
22 r.extend([t*ltrafo for t in subtree])
23 r.extend([t*ctrafo for t in subtree])
24 r.extend([t*rtrafo for t in subtree])
25 return r
27 c = canvas.canvas()
28 for t in tree(7):
29 # apply the transformation to a "sub"-canvas and insert it into the "main" canvas
30 c.insert(canvas.canvas(t).stroke(path.line(0, 0, 0, l)))
31 c.writetofile("tree", paperformat="a4")