## Maurer-Rosen ## mit Zufallsfarben ######################################## ## Größe des Fensters mit r0 = setzen ## ######################################## import random from Tkinter import * import math r0 = 350.0 mx = r0+25 my = r0+25 w0 = 2*int(r0)+30 h0 = 2*int(r0)+30 def die(event=0): sys.exit(0) def color(): col="#" for i in range(3): c = random.randrange(200) if random.random()<0.5: c /= 2 col=col+"%02x"%(c) return col def calc(phi,n): p1 = phi%360 p2 = (n*phi)%360 w = math.pi*p1/180.0 r = r0*math.sin(math.pi*p2/180.0) return (int(r*math.cos(w)+0.5),int(r*math.sin(w)+0.5)) def rose(): global root,lines n = random.randrange(1,360) d = random.randrange(1,360) values=cv.create_text(62,22, text="n="+str(n)+" d="+str(d),font=("Arial",10), fill="#000000", justify=LEFT) farbe = color() t = 0 c = 0 while c<=360: theta=t oldx,oldy = calc(theta,n) while 1: theta = (theta+d)%360 newx,newy = calc(theta,n) cv.itemconfigure(lines[c],fill=farbe) cv.coords(lines[c],mx+oldx,my+oldy,mx+newx,my+newy) # cv.update() c = c+1 if c>=360: break if theta==t: break oldx,oldy=newx,newy cv.update() t=t+1 cv.delete(values) def start(): global root,cv,lines root = Tk() root.title("Maurer-Rosen") cv = Canvas(root,width=w0,height=h0,borderwidth=10,background="#b0b0b0") root.bind("",die) cv.pack(expand=1,fill=NONE) cv.create_rectangle(10,10,2*mx+10,2*my+10,fill="#e8e8e8", outline="") root.update() lines = 361*[0] for l in range(361): lines[l]=cv.create_line(0,0,0,0) while 1: rose() cv.after(2000) root.update() root.update() root.after(1000) root.update() root.destroy() start()