Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. from __future__ import division
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5.  
  6. if __name__ == "__main__":
  7. n = 10**6
  8. x_pos = np.zeros(n)
  9. y_pos = np.zeros(n)
  10.  
  11.  
  12. def new_pos(p, x, y):
  13. if p <= 0.85:
  14. return 0.85 * x + 0.04 * y, -0.04 * x + 0.859 * y + 1.6
  15. elif 0.85 < p <= 0.92:
  16. return 0.2 * x - 0.26 * y, 0.23 * x + 0.22 * y + 1.6
  17. elif 0.92 < p <= 0.99:
  18. return -0.15 * x + 0.28 * y, 0.26 * x + 0.24 * y + 0.44
  19. else:
  20. return 0.0, 0.16 * y
  21.  
  22. for i in range(n - 1):
  23. x_pos[i + 1], y_pos[i + 1] = new_pos(np.random.sample(), x_pos[i], y_pos[i])
  24.  
  25. plt.figure(facecolor='black')
  26. plt.subplot(1, 2, 1, axisbg='black')
  27. plt.plot(x_pos, y_pos, ',', color='Lime')
  28.  
  29. xl, xu, yl, yu = 0.25, 0.75, 2, 3
  30. plt.plot([xl, xu, xu, xl, xl], [yl, yl, yu, yu, yl], 'r')
  31.  
  32. x_pos2 = np.zeros(n)
  33. y_pos2 = np.zeros(n)
  34.  
  35. xn, yn = 0.0, 0.0
  36. cnt = 0
  37. while cnt < n:
  38. xn, yn = new_pos(np.random.sample(), xn, yn)
  39. if xl < xn < xu and yl < yn < yu:
  40. x_pos2[cnt], y_pos2[cnt] = xn, yn
  41. cnt += 1
  42.  
  43. plt.subplot(1, 2, 2, axisbg='black')
  44. plt.plot(x_pos2, y_pos2, ',', color='Lime')
  45. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement