njmcgee
YaBB Newbies
Offline
Posts: 11
Help with pushMatrix and random
Apr 30th , 2007, 3:54am
Hi all, Beneath is a program which draws eight legs and then applies recursion to these legs to create branches. If you take the noLoop() out of setup you will see that the original eight legs animate smoothly but the sub-branches appear only for a moment. I know this is because they are called using random() but I can't get my head around a way to keep them constant. Most things I try result in a popMatrix error. Any ideas? Thanks a lot Nathan ----------------------------------- Leech myLeech; int maxLevel = 200; void setup() { size(400,400); smooth(); noStroke(); myLeech = new Leech(160, 260, 8, 0.97, 0.01); background(0); noLoop(); } void draw() { //translate(200,300); background(0); myLeech.drawCluster(); } class Leech { int xpos; int ypos; int legcount; float leg_sc; float leg_an; float[] scaleinc; float[] angleinc; float[] leg_s; float[] leg_a; Leech(int xp, int yp, int legno, float legscale, float legangle){ xpos = xp; ypos = yp; legcount = legno; scaleinc = new float[legno]; angleinc = new float[legno]; leg_s = new float[legno]; leg_a = new float[legno]; for (int i = 0; i < legcount; i++) { leg_s[i] = random(0.970,0.985); leg_a[i] = random(-0.1,0.1); scaleinc[i] = 0.0001; angleinc[i] = 0.001; } } void drawCluster(){ // this function joins the legs together into a cluster translate(xpos,ypos); // move each leg to the right place for (int i = 0; i < legcount; i++) { pushMatrix(); rotate(i/TWO_PI); // space the legs evenly around a full circle drawleg(i,0,0,150); // legnumber,level,branchdepth,maxlevel popMatrix(); // reset geometry after drawing the leg } } void drawleg(int thisleg, int level, int branchdepth, int maxlevel){ fill(level,level,level); scale(leg_s[thisleg]); rotate(leg_a[thisleg]); translate(0,-5); for(int i =0;i<legcount;i++){ leg_a[i]=leg_a[i]-angleinc[i]; leg_s[i]=leg_s[i]+scaleinc[i]; if (leg_s[i] >= 0.985 || leg_s[i] <= 0.970)scaleinc[i]=scaleinc[i]*-1; if (leg_a[i] >= 0.1 || leg_a[i] <= -0.1)angleinc[i]=angleinc[i]*-1; } ellipse(0,0,30,30); if (random(1.0)<0.020){ // start a branch with pushMatrix(); scale(leg_s[thisleg]); if (branchdepth%2 == 0) { // even numbered branch rotate(-0.8); // rotate to the right //translate(-3,0); } else { rotate(0.8); //translate(3,0); } branchdepth++; drawleg(thisleg,level,branchdepth,maxlevel); popMatrix(); } level++; // increment the level if (level<maxlevel){ // if we're not yet at maxlevel drawleg(thisleg,level,branchdepth,maxlevel); // call the function again } } }