Advertisement
xeromino

flowfield

Apr 20th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. // based on a technique presented by Ianis Lallemand (http://www.ianislallemand.net/) during the 2015 Processing workshop in Paris
  2.  
  3. // Paramètres
  4. int agentCount = 4000;
  5. float agentSize = 1.5;
  6. float agentAlpha = 25;
  7. float fieldIntensity = 1;  // decrease to limit the possible angles
  8. float blurLevel = 40;  // the higher, the smoother the flows
  9.  
  10. // Variables
  11. ArrayList<Agent> agents;
  12. PImage img;
  13.  
  14. void setup()
  15. {
  16.   size(900, 700);
  17.   background(255);
  18.   img = loadImage("mies.jpg");
  19.   img.filter(BLUR, blurLevel);
  20.  
  21.   agents = new ArrayList<Agent>();
  22.   for (int i = 0; i < agentCount; i++)
  23.   {
  24.     Agent a = new Agent();
  25.     agents.add(a);
  26.   }
  27. }
  28.  
  29. void draw()
  30. {
  31.   stroke(0, agentAlpha);
  32.   strokeWeight(agentSize);
  33.  
  34.   for (Agent a : agents)
  35.   {
  36.     a.angle = getAngle(a.position);
  37.     a.updatePosition();
  38.     line(a.previousPosition.x, a.previousPosition.y, a.position.x, a.position.y);
  39.     //if (random(1)>0.5) line(a.previousPosition.x, a.previousPosition.y, a.position.x, a.position.y); // to give it a less clean look
  40.   }
  41. }
  42.  
  43. float getAngle(PVector position) {
  44.   color c = img.get(int(position.x/width * img.width), int(position.y/height * img.height));
  45.   float br = map(brightness(c),0,255,0,TWO_PI);
  46.   return br*fieldIntensity;
  47. }
  48.  
  49. void keyPressed() {
  50.   save(random(1000)+".jpg");
  51. }
  52.  
  53. class Agent
  54. {
  55.   // Attributs de la classe
  56.   PVector position; // Position de l'agent
  57.   PVector previousPosition; // Stockage de la position précédente (pour le dessin)
  58.   float stepSize=1; // Incrément de déplacement (= vitesse de base de l'agent)
  59.   float angle=0; // Angle de déplacement de l'agent
  60.   boolean isPositionResetWhenOutside; // Permet d'activer ou non la réinitialisation de la position de l'agent lorsqu'il quitte l'espace du sketch
  61.  
  62.   // Le constucteur par défaut de la classe
  63.   Agent()
  64.   {
  65.     position = new PVector(random(width), random(height)); // Position aléatoire
  66.     previousPosition = position.get(); // Attention à bien copier le PVector avec la méthode 'get()';
  67.     isPositionResetWhenOutside = true;
  68.   }
  69.  
  70.   // Une méthode de la classe permettant de mettre à jour la position de l'agent (en fonction de son angle de déplacement actuel)
  71.   void updatePosition()
  72.   {
  73.     previousPosition = position.get(); // Sauvegarde de la position précédente
  74.     position.x += cos(angle) * stepSize; // L'agent avance sur une distance égale à 'stepSize' à partir de sa position actuelle, selon un angle 'angle'
  75.     position.y += sin(angle) * stepSize;
  76.     if (isOutsideSketch() && isPositionResetWhenOutside)
  77.     {
  78.       position = new PVector(random(width), random(height)); // Si l'agent sort du sketch, on lui attribue une nouvelle position aléatoire
  79.       previousPosition = position.get();
  80.     }
  81.   }
  82.  
  83.   boolean isOutsideSketch()
  84.   {
  85.     if (position.y < 0 || position.x > width || position.y > height || position.x < 0)
  86.     {
  87.       return true;
  88.     } else {
  89.       return false;
  90.     }
  91.   }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement