Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import controlP5.*;
- import java.util.List;
- import java.util.ArrayList;
- ControlP5 gui;
- List<Atom> atomList;
- List<Neutron> neutronList;
- int currAtoms;
- float currSpacing;
- boolean run;
- Slider numSlider;
- Slider spaceSlider;
- void setup()
- {
- size(800,900);
- gui = new ControlP5(this);
- numSlider = gui.addSlider("Number of Atoms")
- .setPosition(10,50)
- .setRange(0,1000)
- .setValue(100)
- .setDecimalPrecision(0);
- spaceSlider = gui.addSlider("Atom Spacing")
- .setPosition(10,80)
- .setRange(1,30)
- .setValue(22);
- gui.addButton("run")
- .setPosition(width/2-50, height-30);
- gui.addButton("reset")
- .setPosition(width/2+30, height-30);
- updateGrid();
- }
- void draw()
- {
- background(180);
- if(run)
- {
- for(Neutron n : neutronList)
- {
- n.display();
- n.update();
- }
- for(Atom a : atomList)
- {
- a.display();
- a.update();
- }
- }
- else
- {
- if(spaceSlider.getValue() != currSpacing || (int)numSlider.getValue() != currAtoms)
- {
- updateGrid();
- }
- for(Atom a : atomList)
- {
- a.display();
- }
- }
- }
- public void run()
- {
- reset();
- int i = (int)random(atomList.size());
- atomList.get(i).decay();
- run = true;
- }
- public void reset()
- {
- updateGrid();
- run = false;
- }
- public void updateGrid()
- {
- currSpacing = spaceSlider.getValue();
- currAtoms = (int)numSlider.getValue();
- float x = width/2;
- float y = height/2 + 20;
- atomList = new ArrayList<Atom>();
- neutronList = new ArrayList<Neutron>();
- atomList.add(new Atom(x,y,neutronList));
- // will return from inside when all atoms are drawn
- int ring = 0;
- while(atomList.size() < currAtoms)
- {
- for(int dir = 0; dir < 6; dir++)
- {
- for(int j = 0; j < ring; j++)
- {
- x += currSpacing * cos(PI/3*dir);
- y += currSpacing * sin(PI/3*dir);
- atomList.add(new Atom(x,y,neutronList));
- if(atomList.size() >= currAtoms)
- {
- return;
- }
- }
- }
- x += currSpacing * cos(PI*4/3);
- y += currSpacing * sin(PI*4/3);
- ring++;
- }
- }
- public class Atom
- {
- float x;
- float y;
- float r;
- float collisionR;
- List<Neutron> neutronList;
- int spawnNum;
- float spawnVelocity;
- boolean alive;
- public Atom(float _x, float _y, List<Neutron> l)
- {
- x = _x;
- y = _y;
- r = 2.5;
- collisionR = r/2;
- neutronList = l;
- spawnVelocity = 1.5;
- spawnNum = 2;
- alive = true;
- }
- public void display()
- {
- if(alive)
- {
- noStroke();
- fill(0);
- ellipse(x,y,2*r,2*r);
- }
- }
- public void update()
- {
- if(alive)
- {
- Neutron n;
- for(int i = 0; i < neutronList.size(); i++)
- {
- n = neutronList.get(i);
- if(dist(x,y,n.x,n.y)<collisionR+n.collisionR)
- {
- neutronList.remove(i);
- decay();
- return;
- }
- }
- }
- }
- public void decay()
- {
- for(int i = 0; i < spawnNum; i++)
- {
- float ang = random(0,2*PI);
- Neutron n = new Neutron(x,y,spawnVelocity*cos(ang), spawnVelocity*sin(ang));
- neutronList.add(n);
- }
- alive = false;
- }
- }
- public class Neutron
- {
- float x;
- float y;
- float r;
- float collisionR;
- float vx;
- float vy;
- public Neutron(float _x, float _y, float _vx, float _vy)
- {
- x = _x;
- y = _y;
- vx = _vx;
- vy = _vy;
- r = 1;
- collisionR = r/2;
- }
- public void display()
- {
- noStroke();
- fill(255,0,0);
- ellipse(x,y,2*r,2*r);
- }
- public void update()
- {
- x += vx;
- y += vy;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment