We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › passing local variable
Page Index Toggle Pages: 1
passing local variable? (Read 642 times)
passing local variable?
Oct 6th, 2009, 1:41am
 
hi,

i'm relatively new to processing, so this is by far my most ambitious sketch in the environment.
my question goes something like this: is it possible to pass a local variable from the branch-object back to draw()?
i want to put the local variable 'h' in a println() in draw (line 27).

tanks in advance
/claus

Code:

float theta = 12.0;                  
float minLength = 2.0;              
int firstBranch = 250;              

PFont font;

void setup() {
 size(900, 800);
 smooth();
 stroke(1, 255, 40);
 strokeWeight(0.5);
 noLoop();
 background(0);
 font = loadFont("garamond.vlw");    
 textFont(font);                  
}

void draw() {
 theta = radians(theta);            
 translate(width/2, height);      
 line(0, 0, 0, -firstBranch);    
 translate(0, -firstBranch);      
 branch(firstBranch);                
 
 resetMatrix();
 text("angle each step: " + round(degrees(theta)) + " degrees", width-200, height-100);
 //text("branch reduct. / step: " + h, width-200, height-80);
 text("minimum branch length: " + minLength, width-200, height-60);
}

void branch(float h) {
 
 h *= 0.66;                        
   
 if (h > minLength) {                      

   // make branches to the right
   pushMatrix();                    
   rotate(theta);                  
   line(0, 0, 0, -h);                
   translate(0, -h);                
   branch(h);                        
   popMatrix();                      
   
   // make branches to the left
   pushMatrix();                    
   rotate(-theta);                  
   line(0, 0, 0, -h);              
   translate(0, -h);                
   branch(h);                      
   popMatrix();                    
 }
}
Re: passing local variable?
Reply #1 - Oct 6th, 2009, 2:33am
 
Well you could have your branch function return a value:

Code:
float theta = 12.0;                   
float minLength = 2.0;              
int firstBranch = 250;              

PFont font;

void setup() {
 size(900, 800);
 smooth();
 stroke(1, 255, 40);
 strokeWeight(0.5);
 noLoop();
 background(0);
 font = createFont("Arial", 14);    
 textFont(font);                  
}

void draw() {
 theta = radians(theta);            
 translate(width/2, height);      
 line(0, 0, 0, -firstBranch);    
 translate(0, -firstBranch);      
 float foo = branch(firstBranch);
 println(foo);
 
 resetMatrix();
 text("angle each step: " + round(degrees(theta)) + " degrees", width-200, height-100);
 //text("branch reduct. / step: " + h, width-200, height-80);
 text("minimum branch length: " + minLength, width-200, height-60);
}

float branch(float h) {
 
 h *= 0.66;                        
   
 if (h > minLength) {                      

   // make branches to the right
   pushMatrix();                    
   rotate(theta);                  
   line(0, 0, 0, -h);                
   translate(0, -h);                
   branch(h);                        
   popMatrix();                      
   
   // make branches to the left
   pushMatrix();                    
   rotate(-theta);                  
   line(0, 0, 0, -h);              
   translate(0, -h);                
   branch(h);                      
   popMatrix();                    
 }
 return h;
}


The only slight problem you might have is that since branch is recursive (i.e. it makes calls to itself) it may not return the value at the point you wish to display it.
Re: passing local variable?
Reply #2 - Oct 6th, 2009, 2:40am
 
thanks, i'll try it.
Re: passing local variable?
Reply #3 - Oct 6th, 2009, 3:38am
 
You call branch() only once, so at best you will get firstBranch value, no?
Or just put your println calls in branch().
(BTW, that's a function, not an object.)
Re: passing local variable?
Reply #4 - Oct 6th, 2009, 3:57am
 
i couldn't make your solution show me the right float, so i instead made a global variable, and changed the 0.66 in the branch-function with the global variable. then i could put that in my draw() > println().

thanks for your help, though.

Code:

float theta = 12.0;                  
float minLength = 2.0;                
int firstBranch = 250;          
float reduction = 0.66;              

PFont font;

void setup() {
 size(900, 800);
 smooth();
 stroke(1, 255, 40);
 strokeWeight(0.5);
 noLoop();
 background(0);
 font = loadFont("arial.vlw");  
 textFont(font);                    
}

void draw() {
 theta = radians(theta);            
 translate(width/2, height);        
 line(0, 0, 0, -firstBranch);        
 translate(0, -firstBranch);        
 branch(firstBranch);              
 
 resetMatrix();
 text("angle each step: " + round(degrees(theta)) + " degrees", width-200, height-100);
 text("branch reduct. / step: " + reduction, width-200, height-80);
 text("minimum branch length: " + minLength, width-200, height-60);
 
 save("l-system.jpg");
}

void branch(float h) {
 
 h *= reduction;                    
   
 if (h > minLength) {                

   // make branches to the right
   pushMatrix();                    
   rotate(theta);                    
   line(0, 0, 0, -h);                
   translate(0, -h);                
   branch(h);                        
   popMatrix();                    
   
   // make branches to the left
   pushMatrix();                    
   rotate(-theta);                  
   line(0, 0, 0, -h);                
   translate(0, -h);                
   branch(h);                        
   popMatrix();                      
 }
}
Re: passing local variable?
Reply #5 - Oct 6th, 2009, 4:01am
 
PhiLho  wrote on Oct 6th, 2009, 3:38am:
You call branch() only once, so at best you will get firstBranch value, no
Or just put your println calls in branch().
(BTW, that's a function, not an object.)


He makes recursive calls to the branch function from within it, so adding println there will result in lots of output.  I must admit that when I first saw that my instinct was to suggest another approach: I'm sure the same result could be achieved using a loop within the branch function...
Re: passing local variable?
Reply #6 - Oct 6th, 2009, 4:04am
 
clausneergaard wrote on Oct 6th, 2009, 3:57am:
i couldn't make your solution show me the right float, so i instead made a global variable, and changed the 0.66 in the branch-function with the global variable. then i could put that in my draw() > println().


If you just wanted the value initially assigned to h then your solution is obviously the best course of action: it's good practice to avoid 'magic numbers' like that.  You'd normally use the return approach if you wanted the value of h after it had been processed by the function - which clearly doesn't apply here...
Re: passing local variable?
Reply #7 - Oct 6th, 2009, 4:11am
 
yeah - i understand what you're saying.

i think my whole strategy on how to 'build' the code (structure?) was wrong to begin with. so, basically, i was trying to fix something that shouldn't have been structured like that.

cheers,
thanks for your input.
Page Index Toggle Pages: 1