Delaunay Rising P5 sketch

Delaunay Triangulation based animation. Marching points floating up, inspired by an animation from Brewbot


The Delaunay Triangulation is handled by the Processing Triangulation library: wiki.processing.org/w/Triangulation

import org.processing.wiki.triangulate.*;

int mPointsCount = 125;
ArrayList<PVector> points = new ArrayList<PVector>();
ArrayList triangles = new ArrayList();

void setup(){
     size(300, 400);
     frameRate(30);
     noStroke();
     smooth();
     for(int i = 0 ; i < mPointsCount ; i++){
          points.add(new PVector(random(-50, width + 50), random(height), random(TWO_PI)));
     }
}

void draw(){
     background(255);

     //Update point location
     for(int i = 0 ; i < mPointsCount ; i++){
          PVector point = points.get(i);
          point.y = point.y-1;
          point.x += 1.5*cos(point.z);

          //If point has wandered too far off the edges change direction
          if (point.x < -25 || point.x > width + 25) {
               point.z += PI;
          }

          //Recycle point if it's offscreen
          //(this would normally be < 0 but all triangles are white/invisible by the time they reach height/2)
          if(point.y < height/2){
               point.y =  random(height) + height;
          }
     }

     //Get the triangles
     triangles = Triangulate.triangulate(points);

     //draw the triangles
     beginShape(TRIANGLES);
     for (int i = 0; i < triangles.size(); i++) {
          Triangle t = (Triangle) triangles.get(i);

          //Only process if part of the triangle is likely to be on screen
          if(t.p1.y < height + 100){
               int lum = getLuminance(t);

               //Only draw if the fill color is darker than white
               if(lum < 255){
                    fill(lum, lum, lum + 25);
                    vertex(t.p1.x, t.p1.y);
                    vertex(t.p2.x, t.p2.y);
                    vertex(t.p3.x, t.p3.y);
               }
          }
     }
     endShape();
}

//Compute a shade based on the y location of the triangle
int getLuminance(Triangle t){
    double centerY = (t.p1.y + t.p2.y + t.p3.y) / 3;
    double fillColor = (100/(centerY/3)) * 200;
    return (int) fillColor;
}