Advertisement
Guest User

atoms gas simulation

a guest
Dec 21st, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | Source Code | 0 0
  1. int heightPressure;
  2. int atomsAmount = 250;
  3. int atomDiameter = 10;
  4. ArrayList<Atom> atoms = new ArrayList<Atom>();
  5.  
  6. void setup() {
  7.   size(500, 800);
  8.  
  9.   heightPressure = height;
  10.  
  11.   for (int i = 0; i < atomsAmount; i++) {
  12.     atoms.add(new Atom(
  13.       i,
  14.       new PVector(
  15.         random(atomDiameter / 2, width - atomDiameter / 2),
  16.         random(atomDiameter / 2, height - atomDiameter / 2)
  17.       ),
  18.       new PVector(
  19.         random(-1, 1) > 0 ? 1 : -1,
  20.         random(-1, 1) > 0 ? 1 : -1
  21.       ),
  22.       atomDiameter
  23.     ));
  24.   }
  25. }
  26.  
  27. void draw() {
  28.   background(204);
  29.  
  30.   heightPressure = mouseY == 0 ? height : mouseY;
  31.  
  32.   strokeWeight(3);
  33.   line(0, heightPressure, width, heightPressure);
  34.   strokeWeight(6);
  35.   line(width / 2, height, width / 2, heightPressure);
  36.   strokeWeight(1);
  37.  
  38.   for (Atom atom : atoms) {
  39.     atom.update();
  40.     atom.render();
  41.   }
  42. }
  43.  
  44. class Atom {
  45.   int atomIndex;
  46.   PVector pos;
  47.   PVector acc;
  48.   int diameter;
  49.  
  50.   Atom (int index, PVector p, PVector a, int dia) {
  51.     atomIndex = index;
  52.     pos = p;
  53.     acc = a;
  54.     diameter = dia;
  55.   }
  56.  
  57.   void update() {
  58.     // Width
  59.     if (pos.x > width - diameter / 2) {
  60.       acc.x = -1;
  61.     }
  62.     if (pos.x < diameter / 2) {
  63.       acc.x = 1;
  64.     }
  65.    
  66.     // Height
  67.     if (pos.y > heightPressure - diameter / 2) {
  68.       acc.y = -1;
  69.       pos.y = heightPressure - diameter / 2;
  70.     }
  71.     if (pos.y < diameter / 2) {
  72.       acc.y = 1;
  73.     }
  74.    
  75.     if (false) {
  76.       for (int i = 0; i < atoms.size(); i++) {
  77.         if (i != atomIndex) {
  78.           Atom atom2 = atoms.get(i);
  79.          
  80.           // Width
  81.           if (pos.x > atom2.pos.x - diameter) {
  82.             acc.x = -1;
  83.           }
  84.           if (pos.x < atom2.pos.x + diameter) {
  85.             acc.x = 1;
  86.           }
  87.          
  88.           // Height
  89.           if (pos.y > atom2.pos.y - diameter) {
  90.             acc.y = -1;
  91.           }
  92.           if (pos.y < atom2.pos.y + diameter) {
  93.             acc.y = 1;
  94.           }
  95.         }
  96.       }
  97.     }
  98.    
  99.     pos.add(acc);
  100.   }
  101.  
  102.   void render() {
  103.     ellipse(pos.x, pos.y, diameter, diameter);
  104.   }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement