penrose.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/usr/bin/env python3
  2. """ xturtle-example-suite:
  3. xtx_kites_and_darts.py
  4. Constructs two aperiodic penrose-tilings,
  5. consisting of kites and darts, by the method
  6. of inflation in six steps.
  7. Starting points are the patterns "sun"
  8. consisting of five kites and "star"
  9. consisting of five darts.
  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 perf_counter as clock, sleep
  17. f = (5**0.5-1)/2.0 # (sqrt(5)-1)/2 -- golden ratio
  18. d = 2 * cos(3*pi/10)
  19. def kite(l):
  20. fl = f * l
  21. lt(36)
  22. fd(l)
  23. rt(108)
  24. fd(fl)
  25. rt(36)
  26. fd(fl)
  27. rt(108)
  28. fd(l)
  29. rt(144)
  30. def dart(l):
  31. fl = f * l
  32. lt(36)
  33. fd(l)
  34. rt(144)
  35. fd(fl)
  36. lt(36)
  37. fd(fl)
  38. rt(144)
  39. fd(l)
  40. rt(144)
  41. def inflatekite(l, n):
  42. if n == 0:
  43. px, py = pos()
  44. h, x, y = int(heading()), round(px,3), round(py,3)
  45. tiledict[(h,x,y)] = True
  46. return
  47. fl = f * l
  48. lt(36)
  49. inflatedart(fl, n-1)
  50. fd(l)
  51. rt(144)
  52. inflatekite(fl, n-1)
  53. lt(18)
  54. fd(l*d)
  55. rt(162)
  56. inflatekite(fl, n-1)
  57. lt(36)
  58. fd(l)
  59. rt(180)
  60. inflatedart(fl, n-1)
  61. lt(36)
  62. def inflatedart(l, n):
  63. if n == 0:
  64. px, py = pos()
  65. h, x, y = int(heading()), round(px,3), round(py,3)
  66. tiledict[(h,x,y)] = False
  67. return
  68. fl = f * l
  69. inflatekite(fl, n-1)
  70. lt(36)
  71. fd(l)
  72. rt(180)
  73. inflatedart(fl, n-1)
  74. lt(54)
  75. fd(l*d)
  76. rt(126)
  77. inflatedart(fl, n-1)
  78. fd(l)
  79. rt(144)
  80. def draw(l, n, th=2):
  81. clear()
  82. l = l * f**n
  83. shapesize(l/100.0, l/100.0, th)
  84. for k in tiledict:
  85. h, x, y = k
  86. setpos(x, y)
  87. setheading(h)
  88. if tiledict[k]:
  89. shape("kite")
  90. color("black", (0, 0.75, 0))
  91. else:
  92. shape("dart")
  93. color("black", (0.75, 0, 0))
  94. stamp()
  95. def sun(l, n):
  96. for i in range(5):
  97. inflatekite(l, n)
  98. lt(72)
  99. def star(l,n):
  100. for i in range(5):
  101. inflatedart(l, n)
  102. lt(72)
  103. def makeshapes():
  104. tracer(0)
  105. begin_poly()
  106. kite(100)
  107. end_poly()
  108. register_shape("kite", get_poly())
  109. begin_poly()
  110. dart(100)
  111. end_poly()
  112. register_shape("dart", get_poly())
  113. tracer(1)
  114. def start():
  115. reset()
  116. ht()
  117. pu()
  118. makeshapes()
  119. resizemode("user")
  120. def test(l=200, n=4, fun=sun, startpos=(0,0), th=2):
  121. global tiledict
  122. goto(startpos)
  123. setheading(0)
  124. tiledict = {}
  125. tracer(0)
  126. fun(l, n)
  127. draw(l, n, th)
  128. tracer(1)
  129. nk = len([x for x in tiledict if tiledict[x]])
  130. nd = len([x for x in tiledict if not tiledict[x]])
  131. print("%d kites and %d darts = %d pieces." % (nk, nd, nk+nd))
  132. def demo(fun=sun):
  133. start()
  134. for i in range(8):
  135. a = clock()
  136. test(300, i, fun)
  137. b = clock()
  138. t = b - a
  139. if t < 2:
  140. sleep(2 - t)
  141. def main():
  142. #title("Penrose-tiling with kites and darts.")
  143. mode("logo")
  144. bgcolor(0.3, 0.3, 0)
  145. demo(sun)
  146. sleep(2)
  147. demo(star)
  148. pencolor("black")
  149. goto(0,-200)
  150. pencolor(0.7,0.7,1)
  151. write("Please wait...",
  152. align="center", font=('Arial Black', 36, 'bold'))
  153. test(600, 8, startpos=(70, 117))
  154. return "Done"
  155. if __name__ == "__main__":
  156. msg = main()
  157. mainloop()