Guest User

Critical mass

a guest
Mar 3rd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. import controlP5.*;
  2. import java.util.List;
  3. import java.util.ArrayList;
  4.  
  5. ControlP5 gui;
  6.  
  7. List<Atom> atomList;
  8. List<Neutron> neutronList;
  9.  
  10. int currAtoms;
  11. float currSpacing;
  12. boolean run;
  13.  
  14. Slider numSlider;
  15. Slider spaceSlider;
  16.  
  17. void setup()
  18. {
  19. size(800,900);
  20. gui = new ControlP5(this);
  21.  
  22. numSlider = gui.addSlider("Number of Atoms")
  23. .setPosition(10,50)
  24. .setRange(0,1000)
  25. .setValue(100)
  26. .setDecimalPrecision(0);
  27.  
  28. spaceSlider = gui.addSlider("Atom Spacing")
  29. .setPosition(10,80)
  30. .setRange(1,30)
  31. .setValue(22);
  32.  
  33. gui.addButton("run")
  34. .setPosition(width/2-50, height-30);
  35.  
  36. gui.addButton("reset")
  37. .setPosition(width/2+30, height-30);
  38.  
  39. updateGrid();
  40.  
  41. }
  42.  
  43. void draw()
  44. {
  45. background(180);
  46.  
  47. if(run)
  48. {
  49.  
  50. for(Neutron n : neutronList)
  51. {
  52. n.display();
  53. n.update();
  54. }
  55.  
  56. for(Atom a : atomList)
  57. {
  58. a.display();
  59. a.update();
  60. }
  61. }
  62. else
  63. {
  64. if(spaceSlider.getValue() != currSpacing || (int)numSlider.getValue() != currAtoms)
  65. {
  66. updateGrid();
  67. }
  68. for(Atom a : atomList)
  69. {
  70. a.display();
  71. }
  72. }
  73.  
  74. }
  75.  
  76. public void run()
  77. {
  78. reset();
  79. int i = (int)random(atomList.size());
  80. atomList.get(i).decay();
  81. run = true;
  82. }
  83.  
  84. public void reset()
  85. {
  86. updateGrid();
  87. run = false;
  88. }
  89.  
  90. public void updateGrid()
  91. {
  92. currSpacing = spaceSlider.getValue();
  93. currAtoms = (int)numSlider.getValue();
  94.  
  95. float x = width/2;
  96. float y = height/2 + 20;
  97.  
  98. atomList = new ArrayList<Atom>();
  99. neutronList = new ArrayList<Neutron>();
  100.  
  101. atomList.add(new Atom(x,y,neutronList));
  102.  
  103. // will return from inside when all atoms are drawn
  104. int ring = 0;
  105. while(atomList.size() < currAtoms)
  106. {
  107. for(int dir = 0; dir < 6; dir++)
  108. {
  109. for(int j = 0; j < ring; j++)
  110. {
  111. x += currSpacing * cos(PI/3*dir);
  112. y += currSpacing * sin(PI/3*dir);
  113. atomList.add(new Atom(x,y,neutronList));
  114. if(atomList.size() >= currAtoms)
  115. {
  116. return;
  117. }
  118. }
  119. }
  120.  
  121. x += currSpacing * cos(PI*4/3);
  122. y += currSpacing * sin(PI*4/3);
  123. ring++;
  124. }
  125. }
  126.  
  127. public class Atom
  128. {
  129. float x;
  130. float y;
  131. float r;
  132. float collisionR;
  133.  
  134. List<Neutron> neutronList;
  135. int spawnNum;
  136. float spawnVelocity;
  137.  
  138. boolean alive;
  139.  
  140. public Atom(float _x, float _y, List<Neutron> l)
  141. {
  142. x = _x;
  143. y = _y;
  144. r = 2.5;
  145. collisionR = r/2;
  146. neutronList = l;
  147.  
  148. spawnVelocity = 1.5;
  149. spawnNum = 2;
  150.  
  151. alive = true;
  152. }
  153.  
  154. public void display()
  155. {
  156. if(alive)
  157. {
  158. noStroke();
  159. fill(0);
  160. ellipse(x,y,2*r,2*r);
  161. }
  162. }
  163.  
  164. public void update()
  165. {
  166. if(alive)
  167. {
  168. Neutron n;
  169. for(int i = 0; i < neutronList.size(); i++)
  170. {
  171. n = neutronList.get(i);
  172. if(dist(x,y,n.x,n.y)<collisionR+n.collisionR)
  173. {
  174. neutronList.remove(i);
  175. decay();
  176. return;
  177. }
  178. }
  179.  
  180. }
  181. }
  182.  
  183. public void decay()
  184. {
  185. for(int i = 0; i < spawnNum; i++)
  186. {
  187. float ang = random(0,2*PI);
  188. Neutron n = new Neutron(x,y,spawnVelocity*cos(ang), spawnVelocity*sin(ang));
  189. neutronList.add(n);
  190.  
  191. }
  192. alive = false;
  193. }
  194. }
  195.  
  196. public class Neutron
  197. {
  198. float x;
  199. float y;
  200. float r;
  201. float collisionR;
  202.  
  203. float vx;
  204. float vy;
  205.  
  206. public Neutron(float _x, float _y, float _vx, float _vy)
  207. {
  208. x = _x;
  209. y = _y;
  210. vx = _vx;
  211. vy = _vy;
  212.  
  213. r = 1;
  214. collisionR = r/2;
  215. }
  216.  
  217. public void display()
  218. {
  219. noStroke();
  220. fill(255,0,0);
  221. ellipse(x,y,2*r,2*r);
  222. }
  223.  
  224. public void update()
  225. {
  226. x += vx;
  227. y += vy;
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment