here2share

# Tk_basic_vectoring.py

Oct 31st, 2020
1,358
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Tk_basic_vectoring.py
  2.  
  3. from Tkinter import *
  4. import random
  5. import math
  6. import time
  7.  
  8. WW = 1200
  9. HH = 680
  10. root = Tk()
  11. root.geometry(str(WW)+"x"+str(HH)+"+5+5")
  12. canvas = Canvas(root, width=WW, height=HH)
  13. canvas.pack()
  14.  
  15. r = 300
  16. vectors = 1000
  17. midx,midy = 600,360
  18. TWO_PI = math.pi*2
  19.  
  20. def radial_ref(angles=360): # 2500
  21.     d = {}
  22.     d2bn = 0
  23.     if angles:
  24.         d2bn = 360.0/angles
  25.     for angle in range(angles):        
  26.         a = angle
  27.         a = a*d2bn
  28.         x = math.sin(math.radians(a))
  29.         y = math.cos(math.radians(a))
  30.         d[angle] = x,y
  31.     print len(d)
  32.     return d
  33.  
  34. class Slider:
  35.     def __init__(self, master=None, label='', value=0.0, min=0.0, max=100):
  36.         slist.append(0)
  37.         slist[-1] = DoubleVar()
  38.         s = Scale(  master,
  39.                     label=label,
  40.                     variable=slist[-1],
  41.                     from_=min*1.0,
  42.                     to=max*1.0,
  43.                     orient=HORIZONTAL,
  44.                     length=max)
  45.         slist[-1].set(value)
  46.         s.place(x=(len(slist)-1)*120,y=0)
  47.    
  48. slist = []
  49. Slider(canvas, label='vector points', max=100, min=3)
  50. Slider(canvas, label='radius', max=1000, min=10)
  51.  
  52. timer = 0
  53. p = p2 = 0
  54. while 1:
  55.     vectors = int(slist[0].get())
  56.     radius = int(slist[1].get())
  57.     if p <> [vectors,radius]:
  58.         timer = time.time()+0.3
  59.         p = [vectors,radius]
  60.     if p2 <> vectors and timer < time.time():
  61.         PERIM = radial_ref(vectors)
  62.         canvas.delete('all')
  63.         points = []
  64.         for i in range(vectors):
  65.             x,y = PERIM[i]
  66.             x *= radius
  67.             y *= radius
  68.             points += [WW-320+x,midy+y]
  69.         canvas.create_line(points+[points[:2]],fill='blue')
  70.         canvas.create_rectangle(midx-70,midy-20,midx+70,midy+20,outline='yellow',fill='yellow')
  71.         canvas.create_text(midx,midy,text=vectors,font='arial 30',fill='blue')
  72.         p2 = [vectors,radius]
  73.     canvas.update()
  74.  
RAW Paste Data