Skip to content

Instantly share code, notes, and snippets.

@fiskurgit
Created September 26, 2013 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiskurgit/6714940 to your computer and use it in GitHub Desktop.
Save fiskurgit/6714940 to your computer and use it in GitHub Desktop.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment