Skip to content

Instantly share code, notes, and snippets.

@villares
Last active May 28, 2017 22:13
Show Gist options
  • Save villares/f4d0d604300b4864e69353af84743f9b to your computer and use it in GitHub Desktop.
Save villares/f4d0d604300b4864e69353af84743f9b to your computer and use it in GitHub Desktop.
// Recursive lightning
// Raio recursivo
void setup() {
size(400, 400);
frameRate(2);
}
void draw() {
background(0);
stroke(255);
//strokeWeight(3);
raio(width/2, 0, 10, 1);
}
void raio(float x_start, float y_start, int num, float s) {
float w = 50;
float y_step = 40;
float r_range = 15;
pushMatrix();
translate(x_start, y_start);
scale(s, s);
PShape ps = createShape();
ps.beginShape();
ps.noFill();
ps.vertex(0, 0);
for (int i = 1; i < num; i++) {
float x = w/2 + random(-r_range*2, r_range*2);
float y = i*y_step + random(-r_range, r_range);
if ( i % 2 == 0) x = -x;
ps.vertex(x, y);
}
ps.endShape();
shape(ps, 0, 0);
if (num>3) { // enquando for maior que 3 vértices
for (int i = 1; i < ps.getVertexCount()-2; i++) { // só antes dos últimos 2
PVector v = ps.getVertex(i);
raio(v.x, v.y, num-3, 0.5); // recursivo 3 vértices mais curto!
}
}
popMatrix();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment