File: tree.py

package info (click to toggle)
pyx3 0.17-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,328 kB
  • sloc: python: 27,656; makefile: 225; ansic: 130; sh: 17
file content (33 lines) | stat: -rw-r--r-- 1,115 bytes parent folder | download | duplicates (3)
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
# fractal tree
# contributed by Gerhard Schmid and André Wobst

from pyx import *

# base tree length
l = 5

# base transformations for the left, center, and right part of the tree
ltrafo = trafo.rotate(65).scaled(0.4).translated(0, l * 2.0 / 3.0)
ctrafo = trafo.rotate(-4).scaled(0.75).translated(0, l)
rtrafo = trafo.mirror(90).rotated(-65).scaled(0.35).translated(0, l)

def tree(depth):
    "return transformations for a recursive tree of given depth"
    r = [trafo.rotate(5)]
    if depth > 0:
        subtree = tree(depth - 1)
        r.extend([t*ltrafo for t in subtree])
        r.extend([t*ctrafo for t in subtree])
        r.extend([t*rtrafo for t in subtree])
    return r

c = canvas.canvas()
for t in tree(7):
    # apply the transformation to a "sub"-canvas and insert it into the "main" canvas
    c.insert(canvas.canvas([t])).stroke(path.line(0, 0, 0, l))
    # note that there is a difference when only transforming the line as in:
    # c.stroke(path.line(0, 0, 0, l), [t])
    # The difference is, that the linewidth would not be scaled down.
c.writeEPSfile()
c.writePDFfile()
c.writeSVGfile()