PYTHON   26
effet doppler fizeau
Guest on 14th March 2023 01:14:24 PM


  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.animation as animation
  4. import time
  5.  
  6. xmin = -1
  7. xmax = 1
  8. ymin = -1
  9. ymax = 1
  10.  
  11. class Emetteur:
  12.     def __init__(self, f=0.8, v=0):
  13.         self.x = 0
  14.         self.y = 0
  15.         self.f = f
  16.         self.t = time.time()
  17.         self.v = v
  18.  
  19.  
  20. class Vague:
  21.     def __init__(self, x=0, y=0, c=0.2):
  22.         self.to = time.time()
  23.         self.tmax = 3
  24.         self.R = 0
  25.         self.centerx = x
  26.         self.centery = y
  27.         self.c = c
  28.  
  29.     def update(self, dt):
  30.         self.R = self.R + self.c*dt
  31.  
  32.  
  33. k = 2*np.pi
  34. v = 0.05
  35. ech = 18
  36. angle = np.linspace(0, 2, ech)
  37.  
  38. class AnimatedScatter(object):
  39.     def __init__(self, numpoints=72):
  40.         self.numpoints = numpoints
  41.         self.xmin = -1.5
  42.         self.xmax = 1.5
  43.         self.ymin = -1
  44.         self.ymax = 1
  45.         self.t = time.time()
  46.         self.to = time.time()
  47.         self.fig, self.ax = plt.subplots(figsize=(xmax * 6*2, ymax * 6))
  48.         self.angles = np.linspace(0,2, self.numpoints)
  49.         self.lines = []
  50.         self.vagues = []
  51.         self.emetteur = Emetteur()
  52.         self.nbvagues = 0
  53.  
  54.         self.ax.axis([self.xmin, self.xmax, self.ymin, self.ymax])
  55.  
  56.         self.anis = animation.FuncAnimation(self.fig, self.updates, interval=18,
  57.                                            init_func=self.setup_plots, blit=False)
  58.  
  59.         self.anil = animation.FuncAnimation(self.fig, self.updatel, interval=18,
  60.                                            init_func=self.setup_plotl, blit=False)
  61.  
  62.     def setup_plots(self):
  63.         self.scat = self.ax.scatter([0], [0])
  64.         return self.scat
  65.  
  66.     def updates(self, i):
  67.         dt = time.time() - self.t
  68.         if time.time() - self.to > 4:
  69.             self.emetteur.v = 0.6
  70.         self.emetteur.x = self.emetteur.x + 0.01 * self.emetteur.v
  71.  
  72.  
  73.         self.scat.set_offsets([self.emetteur.x, self.emetteur.y])
  74.         return self.scat,
  75.  
  76.     def setup_plotl(self):
  77.         for line in self.lines:
  78.             line.set_data([], [])
  79.         return self.lines
  80.  
  81.     def updatel(self, i):
  82.         dtphy = time.time() - self.t
  83.  
  84.         dt = time.time() - self.emetteur.t
  85.         if dt > self.emetteur.f:
  86.             self.emetteur.t = time.time()
  87.             self.lines.append(self.ax.plot([],[], lw=1)[0])
  88.             self.vagues.append(Vague(self.emetteur.x, self.emetteur.y))
  89.             self.nbvagues = self.nbvagues + 1
  90.  
  91.         for it in range(self.nbvagues):
  92.             self.vagues[it].update(dtphy)
  93.             self.vagues[it].x = self.datax(self.vagues[it].R) + self.vagues[it].centerx
  94.             self.vagues[it].y = self.datay(self.vagues[it].R) + self.vagues[it].centery
  95.             self.lines[it].set_data(self.vagues[it].x, self.vagues[it].y)
  96.         self.t = time.time()
  97.         return self.lines
  98.  
  99.     def datax(self, R):
  100.         x = np.linspace(0,0,self.numpoints)
  101.         for i in range(self.numpoints):
  102.             x[i] = R*np.cos(self.angles[i]*2*np.pi)
  103.         return x
  104.  
  105.     def datay(self, R):
  106.         y = np.linspace(0,0,self.numpoints)
  107.         for i in range(self.numpoints):
  108.             y[i] = R*np.sin(self.angles[i]*2*np.pi)
  109.         return y
  110.  
  111.     def show(self):
  112.         plt.show()
  113.  
  114. a = AnimatedScatter()
  115. a.show()
  116.  
  117. ##%

Raw Paste

Login or Register to edit or fork this paste. It's free.