- import numpy as np
- import matplotlib.pyplot as plt
- import matplotlib.animation as animation
- import time
- xmin = -1
- xmax = 1
- ymin = -1
- ymax = 1
- class Emetteur:
- def __init__(self, f=0.8, v=0):
- self.x = 0
- self.y = 0
- self.f = f
- self.t = time.time()
- self.v = v
- class Vague:
- def __init__(self, x=0, y=0, c=0.2):
- self.to = time.time()
- self.tmax = 3
- self.R = 0
- self.centerx = x
- self.centery = y
- self.c = c
- def update(self, dt):
- self.R = self.R + self.c*dt
- k = 2*np.pi
- v = 0.05
- ech = 18
- angle = np.linspace(0, 2, ech)
- class AnimatedScatter(object):
- def __init__(self, numpoints=72):
- self.numpoints = numpoints
- self.xmin = -1.5
- self.xmax = 1.5
- self.ymin = -1
- self.ymax = 1
- self.t = time.time()
- self.to = time.time()
- self.fig, self.ax = plt.subplots(figsize=(xmax * 6*2, ymax * 6))
- self.angles = np.linspace(0,2, self.numpoints)
- self.lines = []
- self.vagues = []
- self.emetteur = Emetteur()
- self.nbvagues = 0
- self.ax.axis([self.xmin, self.xmax, self.ymin, self.ymax])
- self.anis = animation.FuncAnimation(self.fig, self.updates, interval=18,
- init_func=self.setup_plots, blit=False)
- self.anil = animation.FuncAnimation(self.fig, self.updatel, interval=18,
- init_func=self.setup_plotl, blit=False)
- def setup_plots(self):
- self.scat = self.ax.scatter([0], [0])
- return self.scat
- def updates(self, i):
- dt = time.time() - self.t
- if time.time() - self.to > 4:
- self.emetteur.v = 0.6
- self.emetteur.x = self.emetteur.x + 0.01 * self.emetteur.v
- self.scat.set_offsets([self.emetteur.x, self.emetteur.y])
- return self.scat,
- def setup_plotl(self):
- for line in self.lines:
- line.set_data([], [])
- return self.lines
- def updatel(self, i):
- dtphy = time.time() - self.t
- dt = time.time() - self.emetteur.t
- if dt > self.emetteur.f:
- self.emetteur.t = time.time()
- self.lines.append(self.ax.plot([],[], lw=1)[0])
- self.vagues.append(Vague(self.emetteur.x, self.emetteur.y))
- self.nbvagues = self.nbvagues + 1
- for it in range(self.nbvagues):
- self.vagues[it].update(dtphy)
- self.vagues[it].x = self.datax(self.vagues[it].R) + self.vagues[it].centerx
- self.vagues[it].y = self.datay(self.vagues[it].R) + self.vagues[it].centery
- self.lines[it].set_data(self.vagues[it].x, self.vagues[it].y)
- self.t = time.time()
- return self.lines
- def datax(self, R):
- x = np.linspace(0,0,self.numpoints)
- for i in range(self.numpoints):
- x[i] = R*np.cos(self.angles[i]*2*np.pi)
- return x
- def datay(self, R):
- y = np.linspace(0,0,self.numpoints)
- for i in range(self.numpoints):
- y[i] = R*np.sin(self.angles[i]*2*np.pi)
- return y
- def show(self):
- plt.show()
- a = AnimatedScatter()
- a.show()
- ##%
Raw Paste