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 › linear motion & recursion
Page Index Toggle Pages: 1
linear motion & recursion (Read 534 times)
linear motion & recursion
Dec 25th, 2008, 8:04pm
 
Hi, I'm working on a simple program. The lines that are generated are supposed to move towards the top of the screen. I'm trying to use recursion to replicate the lines. However, I can't seem to get them to move. Can anyone tell me what I'm doing wrong?

void setup() {
 size(1000,800);
 
 background (255);
 smooth();
 drawLines (5,1000);
}

void drawLines (int x, int num) {
 float y = 10.0;
 float speed = 10;
 strokeWeight (random(.25,5));
 y=y+speed;
 line (x,400-y,x,800);
 
 if (num>0) {
   drawLines (x+5, num-1);
 }
}


Re: linear motion & recursion
Reply #1 - Dec 25th, 2008, 10:10pm
 
you should draw your stuff in the draw() method.

Code:
void setup() {
size(1000,800);
smooth();
}

void draw() {
background (255);
drawLines (5,1000);
}

void drawLines (int x, int num) {
float y = 10.0;
float speed = 10;
strokeWeight (random(.25,5));
y=y+speed;
line (x,400-y,x,800);

if (num>0) {
drawLines (x+5, num-1);
}
}
Re: linear motion & recursion
Reply #2 - Dec 25th, 2008, 11:28pm
 
In both cases, nothing will be animated: after a long while (the computation of all the function calls), the lines will be displayed at once. In the draw() function, the process will be repeated, with only some variations on line weight.

Here, recursion is probably overkill. You should revise the method to draw incrementally, ie. the function should be designed to draw more on each draw() call.
Page Index Toggle Pages: 1