Advertisement
xeromino

fireflies

Aug 12th, 2016
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Blob[] blobs = new Blob[20];
  2. int frms = 90;
  3. float theta;
  4.  
  5. void setup() {
  6.   size(750, 540);
  7.   for (int i=0; i<blobs.length; i++) {
  8.     blobs[i] = new Blob(random(width), random(height));
  9.   }
  10. }
  11.  
  12. void draw() {
  13.   background(51);
  14.  
  15.   for (int i=0; i<blobs.length; i++) {
  16.     blobs[i].update();
  17.   }
  18.  
  19.   loadPixels();
  20.  
  21.   for (int x=0; x<width; x++) {
  22.     for (int y = 0; y<height; y++) {
  23.       int index = x + y * width;
  24.       float sum = 0;
  25.       for (Blob b : blobs) {
  26.         float d = dist(x, y, b.pos.x, b.pos.y);
  27.         sum += b.r/d*75;
  28.       }
  29.       pixels[index] = color(sum, sum/2, sum/3);
  30.     }
  31.   }
  32.  
  33.   updatePixels();
  34.  
  35.   theta += TWO_PI/frms;
  36.   //if (frameCount<=frms) saveFrame("image-###.gif");
  37. }
  38.  
  39.  
  40. class Blob {
  41.  
  42.   PVector pos, orig;
  43.   //PVector vel;
  44.   float r, d, offSet;
  45.  
  46.   Blob(float _x, float _y) {
  47.     orig = new PVector(_x, _y);
  48.     pos = new PVector(0, 0);
  49.     r = random(20, 60);
  50.     //vel = PVector.random2D();
  51.     //vel.mult(random(2, 5));
  52.     //diam = 2*r;
  53.     d = random(25, 150);
  54.     offSet= random(-PI, PI);
  55.   }
  56.  
  57.   void update() {
  58.     //pos.add(vel);
  59.     pos.x = orig.x + cos(theta+offSet)*d;
  60.     pos.y = orig.y + sin(theta+offSet)*d;
  61.  
  62.     //if (pos.x > width + diam || pos.x<-diam) vel.x *= -1;
  63.     //if (pos.y > height+ diam || pos.y<-diam) vel.y *= -1;
  64.   }
  65.  
  66.   void show() {
  67.     noFill();
  68.     stroke(0);
  69.     strokeWeight(4);
  70.     ellipse(pos.x, pos.y, 2*r, 2*r);
  71.   }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement