Advertisement
fmasanori

Penrose tiling

Oct 24th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | None | 0 0
  1. #!/usr/bin/python
  2. """   Constructs two aperiodic penrose-tilings,
  3. consisting of kites and darts, by the method
  4. of inflation in six steps.
  5.  
  6. Starting points are the patterns "sun"
  7. consisting of five kites and "star"
  8. consisting of five darts.
  9.  
  10. For more information see:
  11. http://en.wikipedia.org/wiki/Penrose_tiling
  12. -------------------------------------------  
  13. """
  14. from turtle import *
  15. from math import cos, pi
  16. from time import clock, sleep
  17.  
  18. f = (5**0.5-1)/2.0   # (sqrt(5)-1)/2 -- goldener Schnitt
  19. d = 2 * cos(3*pi/10)
  20.  
  21. def kite(l):
  22.     fl = f * l
  23.     lt(36)
  24.     fd(l)
  25.     rt(108)
  26.     fd(fl)
  27.     rt(36)
  28.     fd(fl)
  29.     rt(108)
  30.     fd(l)
  31.     rt(144)
  32.  
  33. def dart(l):
  34.     fl = f * l
  35.     lt(36)
  36.     fd(l)
  37.     rt(144)
  38.     fd(fl)
  39.     lt(36)
  40.     fd(fl)
  41.     rt(144)
  42.     fd(l)
  43.     rt(144)
  44.  
  45. def inflatekite(l, n):
  46.     if n == 0:
  47.         px, py = pos()
  48.         h, x, y = int(heading()), round(px,3), round(py,3)
  49.         tiledict[(h,x,y)] = True
  50.         return
  51.     fl = f * l
  52.     lt(36)
  53.     inflatedart(fl, n-1)
  54.     fd(l)
  55.     rt(144)
  56.     inflatekite(fl, n-1)
  57.     lt(18)
  58.     fd(l*d)
  59.     rt(162)
  60.     inflatekite(fl, n-1)
  61.     lt(36)      
  62.     fd(l)
  63.     rt(180)
  64.     inflatedart(fl, n-1)
  65.     lt(36)
  66.  
  67. def inflatedart(l, n):
  68.     if n == 0:
  69.         px, py = pos()
  70.         h, x, y = int(heading()), round(px,3), round(py,3)
  71.         tiledict[(h,x,y)] = False
  72.         return
  73.     fl = f * l
  74.     inflatekite(fl, n-1)
  75.     lt(36)
  76.     fd(l)
  77.     rt(180)
  78.     inflatedart(fl, n-1)
  79.     lt(54)
  80.     fd(l*d)
  81.     rt(126)
  82.     inflatedart(fl, n-1)
  83.     fd(l)
  84.     rt(144)
  85.  
  86. def draw(l, n, th=2):
  87.     clear()
  88.     l = l * f**n
  89.     turtlesize(l/100.0, l/100.0, th)    
  90.     for k in tiledict:
  91.         h, x, y = k
  92.         setpos(x, y)
  93.         setheading(h)
  94.         if tiledict[k]:
  95.             shape("kite")
  96.             color("black", "green")
  97.         else:
  98.             shape("dart")
  99.             color("black", "red")
  100.         stamp()
  101.  
  102. def sun(l, n):
  103.     for i in range(5):
  104.         inflatekite(l, n)
  105.         lt(72)
  106.  
  107. def star(l,n):
  108.     for i in range(5):
  109.         inflatedart(l, n)
  110.         lt(72)
  111.  
  112. def makeshapes():
  113.     tracer(0)
  114.     begin_poly()
  115.     kite(100)
  116.     end_poly()
  117.     addshape("kite", get_poly())
  118.     begin_poly()
  119.     dart(100)
  120.     end_poly()
  121.     addshape("dart", get_poly())
  122.     tracer(1)
  123.    
  124. def start():
  125.     #winsize(800, 800)
  126.     reset()
  127.     ht()
  128.     pu()
  129.     makeshapes()
  130.     resizemode("user")
  131.    
  132. def test(l=200, n=4, fun=sun, th=2):
  133.     global tiledict
  134.     home()
  135.     setheading(0)
  136.     tiledict = {}
  137.     a = clock()
  138.     tracer(0)
  139.     fun(l, n)
  140.     b = clock()
  141.     draw(l, n, th)
  142.     tracer(1)
  143.     c = clock()
  144.     nk = len([x for x in tiledict if tiledict[x]])
  145.     nd = len([x for x in tiledict if not tiledict[x]])
  146.    
  147. def demo(fun=sun):
  148.     start()
  149.     for i in range(8):
  150.         a = clock()
  151.         test(300, i, fun)
  152.         b = clock()
  153.         t = b - a
  154.         if t < 4:
  155.             sleep(4 - t)
  156.        
  157. def main():
  158.     #title("Penrose-Parkettierung mit Kites und Darts.")
  159.     mode("logo")
  160.     demo(sun)
  161.     sleep(4)
  162.     demo(star)
  163.     pencolor("black")
  164.     goto(0,-200)
  165.     pencolor((1,0.9,0.7))
  166.     write("Please wait...",
  167.           align="center", font=('Arial Black', 36, 'bold'))
  168.     test(600, 8)
  169.     return "Done"
  170.  
  171. if __name__ == "__main__":
  172.     msg = main()
  173.     mainloop()
  174.    
  175.  
  176.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement