#!/usr/bin/env python #------------------------------------------------------------------------------ # particelle2.py - by C.Fin - Python2&3 #------------------------------------------------------------------------------ try: import tkinter as tk except ImportError: import Tkinter as tk from random import randint from math import hypot #------------------------------------------------------------------------------ WID = HEI = 500 # dimensioni canvas N_PARTICELLE = 40 # numero di particelle F_PARTICELLE = 500.0 # forza respingente tra particelle RAGGIO = 5 # raggio particelle SMORZAMENTO = 0.99 # fattore smorzamento del moto #------------------------------------------------------------------------------ class Particella: def __init__ (self, item): area = int(0.3 * WID) self.x = randint(-area, area) + WID/2.0 self.y = randint(-area, area) + HEI/2.0 self.vx = randint(-1, 1) self.vy = randint(-1, 1) self.item = item #------------------------------------------------------------------------------ def calcola_particelle(forzac, part): #------calcola contributo campo esterno for p in part: dx = p.x - WID/2 dy = HEI/2 - p.y dist = hypot(dx, dy) distc = WID/2 - dist forza= -forzac / (distc * distc) * 360 forzax = forza * dx / dist forzay = -forza * dy / dist p.vx += forzax p.vy += forzay #------calcola contributo altre particelle if len(part) > 1: for i in range(len(part)): for j in range(len(part)): if j != i: dx = part[i].x - part[j].x dy = part[i].y - part[j].y dist = hypot(dx, dy) if dist < 12: dist = 12 # limita forza repulsiva forza = -F_PARTICELLE / (dist * dist) forzax = -forza * dx / dist forzay = -forza * dy / dist part[i].vx += forzax part[i].vy += forzay #------aggiorna posizioni for p in part: p.vx *= SMORZAMENTO p.vy *= SMORZAMENTO p.x += p.vx p.y += p.vy #------------------------------------------------------------------------------ def disegna_particelle(canvas, part): for p in part: canvas.coords(p.item, p.x-RAGGIO, p.y-RAGGIO, p.x+RAGGIO, p.y+RAGGIO) #------------------------------------------------------------------------------ class GUI: def __init__(self): self.form = tk.Tk() self.form.title("Particelle") self.form.resizable(False, False) self.frame1 = tk.Frame(self.form, width=100, relief="raised", bd=2) self.frame1.pack(side="left", expand=1, fill="y") self.canvas = tk.Canvas( self.form, highlightthickness=0, width=WID, height=HEI, bg="white",) self.canvas.pack(side="left", expand=1, fill="both") self.scale1 = tk.Scale(self.frame1, from_=100, to=10) self.scale1.pack(expand=1, padx=8) self.campo1 = self.canvas.create_oval( (10, 10, WID-10, HEI-10), outline="red", width=0) #------------------------------------------------------------------------------ class Particelle: def __init__ (self): self.f_campo = 1.0 self.gui = GUI() self.gui.scale1.configure(command=self.chgscale) self.p = [] for _ in range(N_PARTICELLE): item = self.gui.canvas.create_oval( (0, 0, 0, 0), fill="green", outline="black") self.p.append(Particella(item)) def chgscale(self, valore): self.f_campo = float(valore) self.gui.canvas.itemconfig( self.gui.campo1, width=int(self.f_campo)/10) def animazione(self): self.gui.form.after(30, self.animazione) calcola_particelle(self.f_campo, self.p) disegna_particelle(self.gui.canvas, self.p) def start(self): self.animazione() self.gui.form.mainloop() #------------------------------------------------------------------------------ app = Particelle() app.start()