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 › Drawing within a loop
Page Index Toggle Pages: 1
Drawing within a loop (Read 1171 times)
Drawing within a loop
May 11th, 2010, 12:04pm
 
I'm attempting to draw each step of a for loop with rectangles that resize based on a changing value. I don't know how to go about this.

Here is an example of what I'm trying to do:

void drawTest()
{
 for(int i = 0; i < 20; i++)
 {
   rect(20*i,20,20,20*i);
 }
}

void setup()
{
 size(500,500);
 noStroke();
 colorMode(RGB, 255, 255, 255, 100);
 frameRate(1);
}

void draw()
{
 drawTest();
}

What happens is only a single rectangle shows up, not a rectangle for each step each frame.

Help would be much appreciated and highly valued.

EDIT: It seems as though something stores all of the rectangles being printed, and then prints them all in a "lump" at the end of the for loop. Is there any way to make it print step by step without storing the values of "i" in a class or something similar?
Re: Drawing within a loop
Reply #1 - May 11th, 2010, 1:29pm
 
Super classical question...
Nothing is drawn before the end of draw().
Classical solution: use draw() for what it is: a loop, in place for you for loop.
Make i (with a better name) global and increment it on each draw() call.
Don't forget to call background() at the start of draw().
Re: Drawing within a loop
Reply #2 - May 11th, 2010, 2:09pm
 
So if my understanding is correct, draw() should not be called explicitly. So a recursive step-printing, like my proposed "for" printing, would be impossible unless each step was "saved" within a class, correct?

For example, if I wanted to do a tree traversal and print step-by-step to the screen, I would have to somehow save my location and locate it again after I print to the screen. Is this correct? How would I go about doing something like that?
Re: Drawing within a loop
Reply #3 - May 11th, 2010, 2:30pm
 
gravix wrote on May 11th, 2010, 2:09pm:
So a recursive step-printing, like my proposed "for" printing, would be impossible unless each step was "saved" within a class, correct

Technically true, but the sketch itself is a class, and you can just define a variable to store your current position, like so:

Code:

void setup()
{
size(500,500);
noStroke();
colorMode(RGB, 255, 255, 255, 100);
frameRate(1);
}

int i = 0;

void draw()
{
 i++;
 if (i >=20) i = 0;

 rect(20*i,20,20,20*i);

}


Quote:
For example, if I wanted to do a tree traversal and print step-by-step to the screen, I would have to somehow save my location and locate it again after I print to the screen. Is this correct? How would I go about doing something like that?

If by "print to the screen" you mean "display it nicely in the sketch window" then yes.  You'd need to have a way to advance your tree-traversal one step at a time and store the current state in some way after each step.

(If you're simply trying to get the data to your eyes in some kind of usable format for debugging purposes, then I'd recommend using println().)
Re: Drawing within a loop
Reply #4 - May 11th, 2010, 2:39pm
 
Yes, I wanted it to display in the sketch window. I will try to figure out a way to progress my recursion step by step.

Thank you PhiLho and Smitty for your prompt and informed answers. It is greatly appreciated.
Page Index Toggle Pages: 1