Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // based on a technique presented by Ianis Lallemand (http://www.ianislallemand.net/) during the 2015 Processing workshop in Paris
- // Paramètres
- int agentCount = 4000;
- float agentSize = 1.5;
- float agentAlpha = 25;
- float fieldIntensity = 1; // decrease to limit the possible angles
- float blurLevel = 40; // the higher, the smoother the flows
- // Variables
- ArrayList<Agent> agents;
- PImage img;
- void setup()
- {
- size(900, 700);
- background(255);
- img = loadImage("mies.jpg");
- img.filter(BLUR, blurLevel);
- agents = new ArrayList<Agent>();
- for (int i = 0; i < agentCount; i++)
- {
- Agent a = new Agent();
- agents.add(a);
- }
- }
- void draw()
- {
- stroke(0, agentAlpha);
- strokeWeight(agentSize);
- for (Agent a : agents)
- {
- a.angle = getAngle(a.position);
- a.updatePosition();
- line(a.previousPosition.x, a.previousPosition.y, a.position.x, a.position.y);
- //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
- }
- }
- float getAngle(PVector position) {
- color c = img.get(int(position.x/width * img.width), int(position.y/height * img.height));
- float br = map(brightness(c),0,255,0,TWO_PI);
- return br*fieldIntensity;
- }
- void keyPressed() {
- save(random(1000)+".jpg");
- }
- class Agent
- {
- // Attributs de la classe
- PVector position; // Position de l'agent
- PVector previousPosition; // Stockage de la position précédente (pour le dessin)
- float stepSize=1; // Incrément de déplacement (= vitesse de base de l'agent)
- float angle=0; // Angle de déplacement de l'agent
- boolean isPositionResetWhenOutside; // Permet d'activer ou non la réinitialisation de la position de l'agent lorsqu'il quitte l'espace du sketch
- // Le constucteur par défaut de la classe
- Agent()
- {
- position = new PVector(random(width), random(height)); // Position aléatoire
- previousPosition = position.get(); // Attention à bien copier le PVector avec la méthode 'get()';
- isPositionResetWhenOutside = true;
- }
- // Une méthode de la classe permettant de mettre à jour la position de l'agent (en fonction de son angle de déplacement actuel)
- void updatePosition()
- {
- previousPosition = position.get(); // Sauvegarde de la position précédente
- position.x += cos(angle) * stepSize; // L'agent avance sur une distance égale à 'stepSize' à partir de sa position actuelle, selon un angle 'angle'
- position.y += sin(angle) * stepSize;
- if (isOutsideSketch() && isPositionResetWhenOutside)
- {
- position = new PVector(random(width), random(height)); // Si l'agent sort du sketch, on lui attribue une nouvelle position aléatoire
- previousPosition = position.get();
- }
- }
- boolean isOutsideSketch()
- {
- if (position.y < 0 || position.x > width || position.y > height || position.x < 0)
- {
- return true;
- } else {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement