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 multiple rectangles
Page Index Toggle Pages: 1
drawing multiple rectangles (Read 764 times)
drawing multiple rectangles
Apr 25th, 2009, 3:41pm
 
Hi,

i am an beginner with processing and i dont know what i wrote wrong in my syntax.

I want to write a Program, that diplayes me a specific number of rects in random postions. The Problem is, the Program loops forever, although i used a for loop.


Here is the text:


int Wertx;
int Werty;

void setup(){

 size(200,200);
 smooth();

}
void draw(){

 Wertx = int (random(0,180));
 Werty = int (random(0,180));


 for  (int i = 0; i<7; i = i+1){

   rect (Wertx,Werty,20,20);

 }
}



i hope anybody can help me
Re: drawing multiple rectangles
Reply #1 - Apr 25th, 2009, 4:20pm
 
I notice two things:

1. Each frame (each run through of the draw loop) you come up with a new and x and y location, but you draw seven rectangles all with the same location. This means you are drawing seven identical rectangles all on top of each other. If you want to draw seven rectangles in random locations 'at once', meaning per frame, include the two random lines where you set the locations inside of the for loop.

2. You seem to want to draw a set number of rectangles on the screen and keep them there, that is pick a set of locations and store them. Doing this the easy way for me would include creating a Class of objects that each store a position, and giving them a random location each when I create them in setup()

If you're not familiar with classes, you can do this the manual way (less efficient for you as a typist) and create seven sets of location variables, e.g. x1,y2 x2,y2, etc etc. You then would give them random locations in the setup function, and draw them out in the draw loop.

Hope this helps.
Re: drawing multiple rectangles
Reply #2 - Apr 26th, 2009, 1:08am
 
Something that is often overlooked is that draw() is a loop in itself.
There are several ways to do what you want, eg. the method given by AceFace.
To follow more closely your description of what is expected, you can stop the draw() loop after the wanted number of iterations.
Code:
void setup(){

size(200,200);
smooth();

}
void draw(){

int Wertx = int (random(0,180));
int Werty = int (random(0,180));
rect (Wertx,Werty,20,20);

if (frameCount > 7) {
noLoop();
}

}
Re: drawing multiple rectangles
Reply #3 - Apr 27th, 2009, 3:33pm
 
Ok, thank you now its working Smiley
Re: drawing multiple rectangles
Reply #4 - Apr 29th, 2009, 3:06am
 
Tying the loop to frameCount allowed my computer to draw too many rectangles per frame so when frameCount == 7 (i'm guessing) my sketch window was .. pretty neat looking actually but hundreds of rects. maybe i made a mistake in the code .. i rewrote with a recursive while() for fun:

Code:

int boxes;

void setup() {
 boxes = 0;
 while(boxes < 7) {
   rect(random(100),random(100),20,20);
   boxes++;
 }
}

void draw() {
}


draw() running to make sure the sketch window maintains focus and can be exited with escape. any downside? is noLoop() implied?
Re: drawing multiple rectangles
Reply #5 - Apr 29th, 2009, 8:54am
 
mgs wrote on Apr 29th, 2009, 3:06am:
is noLoop() implied

No. You loop, but do nothing in draw(). At worst, CPU usage might be slightly higher.
Page Index Toggle Pages: 1