Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int heightPressure;
- int atomsAmount = 250;
- int atomDiameter = 10;
- ArrayList<Atom> atoms = new ArrayList<Atom>();
- void setup() {
- size(500, 800);
- heightPressure = height;
- for (int i = 0; i < atomsAmount; i++) {
- atoms.add(new Atom(
- i,
- new PVector(
- random(atomDiameter / 2, width - atomDiameter / 2),
- random(atomDiameter / 2, height - atomDiameter / 2)
- ),
- new PVector(
- random(-1, 1) > 0 ? 1 : -1,
- random(-1, 1) > 0 ? 1 : -1
- ),
- atomDiameter
- ));
- }
- }
- void draw() {
- background(204);
- heightPressure = mouseY == 0 ? height : mouseY;
- strokeWeight(3);
- line(0, heightPressure, width, heightPressure);
- strokeWeight(6);
- line(width / 2, height, width / 2, heightPressure);
- strokeWeight(1);
- for (Atom atom : atoms) {
- atom.update();
- atom.render();
- }
- }
- class Atom {
- int atomIndex;
- PVector pos;
- PVector acc;
- int diameter;
- Atom (int index, PVector p, PVector a, int dia) {
- atomIndex = index;
- pos = p;
- acc = a;
- diameter = dia;
- }
- void update() {
- // Width
- if (pos.x > width - diameter / 2) {
- acc.x = -1;
- }
- if (pos.x < diameter / 2) {
- acc.x = 1;
- }
- // Height
- if (pos.y > heightPressure - diameter / 2) {
- acc.y = -1;
- pos.y = heightPressure - diameter / 2;
- }
- if (pos.y < diameter / 2) {
- acc.y = 1;
- }
- if (false) {
- for (int i = 0; i < atoms.size(); i++) {
- if (i != atomIndex) {
- Atom atom2 = atoms.get(i);
- // Width
- if (pos.x > atom2.pos.x - diameter) {
- acc.x = -1;
- }
- if (pos.x < atom2.pos.x + diameter) {
- acc.x = 1;
- }
- // Height
- if (pos.y > atom2.pos.y - diameter) {
- acc.y = -1;
- }
- if (pos.y < atom2.pos.y + diameter) {
- acc.y = 1;
- }
- }
- }
- }
- pos.add(acc);
- }
- void render() {
- ellipse(pos.x, pos.y, diameter, diameter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement