File: sierpinski.py

package info (click to toggle)
pyx 0.9-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,064 kB
  • ctags: 2,665
  • sloc: python: 15,205; makefile: 142; ansic: 131
file content (27 lines) | stat: -rw-r--r-- 686 bytes parent folder | download | duplicates (7)
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
# Sierpinski triangle
# contributed by Gerhard Schmid

from math import sqrt
from pyx import *

# triangle geometry
l = 10
h = 0.5 * sqrt(3) * l

# base triangle path
p = path.path(path.moveto(0, 0),
              path.lineto(l, 0),
              path.lineto(0.5 * l, h),
              path.closepath())

for i in range(6):
    # path is scaled down ...
    p = p.transformed(trafo.scale(0.5))
    # ... and three times plotted (translated accordingly)
    p += ( p.transformed(trafo.translate(0.5 * l, 0)) +
           p.transformed(trafo.translate(0.25 * l, 0.5 * h)))

c = canvas.canvas()
c.stroke(p, [style.linewidth.Thin])
c.writeEPSfile("sierpinski")
c.writePDFfile("sierpinski")