Advertisement
xeromino

A

May 21st, 2014
793
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. int fc, num = 2000;
  2. ArrayList ballCollection;
  3. boolean save = false;
  4. float scal, theta;
  5. PImage img;
  6.  
  7. void setup() {
  8.   background(20);
  9.   img=loadImage("A.gif");
  10.   size(img.width, img.height);
  11.   ballCollection = new ArrayList();
  12.   createStuff();
  13. }
  14.  
  15. void draw() {
  16.   background(20);
  17.   for (int i=0; i<ballCollection.size (); i++) {
  18.     Ball mb = (Ball) ballCollection.get(i);
  19.     mb.run();
  20.   }  
  21.  
  22.   theta += .0523;
  23.  
  24.   if (save) {
  25.     if (frameCount%4==0 && frameCount < fc + 121) saveFrame("image-####.gif");
  26.   }
  27. }
  28.  
  29. void keyPressed() {
  30.   fc = frameCount;
  31.   save = true;
  32.   //saveFrame("image-###.jpg");
  33. }
  34.  
  35. void mouseReleased() {
  36.   createStuff();
  37. }
  38.  
  39. void createStuff() {
  40.   ballCollection.clear();
  41.   for (int i=0; i<num; i++) {
  42.     int x = (int)random(width);
  43.     int y = (int)random(height);
  44.     color c = img.get(x, y);
  45.     if (brightness(c)< 10) {
  46.       PVector org = new PVector(x, y);
  47.       float radius = random(5, 10);
  48.       PVector loc = new PVector(org.x+radius, org.y);
  49.       float offSet = random(TWO_PI);
  50.       int dir = 1;
  51.       float r = random(1);
  52.       if (r>.5) dir =-1;
  53.       Ball myBall = new Ball(org, loc, radius, dir, offSet);
  54.       ballCollection.add(myBall);
  55.     }
  56.   }
  57. }
  58. class Ball {
  59.  
  60.   PVector org, loc;
  61.   float sz = 2;
  62.   float radius, offSet, a;
  63.   int s, dir, countC, d = 20;
  64.   boolean[] connection = new boolean[num];
  65.  
  66.   Ball(PVector _org, PVector _loc, float _radius, int _dir, float _offSet) {
  67.     org = _org;
  68.     loc = _loc;
  69.     radius = _radius;
  70.     dir = _dir;
  71.     offSet = _offSet;
  72.   }
  73.  
  74.   void run() {
  75.     display();
  76.     move();
  77.     lineBetween();
  78.   }
  79.  
  80.   void move() {
  81.     loc.x = org.x + sin(theta*dir+offSet)*radius;
  82.     loc.y = org.y + cos(theta*dir+offSet)*radius;
  83.   }
  84.  
  85.   void lineBetween() {
  86.     countC = 1;
  87.     for (int i=0; i<ballCollection.size(); i++) {
  88.       Ball other = (Ball) ballCollection.get(i);
  89.       float distance = loc.dist(other.loc);
  90.       if (distance >0 && distance < d) {
  91.         a = map(countC,0,10,10,255);
  92.         stroke(255, a);
  93.         line(loc.x, loc.y, other.loc.x, other.loc.y);
  94.         connection[i] = true;
  95.       }
  96.       else {
  97.         connection[i] = false;
  98.       }
  99.     }
  100.     for (int i=0; i<ballCollection.size(); i++) {
  101.       if (connection[i]) countC++;
  102.     }
  103.   }
  104.  
  105.   void display() {
  106.     noStroke();
  107.     fill(255, 200);
  108.     ellipse(loc.x, loc.y, sz, sz);
  109.   }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement