Advertisement
here2share

# Tk_Penrose.py

Nov 14th, 2020
1,346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. # Tk_Penrose.py -- to quite an extent
  2.  
  3. zeta = [(-1)**(.4*i) for i in range(5)]
  4.  
  5. def rhombus_at_intersection(gamma, r, s, kr, ks):
  6.     """
  7.    Find the rhombus corresponding to a pentagrid intersection point.
  8.    Generates four complex numbers, giving the vertices of the rhombus
  9.    corresponding in the pentagrid described by parameters gamma to the
  10.    intersection of the two lines with equations:
  11.       (z/zeta[r]).real + gamma[r] == kr
  12.    and
  13.       (z/zeta[s]).real + gamma[s] == ks
  14.    The vertices traverse the perimeter of the rhombus in order (though that
  15.    order may be either clockwise or counterclockwise).
  16.    """
  17.     z0 = 1j*(zeta[r]*(ks-gamma[s]) - zeta[s]*(kr-gamma[r])) / zeta[s-r].imag
  18.     k = [0--((z0/t).real+p)//1 for t, p in zip(zeta, gamma)]
  19.     for k[r], k[s] in [(kr, ks), (kr+1, ks), (kr+1, ks+1), (kr, ks+1)]:
  20.         yield sum(x*t for t, x in zip(zeta, k))
  21.  
  22. def tiling(gamma, size):
  23.     """
  24.    Find all rhombuses corresponding to a limited set of intersections.
  25.    """
  26.     for r in range(5):
  27.         for s in range(r+1, 5):
  28.             for kr in range(-size, size+1):
  29.                 for ks in range(-size, size+1):
  30.                     color = "cyan" if (r-s)**2%5==1 else "green"
  31.                     yield rhombus_at_intersection(gamma, r, s, kr, ks), color
  32.  
  33. def to_canvas(vertices, scale, center):
  34.     """
  35.    Transform complex values to canvas points.
  36.    Generates an interleaved list (x0, y0, x1, y1, ...), suitable
  37.    for passing to Canvas.create_polygon.
  38.    """
  39.     for z in vertices:
  40.         w = center + scale*z
  41.         yield from (w.real, w.imag)
  42.  
  43. def main():
  44.     from tkinter import Canvas, mainloop, Tk
  45.     gamma, size = [0.3, 0.2, -0.1, -0.4, 0.0], 11
  46.     width, height, scale, center = 800, 800, 20, 400+400j
  47.     w = Canvas(Tk(), width=width, height=height)
  48.     w.pack()
  49.     for rhombus, color in tiling(gamma, size):
  50.         coords = to_canvas(rhombus, scale, center)
  51.         w.create_polygon(*coords, fill=color, outline="black")
  52.     mainloop()
  53.  
  54. if __name__ == '__main__':
  55.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement