here2share

# Tk_basic_spirograph.py

Oct 20th, 2021 (edited)
396
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Tk_basic_spirograph.py
  2.  
  3. import math
  4. import random
  5. from Tkinter import *
  6.  
  7. ww = hh = 680
  8. ctr = ww/2
  9.  
  10. root = Tk()
  11. root.title("# Tk_basic_spirograph")
  12.  
  13. root.geometry("%dx%d+0+0"%(ww,hh))
  14.  
  15. canvas = Canvas(root, width=ww, height=hh)
  16. canvas.pack()
  17.  
  18. def e(n):
  19.     return n if n else 1
  20.  
  21. def drawSpiros(x, y, l):
  22.     def draw():
  23.         canvas.delete('all')
  24.         canvas.create_line(xy[::-1], fill='red', width=1)
  25.         canvas.update()
  26.     def pxy():
  27.         px = R*(1-k)*(math.cos(a) + l*k*math.cos((1-k)* a/e(k)))
  28.         py = R*(1-k)*(math.sin(a) - l*k*math.sin((1-k)* a/e(k)))
  29.         return -px,-py
  30.    
  31.     a = 0.0
  32.     l *= 0.1
  33.     r = 100
  34.     incr = 50
  35.     t = [5,0.5,0.05,0.001]
  36.     end = t[-1]
  37.     p = 0
  38.     while 1:
  39.         R = r*1.0*sides
  40.         k = r/e(R)
  41.         px,py = pxy()
  42.         if abs(px) <= 300:
  43.             if incr == end:
  44.                 break
  45.             incr = t.pop(0)
  46.             r = p
  47.         p = r
  48.         r -= incr
  49.         # print incr
  50.  
  51.     xy = []
  52.     for i in range(0, 360+10):
  53.         a = math.radians(i)
  54.         px,py = pxy()
  55.         xy += [px + x, py + y]
  56.     draw()
  57.  
  58. for sides in range(3,25):
  59.     print sides,'SIDES'
  60.     for value in range(2,80+sides*50):
  61.         drawSpiros(ctr, ctr, value**1.2)
RAW Paste Data