• fullscreen
  • Mesh.pde
  • Physics.pde
  • Trayectory.pde
  • dia21.pde
  • /*
    **  Elaborado por Thomas Sanchez Lengeling
     **  Utilizando liberias de mesh para triangulacion de voronoi y delaunay y toxilibs
     */
    
    import megamu.mesh.*;
    
    class Mesh {
    
      boolean emptyVoronoi  = false;
      boolean emptyDelaunay = false;
    
      Mesh() {
        fillPoints();
        setVoronoi();
        setDelaunay();
      }
    
      void update() {
        fillPoints();
        setDelaunay();
        setVoronoi();
      }
    
      void draw(PApplet pa) {
        if (showVoronoi) {
          drawVoronoi(pa);
        }
        if ( showDelaunay) {
          drawDelaunay();
        }
      }
    
      void setDelaunay() {
        if ( pointsVD != null && voronoi != null) {  //voronoi != null necesario
          voronoi   = new Voronoi(pointsVD);
          regionsV  = voronoi.getRegions();
          emptyVoronoi= true;
        }
    
        if ( pointsVD != null) {
          delaunay = new Delaunay(pointsVD);
          edgesD    = delaunay.getEdges();
        }
      }
    
      void setVoronoi() {
        if ( pointsVD != null) {
          delaunay = new Delaunay(pointsVD);
          edgesD    = delaunay.getEdges();
          emptyDelaunay = true;
        }
      }
    
      void drawDelaunay() {
        if ( pointsVD != null) {
          stroke(150, 100);
          for (int i=0; i< edgesD.length; i++) {  
            float  startX = edgesD[i][0];  
            float  startY = edgesD[i][1];  
            float  endX =   edgesD[i][2];  
            float  endY =   edgesD[i][3];  
            line(startX - pSize/2, startY - pSize/2, endX - pSize/2, endY - pSize/2);
          }
        }
      }
    
      void drawVoronoi(PApplet pa) {
        if (regionsV != null) {//pointsVD != null) {
          fill(0);
          for (int i =0; i < regionsV.length; i++) {
            regionsV[i].draw(pa);
          }
        }
      }
    
      void fillPoints() {
        for (int i =0; i < numPoints/2; i++) {
          VerletParticle2D p = ((VerletParticle2D)physics.particles.get(i));
          pointsVD[i][0] = p.x;
          pointsVD[i][1] = p.y;
        }
      }
    }
    
    
    /*
    **  Elaborado por Thomas Sanchez Lengeling
    **  Utilizando liberias de mesh para triangulacion de voronoi y delaunay y toxilibs
    */
    
    import toxi.physics2d.*;
    import toxi.physics2d.behaviors.*;
    import toxi.geom.*;
    
    class Particles {
    
      Particles(int numPoints) {
        addParticles(numPoints);
      }
    
      void addParticles(int numPoints) {
        int i =0;
        while ( i < numPoints) {
          float posX = random(pSize - width/2, width/2 - pSize);
          float posY = random(pSize - height/2, height/2 - pSize);
          VerletParticle2D p = new VerletParticle2D(Vec2D.randomVector().addSelf(posX, posY));
          p.addForce(new Vec2D(random(-1.0, 1.0), random(-2.0, 1.0)));
          p.setWeight(random(1, 3));
          physics.addParticle(p);
          i++;
        }
      }
    
      VerletParticle2D getParticle() {
        VerletParticle2D p = ((VerletParticle2D)physics.particles.get((int)random(numPoints)));
        return p;
      }
    
      void draw() {
        for (int i = 0; i < numPoints; i++) {
          VerletParticle2D p = ((VerletParticle2D)physics.particles.get(i));
          p.addForce(new Vec2D(random(-0.7, 0.7), random(-0.7, 0.7)));
          if (p.y >= height/2 -60 && p.x <= -width/2 -  50)
            p.addForce( new Vec2D(random(-0.4, -0.9), random(-0.1, -0.7)));
          ellipse(p.x - pSize/2, p.y - pSize/2, pSize, pSize);  //por amp
        }
      }
    }
    
    
    /*
    **  Elaborado por Thomas Sanchez Lengeling
    **  Utilizando liberias de mesh para triangulacion de voronoi y delaunay y toxilibs
    */
    
    int releaseCount =0;
    boolean startTmp = true;
    boolean startBehavior = true;
    float mouseXTmp;
    float mouseYTmp;
    Vec2D trayec;
    
    float  attractionForce = 1.0;
    float  timeStep        = 0.01;
    
    
    void startTrayec() {
      if (startTmp == true && startTrayectory == true) {
        mousePosicion = new Vec2D(mouseX - width/2, mouseY - height/2);
        trayec        = mousePosicion;
        mouseXTmp =  mouseX - width/2;
        mouseYTmp =  mouseY - height/2;
        atraccionMouse= new AttractionBehavior(mousePosicion, 750, attractionForce, timeStep);// 700, 1.4f, 0.905);//2.5f);// -2.0f);//15 
        physics.addBehavior(atraccionMouse);
        startTmp = false;
      }
      if (startTrayectory) {
        trayec.x = (mouseXTmp)*noise(noiseValX) - (200 + random(-100, 100))*cos(noiseValX);
        trayec.y = (mouseYTmp)*noise(noiseValY) - 200*tan(random(PI))*sin(noiseValY) ;
        mousePosicion.set(trayec.x, trayec.y);
        releaseCount = 0;
        // println(trayec.x+ "  "+trayec.y);
      }
      if ( startTrayectory == false && startTmp == false && startBehavior == true) {
        physics.removeBehavior(atraccionMouse);
        releaseMouse = new AttractionBehavior(mousePosicion, 200, -2.5f, 0.5f); //100, -2.5 0.5
        physics.addBehavior(releaseMouse);
        releaseCount = 0;
        startBehavior = false;
      }
      if ( startBehavior == false &&  releaseCount > frameRate*0.8) {
        physics.removeBehavior(releaseMouse);
        startTmp = true;
        startBehavior = true;
      }
      if ( startBehavior == false)
        releaseCount++;
      // theta+=0.06;
      noiseValY += 0.02;
      noiseValX += 0.01;
    }
    
    /*
    **  Elaborado por Thomas Sanchez Lengeling
     **  Utilizando liberias de mesh para triangulacion de voronoi y delaunay y toxilibs
     **
     **  Presiona e para expander y contraer y w para monstrar triangulacion
     */
    
    Voronoi           voronoi;
    Delaunay          delaunay;
    Particles         puntos;
    Mesh              mesh;
    VerletPhysics2D   physics;
    
    int   numPoints   = 1000;
    float noiseValY   = 0.0;
    float noiseValX   = 0.1;
    int   pSize       = 4;  //tam Particlas Default
    
    float[][]   pointsVD;
    float[][]   edgesD;
    MPolygon    regionsV[];
    
    boolean showVoronoi;
    boolean showPoints;
    boolean updateMesh;
    boolean showDelaunay = true;
    
    //PHYSICS
    boolean unLock;
    boolean startTrayectory;
    int trayectoryType =1;
    
    Vec2D mousePosicion;
    AttractionBehavior atraccionMouse;
    AttractionBehavior releaseMouse;
    
    int i =0;
    
    void setup() {
      size(800, 700);
      smooth();
    
      physics = new VerletPhysics2D();
      physics.setDrag(0.05f);
      physics.setWorldBounds( new Rect(-width/2, -height/2, width, height));
      physics.addBehavior(new GravityBehavior(new Vec2D(0.003, 0.03) ) );
    
      pointsVD = new float[numPoints][2];
      puntos = new Particles(numPoints);
      mesh   = new Mesh();
    
      ellipseMode(CENTER);
      background(0);
    }
    
    void draw() {
      fill(0, 12);
      rect(0, 0, width, height);
    
      pushMatrix();
      translate(width/2, height/2);
    
      physics.update();
    
      startTrayec();
      if (showPoints) {
        noStroke();
        fill(200, 200, 60, 90);
        puntos.draw();
      }
    
      //if (updateMesh) {
      mesh.draw(this);
      mesh.update();
      //}
      popMatrix();
    }
    
    void keyPressed() {
      switch(key) {
      case 'e':
        startTrayectory = !startTrayectory;
        break;
      case 'q':
        physics.removeBehavior(releaseMouse);
        break;
      case 'w':
        unLock = !unLock;
        mesh.fillPoints();
        updateMesh = !updateMesh;
        break;
      case 'z':
        showPoints = !showPoints;
        break;
      case 'x':
        showDelaunay =  !showDelaunay;
        break;
      case '1':
        save("dia20"+i+".jpg"); 
        i++;
        break;
      }
    }
    
    

    code

    tweaks (0)

    license

    advertisement

    Thomas Sanchez Lengling

    Catching a Swarm of bees

    Add to Faves Me Likey@! 13
    You must login/register to add this sketch to your favorites.

    Hackpact Genetive Art Mexico day 20

    Press 'e' to attract points to the current mouse location
    Press again 'e' to expand points, further away from the the center of the swarm, points expand faster and bigger.

    'z' to toggle the drawing points
    'x' to toggle the Delaunay Diagram of the points

    You need to login/register to comment.