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 › Stuck and Seeking Solution
Page Index Toggle Pages: 1
Stuck and Seeking Solution (Read 213 times)
Stuck and Seeking Solution
Feb 18th, 2009, 12:27am
 
Hey,
I'm trying to build a stripe generator that will pull colors from an array and use the 3 or 4 colors I set to randomly fill my canvas with vertical stripes.  I've got the code set to actually make the stripes, but I can't figure out how to have the colors pulled from the array.

When I have it generate any number of stripes greater than the colors I have set, it crashes.

Here is what I have:

int y;
float speedX = 3.0;
int shapes = 3;
float[]x = new float[shapes];
float[]w = new float[shapes];
float[]h = new float[shapes];
color c1 = #040A27;
color c2 = #17204B;
color c3 = #4C5A9B;
color[]colors = {c1, c2, c3};  // max colors = number of shapes
void setup(){
 size(400, 400);
 frameRate(30);
 noStroke();
 // fill arrays will random values
  for (int i=0; i<shapes; i++){
    x[i]=random(width);
    w[i]=random(15)+4;
    h[i]=w[i];
    //colors[i]=color();
  }
}

void draw(){
 for (int i=0; i<shapes; i++){
   fill(colors[i]);
   rect(x[i], y, w[i], h[i]);
 }
 y+=speedX;
}

and this just creates three random stripes.  Somehow I got to a point where it filled the canvas with shades of grey but I want 3 shades of red, or 3 shades of blue, etc.

Thanks for any help you can give to get me moving again.

Jeff
Re: Stuck and Seeking Solution
Reply #1 - Feb 18th, 2009, 1:48am
 
You get an array index out of bounds, because you are attempting to access an element of the colors[] array greater than [0],[1],or [2]. So if you want to get the color for shape 100, you need to make sure you are still choosing either [0],[1],or [2].

Look up the mod operator (%) (which, in my experience teaching programming, some people have a hard time wrapping their head around). x%y = the remainder of x divided by y. This causes numbers to "fold" back around when you are incrementing. So if you do x%3, no matter what x is, you will get zero, one, or two.

So change the fill command in your draw() loop to:

fill(colors[i%3]);

or, more robustly (in case you change the size of your colors array later):

fill(colors[i%colors.length]);

hope that was what you were looking for....
Re: Stuck and Seeking Solution
Reply #2 - Feb 18th, 2009, 1:50am
 
The problem is you're never checking the value of i against the bounds of the colors array.  Here's a working version with minimal changes:

int y;
float speedX = 3.0;
int shapes = 5;
int numColors = 3;
float[]x = new float[shapes];
float[]w = new float[shapes];
float[]h = new float[shapes];
color c1 = #040A27;
color c2 = #17204B;
color c3 = #4C5A9B;
color[]colors = {c1, c2, c3};  // max colors = number of shapes
void setup(){
 size(400, 400);
 frameRate(30);
 noStroke();
 // fill arrays will random values
  for (int i=0; i<shapes; i++){
    x[i]=random(width);
    w[i]=random(15)+4;
    h[i]=w[i];
    //colors[i]=color();
  }
}

void draw(){
 for (int i=0; i<shapes; i++){
   fill(colors[i % numColors]);
   rect(x[i], y, w[i], h[i]);
 }
 y+=speedX;
}
Re: Stuck and Seeking Solution
Reply #3 - Feb 18th, 2009, 11:48pm
 
Thanks to you both for the replies.  That did indeed work - perfect.  

Jeff
Page Index Toggle Pages: 1