Advertisement
MrMusAddict

Stain Glass Mitosis

Mar 12th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. ArrayList<Entity> entities = new ArrayList<Entity>();
  2.  
  3. float eSize = 10;
  4.  
  5. void setup() {
  6.   size(600, 600);
  7.   //fullScreen();
  8.   for (int i = 0; i < 1; i++) {
  9.     entities.add(new Entity(width/2 + random(-1, 1), height/2.0 + random(-1, 1), color(127)));
  10.   }
  11.   noStroke();
  12. }
  13.  
  14. void draw() {
  15.   background(51);
  16.   for (int i = entities.size() - 1; i >= 0; i--) {
  17.     Entity e = entities.get(i);
  18.     e.update();
  19.     if (!e.moved() && entities.size() < 2500){
  20.       entities.add(new Entity(e.pos.x + random(-0.2, 0.2), e.pos.y + random(-0.2, 0.2), e.col));
  21.     }
  22.   }
  23. }
  24.  
  25. class Entity {
  26.   PVector pos;
  27.   PVector oldPos;
  28.   color col;
  29.  
  30.   Entity(float x, float y, color c) {
  31.     pos = new PVector(x, y);
  32.     oldPos = new PVector(0, 0);
  33.     float r = red(c) * random(0.8, 1.25);
  34.     float g = green(c) * random(0.8, 1.25);
  35.     float b = blue(c) * random(0.8, 1.25);
  36.     col = color(r,g,b);
  37.   }
  38.  
  39.   void update() {
  40.     updatePos();
  41.     fill(col, 10);
  42.     ellipse(pos.x, pos.y, eSize, eSize);
  43.     fill(col);
  44.     ellipse(pos.x, pos.y, eSize/2.0, eSize/2.0);
  45.   }
  46.  
  47.   void updatePos() {
  48.     oldPos = new PVector(pos.x, pos.y);
  49.     PVector move = new PVector(0, 0);
  50.  
  51.     for (int i = 0; i < entities.size(); i++) {
  52.       Entity en = entities.get(i);
  53.       if (en != this) {
  54.         float d = dist(pos.x, pos.y, en.pos.x, en.pos.y);
  55.         if (d < eSize) {
  56.           move.x += ((pos.x - en.pos.x)*(eSize - d));
  57.           move.y += ((pos.y - en.pos.y)*(eSize - d));
  58.         }
  59.       }
  60.     }
  61.  
  62.     pos.add(move.normalize().mult(0.2));
  63.   }
  64.  
  65.   boolean moved() {
  66.  
  67.     if (oldPos.x == pos.x && oldPos.y == pos.y) {
  68.       return false;
  69.     } else {
  70.       return true;
  71.     }
  72.   }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement