Advertisement
xeromino

impress

May 19th, 2014
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. float[][] noiseMap;
  2. ArrayList<Brush> brushes;
  3. PImage imgMap;
  4. int num = 2000, min=2, max=50;
  5. float noiseIncr = .01, drain=.05, brushSize;
  6.  
  7. void setup() {
  8.   imgMap = loadImage("nathalie.jpg");
  9.   size(imgMap.width, imgMap.height);
  10.  
  11.   noiseMap = new float[width][height]; //creating a 2D array for the noise values to be used later
  12.   brushes = new ArrayList<Brush>();
  13.  
  14.   float xstart = random(10);
  15.   float xnoise = xstart;
  16.   float ynoise = random(10);
  17.  
  18.   for (int y=0; y<height; y++) {
  19.     ynoise += noiseIncr;
  20.     xnoise = xstart;
  21.     for (int x=0; x<width; x++) {
  22.       xnoise += noiseIncr;
  23.       float myNoise = noise(xnoise, ynoise);    
  24.       noiseMap[x][y] = myNoise;
  25.     }
  26.   }
  27. }
  28.  
  29. void draw() {
  30.   brushSize = constrain(map(frameCount, 0, 1000, max, min), min, max);
  31.   if (brushes.size()<num) brushes.add(new Brush(new PVector(random(width), random(height))));  
  32.   for (int i=0; i<brushes.size(); i++) {
  33.     Brush brush = brushes.get(i);
  34.     brush.move();
  35.     brush.display();
  36.     brush.life -= drain; //the brush dies at a certain point and gets reborn somewhere else
  37.     if (brush.life<0) {
  38.       brush.life = 1;
  39.       brush.resetPosition();
  40.     }
  41.   }
  42. }
  43.  
  44. void keyPressed() {
  45.   save(random(2334234)+".tif");
  46. }
  47.  
  48. class Brush {
  49.  
  50.   PVector location;
  51.   PVector velocity = new PVector(0, 0);
  52.   float angle, life=1.0;
  53.   color s;
  54.  
  55.   Brush(PVector _location) {
  56.     location = _location;
  57.     createVector();
  58.   }
  59.  
  60.   void move() {
  61.     location.add(velocity);
  62.   }
  63.  
  64.   void display() {
  65.     strokeWeight(brushSize);
  66.     stroke(s, 20);
  67.     point(location.x, location.y);
  68.   }
  69.  
  70.   void resetPosition() {
  71.     location = new PVector(random(width), random(height), 0);
  72.     createVector();
  73.   }
  74.  
  75.   void createVector() {
  76.     s = imgMap.get(int(location.x), int(location.y));
  77.     angle = noiseMap[int(location.x)][int(location.y)] * TWO_PI; // an angle gets created based on the noise values
  78.     velocity.add(cos(angle), sin(angle), 0); //that angle gets turned into a velocity vector that gets normalized in the next step
  79.     velocity.normalize();
  80.   }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement