Advertisement
fmasanori

rhombi

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