Advertisement
PeterSpeckmayer

point picking in a sphere and close to the surface

Mar 17th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1.  
  2. from pylab import *
  3. from math import *
  4. from random import *
  5.  
  6. def rand(a, b):
  7.     return (b-a)*random() + a
  8.  
  9.  
  10. surfaceThickness = 0.01
  11. manyBins = 50
  12. fewBins = 10
  13.  
  14. numpoints = xrange(10000)
  15.  
  16. # point picking in a sphere
  17. # mathworld.wolfram.com/SpherePointPicking.html (area has to be preserved)
  18. # www.mathworks.de/de/help/matlab/math/numbers-placed-randomly-within-volume-of-sphere.html
  19.  
  20. theta = [acos(2.0*rand(0.0, 1.0)-1.0) for x in numpoints]
  21. phi = [rand(0.0, 2*pi) for x in numpoints]
  22.  
  23. radius = [3*(rand (1.0-surfaceThickness,1.0)**(1.0/3.0)) for x in numpoints]
  24. pointsSurf = [(r*sin(t)*cos(p), r*sin(t)*sin(p), r*cos(t)) for r,t,p in zip (radius,theta,phi)] # sphere
  25.  
  26. radius = [3*(rand (0.0,1.0)**(1.0/3.0)) for x in numpoints]
  27. pointsFull = [(r*sin(t)*cos(p), r*sin(t)*sin(p), r*cos(t)) for r,t,p in zip (radius,theta,phi)] # sphere
  28.  
  29. px, py, pz = zip (*pointsSurf)
  30.  
  31. subplot (2,2,1)
  32. hist2d (px,py, bins=manyBins)
  33. colorbar ()
  34.  
  35. subplot (2,2,3)
  36. hist2d (px,py, bins=fewBins)
  37. colorbar ()
  38.  
  39. px, py, pz = zip (*pointsFull)
  40.  
  41. subplot (2,2,2)
  42. hist2d (px,py, bins=manyBins)
  43. colorbar ()
  44.  
  45. subplot (2,2,4)
  46. hist2d (px,py, bins=fewBins)
  47. colorbar ()
  48.  
  49.  
  50. show ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement