Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_6_sided_snowflakes.py
- from math import cos,sin,pi,radians,degrees,atan2
- import random
- ri=random.randint
- try:
- import tkinter as tk
- except:
- import Tkinter as tk
- root=tk.Tk()
- root.title("Tk 6 Sided Snowflakes")
- ww=640
- hh=640
- wc=ww/2
- hc=hh/2
- class Cv(): 0
- cv=Cv()
- xy={}
- span=ww/2-20
- curve=pi/12
- mouse=[ww/2,hh/2]
- def find_xy(d, theta, x0=0, y0=0):
- theta_rad = pi/2 - radians(theta)
- return x0 + d*cos(theta_rad), y0 + d*sin(theta_rad)
- def dist__(a,b):
- return (abs(a)**2+abs(b)**2)**0.5
- def degrees__(x1,y1,x2,y2):
- return (degrees(atan2((x2-x1),(y2-y1)*-1))+90)%360 if (y2-y1) else 0.0
- for y in range(span):
- for ym in (-y,y):
- for x in range(span):
- for xm in (-x,x):
- deg=int(degrees__(0, 0, xm, ym))
- distance=int(dist__(xm, ym))
- if distance > span:
- break
- if deg <= 30 and xm < 1:
- xy[xm,ym]=distance,deg
- arm=[]
- ttt=range(20,100,5)
- for y in ttt:
- for ym in (-y,y):
- for x in ttt:
- for xm in (-x,x):
- arm+=[(xm,ym)]
- canvas=tk.Canvas(root,width=ww,height=hh,bg="dark blue")
- canvas.pack(fill="both",expand=True)
- def mmove(event):
- mouse[:]=[z*1.0 for z in (event.x,event.y)]
- canvas.bind('<Motion>',mmove)
- def rgb2hex(r,g,b):
- return '#{:02x}{:02x}{:02x}'.format(r,g,b)
- def rndrgb():
- r,g,b=([ri(0, 255) for _ in 'rgb'])
- return rgb2hex(r,g,b)
- def plot():
- canvas.delete(tk.ALL)
- snowflake=[(ri(150,290), 320)]
- px,py=random.choice(xylist[-20:])
- count = ri(9,18)
- random.shuffle(arm)
- for x,y in arm:
- t=x+px,y+py
- if t in xylist:
- snowflake+=[t]
- px,py=t
- if not count:
- break
- count -= 1
- snowflake+=[(60,320),(ri(90,240)-span,-ri(5,200))]
- for t in range(0,360,60):
- aaa=[]
- bbb=[]
- for x,y in snowflake:
- try:
- dist,angle=xy[x-wc,y-hc]
- except:
- angle=degrees__(0, 0, x-wc, y-hc)
- dist=dist__(x, y)
- t%=360
- xxx,yyy=find_xy(dist,t+angle)
- aaa+=[(xxx+wc,yyy+hc)]
- xxx,yyy=find_xy(dist,t-angle)
- bbb+=[(xxx+wc,yyy+hc)]
- canvas.create_polygon(aaa,width=3,outline=cv.color,fill='')
- canvas.create_polygon(bbb,width=3,outline=cv.color,fill='')
- '''
- '''
- canvas.update()
- xylist=list(xy)
- xylist.sort(key=sum)
- xylist=[(x2+wc,y2+hc) for x2,y2 in xylist]
- xyL=len(xylist)
- cv.color="white"
- def tick():
- plot()
- canvas.after(1000,tick)
- tick()
- '''
- '''
- tk.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement