more details
[PyX/mjg.git] / gallery / path / sierpinski.py
blob38a177577e7cadb7279b5c63e79cb5c9823fda86
1 # Sierpinski triangle
2 # contributed by Gerhard Schmid
4 from math import sqrt
5 from pyx import *
7 # triangle geometry
8 l = 10
9 h = 0.5 * sqrt(3) * l
11 # base triangle path
12 p = path.path(path.moveto(0, 0),
13 path.lineto(l, 0),
14 path.lineto(0.5 * l, h),
15 path.closepath())
17 for i in range(6):
18 # path is scaled down ...
19 p = p.transformed(trafo.scale(0.5))
20 # ... and three times plotted (translated accordingly)
21 p += ( p.transformed(trafo.translate(0.5 * l, 0)) +
22 p.transformed(trafo.translate(0.25 * l, 0.5 * h)))
24 c = canvas.canvas()
25 c.stroke(p, [style.linewidth.Thin])
26 c.writeEPSfile("sierpinski")
27 c.writePDFfile("sierpinski")