Snowflakes

Visuels Génératifs

 

The code is based on a l-system implementation (http://en.wikipedia.org/wiki/L-system), here is the Processing code to generate the bigger ones, you can press the right arrow to generate a new one, any other key will save the image :


int maxLevel =10;
int numChildren =1;
float angle = PI/4;

Branch trunk;

void keyPressed() {
if (key == CODED && keyCode == RIGHT) {
maxLevel = int(random(5, 25));
angle = PI/(int(random(1, 15))*2);
newTree();
trunk.siz = random(50, 500);
} else {
saveFrame("snowFlake-######.png");
}
}

void setup() {
size(1280, 766);
noFill();
smooth();
newTree();
background(0);
stroke(255);
}

void newTree() {
pushMatrix();
translate(width/2, height/2);
trunk = new Branch(1, 0, 0, 0);
trunk.drawMe();
popMatrix();
}

void draw() {
noStroke();
fill(0, 150);
rect(0, 0, width, height);
//background(0);

stroke(255);
pushMatrix();
translate(width/2, height/2);
trunk.updateMe(0, 0);
trunk.drawMe();
popMatrix();
}

class Branch {
float level, index;
float x, y;
float endx, endy;
float noiseF2;

float siz =100;

Branch[] children = new Branch[0]; //

Branch (float lev, float ind, float ex, float wy) {
level =lev;
index =ind;
updateMe(ex, wy);

if (level<maxLevel) { // avoid infinite loop
children = new Branch[numChildren];
for (int i = 0; i<numChildren;i++) {
children[i] = new Branch(level+1, i, endx, endy);
}
}
}

void updateMe(float ex, float wy) {
x =ex;
y =wy;

float newsiz =map (maxLevel-level, 0, maxLevel, 1, siz);
endx = x;
endy = y-newsiz/3;
//siz -=10;

for (int i =0; i<children.length; i++) {
children[i].updateMe(endx, endy);
}
}

void drawMe() {
//siz-=1;
float newsiz =map (maxLevel-level+1, 0, maxLevel, 2, siz);
for (float i = 0 ; i < TWO_PI ; i+=angle) {
rotate(i);
strokeWeight(map(maxLevel-level+1, 0, maxLevel, 0.005, 0.5));
pushMatrix();
beginShape();
vertex(x-newsiz, y);
vertex(x, y+newsiz*2/3);
vertex(x+newsiz, y);
vertex(x, y+newsiz*1/3);
vertex(x-newsiz, y);
endShape();
popMatrix();
}

for (int i = 0 ; i<children.length;i++) {
//children[i].updateMe();
children[i].drawMe();
}
}
}

					
Publicités

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s