Guest User

Untitled

a guest
Jun 16th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. from mpl_toolkits.mplot3d import Axes3D
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from sys import argv
  5.  
  6. R = 5
  7. p = 2
  8. fmax = (R + p)/(4*(np.pi**2)*R)
  9.  
  10. def f(u, v):
  11. return (R + p*np.cos(v))/(4*(np.pi**2)*R)
  12.  
  13. def points(f, fmax, n=500):
  14. points = []
  15. R = np.random.random
  16. while True:
  17. u = R()*2*np.pi
  18. v = R()*2*np.pi
  19.  
  20. if fmax*R() < f(u, v): points.append((u, v))
  21. if len(points) == n: break
  22.  
  23. return points
  24.  
  25.  
  26. def torus(points):
  27. fig = plt.figure()
  28. ax = fig.gca(projection='3d')
  29. plt.hold(True)
  30.  
  31. u = np.linspace(0, 2*np.pi, 100)
  32. v = np.linspace(0, 2*np.pi, 100)
  33.  
  34. x = np.outer((R + p*np.cos(v)), np.cos(u))
  35. y = np.outer((R + p*np.cos(v)), np.sin(u))
  36. z = np.outer(p*np.sin(v), np.ones(np.size(u)))
  37.  
  38. pu = np.array([p[0] for p in points])
  39. pv = np.array([p[1] for p in points])
  40.  
  41. px = (R + p*np.cos(pv))*np.cos(pu)
  42. py = (R + p*np.cos(pv))*np.sin(pu)
  43. pz = p*np.sin(pv)
  44.  
  45. ax.plot_surface(x, y, z, rstride=10, cstride=10, color='w', alpha=.5)
  46. ax.scatter(px, py, pz, color = 'r', s=10, alpha=1)
  47.  
  48. plt.show()
  49.  
  50. if __name__ == "__main__":
  51. if len(argv)>1:
  52. n = int(argv[1])
  53. points = points(f, fmax, n)
  54. else:
  55. points = points(f, fmax)
  56. torus(points)
Advertisement
Add Comment
Please, Sign In to add comment