Advertisement
xeromino

Einstein exploding

Oct 2nd, 2013
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. PImage img;
  2. ArrayList pieces;
  3. float x, y;
  4. float incr;
  5. color col;
  6. int counter = 0;
  7.  
  8. void setup() {
  9.   img = loadImage("http://i.imgur.com/GypezS6.jpg");
  10.   size(img.width, img.height);
  11.   img.loadPixels();
  12.   rectMode(CENTER);
  13.  
  14.   pieces = new ArrayList();
  15.   incr = 2;
  16.  
  17.   for (int i = 0;  i<width; i += incr) {
  18.     for (int j = 0; j < height; j += incr) {
  19.       x = i + incr/2;
  20.       y = j + incr/2;
  21.       col = img.pixels[int(y)*width+int(x)];
  22.       Piece piece = new Piece(x, y, col);
  23.       pieces.add(piece);
  24.     }
  25.   }
  26. }
  27.  
  28. void draw() {
  29.   background(0);
  30.   for (int i = 0; i < pieces.size(); i++) {
  31.     Piece p = (Piece) pieces.get(i);
  32.     p.run();
  33.   }
  34. }
  35.  
  36. class Piece {
  37.   float x, y;
  38.   float incr_ex = 0.01;
  39.   color c;
  40.   PVector s;
  41.  
  42.   Piece(float _x, float _y, color _col) {
  43.     x = _x;
  44.     y = _y;
  45.     c = _col;
  46.     s = new PVector(random(-2, 2), random(-2, 2));
  47.   }
  48.  
  49.   void run() {
  50.     display();
  51.     if (frameCount > 75) {
  52.       explode();
  53.     }
  54.   }
  55.  
  56.   void explode() {
  57.     x += s.x;
  58.     y += s.y;
  59.   }
  60.  
  61.   void display() {
  62.     fill(c);
  63.     noStroke();
  64.     rect(x, y, incr, incr);
  65.   }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement