File: sierpinski.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 (28 lines) | stat: -rw-r--r-- 679 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
# 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()
c.writePDFfile()
c.writeSVGfile()