Advertisement
Guest User

aaa

a guest
Jun 24th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.animation import FuncAnimation
  4.  
  5. fig = plt.figure(figsize=(7, 7))
  6. ax = fig.add_axes([0, 0, 1, 1], frameon=False)
  7. ax.set_xlim(-1, 1), ax.set_xticks([])
  8. ax.set_ylim(-1, 1), ax.set_yticks([])
  9.  
  10. # Set initial coordinates
  11. x0 = 0
  12. y0 = 0
  13.  
  14. # Set current coordinates
  15. x_curr = x0
  16. y_curr = y0
  17.  
  18. # Initialize array of coordinates history
  19. x_ac = [x0]
  20. y_ac = [y0]
  21.  
  22. # Initialize destination coordinates
  23. xd = x0
  24. yd = y0
  25.  
  26. # Set number and index of steps
  27. n_steps = 20
  28. step_i = 0
  29.  
  30. # Set waiting parameters
  31. waiting = True
  32. n_waiting = 5
  33. wait_i = 0
  34.  
  35. def update(frame_number):
  36.     global step_i, waiting, wait_i, x_curr, y_curr, x0, y0, xd, yd
  37.  
  38.     if not waiting:
  39.         if step_i < n_steps:
  40.             # Task 7.1.1
  41.             # x_curr = x0 + (float((xd - x0)) / float(n_steps)) * step_i
  42.             # y_curr = y0 + (float((yd - y0)) / float(n_steps)) * step_i
  43.             # x_curr = x0 + np.random.uniform(-1, 1)/float(n_steps) * step_i
  44.             # y_curr = y0 + np.random.uniform(-1, 1)/float(n_steps) * step_i
  45.             # x_curr = 2*np.random.rand()-1
  46.             # y_curr = 2*np.random.rand()-1
  47.             x_curr = np.random.uniform(-1, 1)
  48.             y_curr = np.random.uniform(-1, 1)
  49.             step_i += 1
  50.         else:
  51.             # Task 7.1.1
  52.             x_curr = xd
  53.             y_curr = yd
  54.             step_i = 0
  55.             waiting = True
  56.  
  57.         # Accumulate coordinates
  58.         x_ac.append(x_curr)
  59.         y_ac.append(y_curr)
  60.  
  61.     else:
  62.         if wait_i <= n_waiting:
  63.             wait_i += 1
  64.         else:
  65.             wait_i = 0
  66.             # Task 7.1.1
  67.             x0 = xd
  68.             y0 = yd
  69.             xd = np.random.uniform(-1, 1)
  70.             yd = np.random.uniform(-1, 1)
  71.             waiting = False
  72.  
  73.     # Plot animation
  74.     ax.clear()
  75.     plt.plot(x_ac, y_ac)
  76.     plt.plot(x_curr, y_curr, 'o')
  77.     ax.set_xlim(-1, 1), ax.set_xticks([])
  78.     ax.set_ylim(-1, 1), ax.set_yticks([])
  79.  
  80.  
  81. # Construct the animation, using the update function as the animation director.
  82. fig.set_tight_layout(False)
  83. animation = FuncAnimation(fig, update, interval=10)
  84. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement