FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   EXTREMELY basic for() question
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: EXTREMELY basic for() question  (Read 328 times)
eicasm


EXTREMELY basic for() question
« on: Mar 30th, 2004, 2:37am »

shouldn't this code output 16 squares? it does nothing.
if i replace the for() with a loop() it spews squares all over the place but it won't stop... is there something obvious i am overlooking?
 
 
 
void setup() {
  size(400,400);
  background(0);
  noStroke();
}
 
for(int i=0; i < 17; i++) {
  float r = random(16);
  float x = random(400);
  float y = random(400);
     
  rect(x, y, r, r);
}
 
eicasm


Re: EXTREMELY basic for() question
« Reply #1 on: Mar 30th, 2004, 2:57am »

ok -- if i take everything out of setup() it works as expected. why is that?
 
Charles Hinshaw

WWW Email
Re: EXTREMELY basic for() question
« Reply #2 on: Mar 30th, 2004, 5:05am »

everything isn't in setup() -- only the first half. your for() loop is outside of setup(), but if you move it in, so that it looks like:
 
Code:

void setup() {
 
  size(400,400);
  background(0);
  noStroke();
  for(int i=0; i < 17; i ++){
    float r = random(16);
    float x = random(400);
    float y = random(400);
 
    rect(x,y,r,r);
  }
}

 
everything works.
 
just out of curiosity -- why are you using float values for things that seem to be ints?
 
 

Charles Hinshaw
PHAERSE

http://www.phaerse.com
eicasm


Re: EXTREMELY basic for() question
« Reply #3 on: Mar 30th, 2004, 5:09am »

i thought random() only returned floats
 
Charles Hinshaw

WWW Email
Re: EXTREMELY basic for() question
« Reply #4 on: Mar 30th, 2004, 5:22pm »

you could do something like:
 
int r = int(random(16));
 
the int() converts a datatype to an integer.
 
 

Charles Hinshaw
PHAERSE

http://www.phaerse.com
fry


WWW
Re: EXTREMELY basic for() question
« Reply #5 on: Mar 30th, 2004, 6:35pm »

in answer to the original question, you're mixing modes of use for processing.. see http://processing.org/reference/environment/index.html#Programming_modes for more info, but you can either write that code as:
 
Code:

size(400,400);  
background(0);  
noStroke();  
 
for(int i=0; i < 17; i++) {  
  float r = random(16);  
  float x = random(400);  
  float y = random(400);  
 
  rect(x, y, r, r);  
}

 
which in the docs is called 'static' mode. or you can use functions, in your case setup() and draw() (which are special to processing), which does essentially the same thing:
 
Code:

void setup() {
  size(400,400);  
  background(0);  
  noStroke();  
}
 
void draw() {
  for(int i=0; i < 17; i++) {  
    float r = random(16);  
    float x = random(400);  
    float y = random(400);  
   
    rect(x, y, r, r);  
  }
}

 
if you want to add interaction, changing draw() to loop() will allow you to handle the rectangles with mouse input and that sort of thing.  
 
but if you do that, you'll probably want to move the background() command to the beginning of loop(), so that the screen is cleared on each frame.
 
good luck!
 
eicasm


Re: EXTREMELY basic for() question
« Reply #6 on: Mar 31st, 2004, 12:47am »

on Mar 30th, 2004, 5:22pm, Charles Hinshaw wrote:
you could do something like:
 
int r = int(random(16));
 
the int() converts a datatype to an integer.
 

 
 
 
..and i've done this now with a later incarnation of it!
 
and fry, thanks! i knew it was something simple.
 
Pages: 1 

« Previous topic | Next topic »