- #%%
- import numpy as np
- import matplotlib.pyplot as plt
- import matplotlib.animation as animation
- import time
- import random as rn
- xmin = -1.6
- xmax = 1.6
- ymin = -0.8
- ymax = 0.8
- N = 2000 #nombre de particules
- fig, ax = plt.subplots(figsize=(xmax*6.5,ymax*6.5))
- ax.set(xlim=(xmin, xmax))
- ax.set(ylim=(ymin,ymax))
- scat = ax.scatter([0,0],[0,0], s=5, c='black')
- k = 2*np.pi
- dt = 0.01
- v = 1 # vitesse des particules
- class Particule:
- def __init__(self, x=0, y=0):
- self.px = rn.uniform(-1.6, -1.4)
- self.py = rn.uniform(ymin, ymax)
- self.dirx = rn.uniform(0,2)
- self.diry = rn.uniform(0,2)
- self.t = time.time()
- class ParticuleManager:
- def __init__(self):
- self.to = time.time()
- def move(self, p):
- t = time.time() - p.t
- self.setNextPos(p, t)
- if t > 0.1:
- p.dirx = rn.uniform(0,2)
- p.diry = rn.uniform(0,2)
- p.t = time.time()
- def setNextPos(self, p, t):
- nextposx = -99
- nextposy = -99
- while (nextposx > xmax or nextposx < xmin) or (nextposy > ymax or nextposy < ymin):
- p.dirx = rn.uniform(0,2)
- nextposx = p.px + v*t*np.cos(k*p.dirx)
- p.diry = rn.uniform(0,2)
- nextposy = p.py + v*t*np.sin(k*p.diry)
- p.px = nextposx
- p.py = nextposy
- man = ParticuleManager()
- particules = []
- for i in range (0,N):
- particules.append(Particule())
- positions = [[0]*2 for i in range(N)]
- for i in range(N):
- positions[i][0] = particules[i].px
- positions[i][1] = particules[i].py
- def animate(i):
- for i in range(N):
- positions[i][0] = particules[i].px
- positions[i][1] = particules[i].py
- scat.set_offsets(positions)
- for p in particules:
- man.move(p)
- ani = animation.FuncAnimation(fig, animate, interval=10, frames=500, repeat=True)
- plt.show()
- #%%
Raw Paste