Position loop with specific colors

Hi there! I have a loop with 5 quads on random positions and I want to assign a specific color to every quad. How could I control that? Thanks!

void setup() {
  size(1000, 1000);
  background(255);
  
  for(int j=0; j<5;j++){
  if (j>0.2) {
      fill(0,0,128,130);
      }
      else if (j>0.2){
      fill(64,224,208,130);
      }
      else if (j>0.2){
      fill(255,20,147, 130);
      }
      else if (j>0.2){
      fill(128,0,128,130);
      }   
      else{
      fill(255,255,224,130);
      }
  noStroke();
  drawQuad(random(width), random(height),600);
  }

    
  
}




void drawQuad(float cx, float cy, float av_r) {
  PVector[] pts = new PVector[4];
  
  for (int i = 0; i < 4; i++) {
    float angle = random(i * HALF_PI, (i + 1) * HALF_PI);
    float r = random(0.5 * av_r, 1.5 * av_r);
    float x = cx + r * cos(angle);
    float y = cy + r * sin(angle);
    pts[i] = new PVector(x, y);
  }
  
  quad(pts[0].x, pts[0].y, pts[1].x, pts[1].y, pts[2].x, pts[2].y,pts[3].x, pts[3].y);
}
1 Like

Hi,
firstly, some ‘literature’

You could do something like create a color array, size 5 and use

fill( array[ j ] );
drawQuad(1,2,3);

Now to fix your problem.
You are using IF / ELSE statements in a strange way.
Essentially, what you are checking is

  • is j > 0.2?
    • If it is, fill with color 1
    • If it isn’t, check if it is greater than 0.2, which is the same as the first Statement and yields same the same results.

So what could you do to change that?

Well you could check the value with == operator

So you could do this:

int x = 6;

if (x == 0) {
   //code inside the { } runs IF x = 0
}
if(x == 1) {
  //runs if x = 1
}
//and so on.

But since you are not changing the value of X and only want to choose exactly one outcome, you can use ELSE statements, to make the code run faster and just follow good practise.

So the new code would look like

int x = 5;
if(x == 0) {
  //run if x==0
} else if(x == 1) {
  //run if x == 1
} else if(x == 2) {
  //run if x==2
} //and so on.

When any of the IF statements are fulfilled, the code inside the brackets runs and exits the IF-ELSE structure.

A better way to imagine it:

int x = 123;
if(x % 2 == 0) { 
  //run if X is even
} else {
  //x is odd, since if an INTEGER isn't even, its odd.
  
  if(x%3==0)  {
     //runs if X is odd, and divisible by 3
  } else {
     
      if(x % 5==0) {
          //runs if X is odd, is not divisible by 3 and IS divisible by 5
      }
  }
}

I hope this clears things up.

before setup()

color[] array = {
      color (0,0,128,130), 
      color (64,224,208,130),
      ...
      ....
};

Thank you so much. But else if Wil not work without the color array, will It?





void setup() {
  size(1000, 1000);
  background(255);
  
  for(int j=0; j<5;j++){
  int a = 5; 
  if (a==0) {
      fill(0,0,128,130);
      }
      else if (a==1){
      fill(64,224,208,130);
      }
      else if (a==2){
      fill(255,20,147, 130);
      }
      else if (a==3){
      fill(128,0,128,130);
      }   
      else if(a==4){
      fill(255,255,224,130);
      }
  noStroke();
  drawQuad(random(width), random(height),600);
  }

    
  
}




void drawQuad(float cx, float cy, float av_r) {
  PVector[] pts = new PVector[4];
  
  for (int i = 0; i < 4; i++) {
    float angle = random(i * HALF_PI, (i + 1) * HALF_PI);
    float r = random(0.5 * av_r, 1.5 * av_r);
    float x = cx + r * cos(angle);
    float y = cy + r * sin(angle);
    pts[i] = new PVector(x, y);
  }
  
  quad(pts[0].x, pts[0].y, pts[1].x, pts[1].y, pts[2].x, pts[2].y,pts[3].x, pts[3].y);
}

It will absolutely still work.

The approach with arrays was just another solution for the problem, that is dynamic and requires less code.

I didn’t want to spoil it before but yeah.
So the solution with the array would be like this:

color array[] = {#112233,#445566,#778899,  color(11,22,33), color(0) };
for(int i = 0; i < 5; i++) {
   fill(array[i]);
   drawQuad(1,2,3);
}

Define the array, put in 5 things inside, create a for loop, run it 5 times, each time get a different element of the array.


An issue I found with your code:

You are always using the value 5. It doesn’t change. Try to use the for loop’s counter ‘j’.
So what you could do is:

int a = j;

or just remove ‘a’ and replace all the if statements with ‘j== [number]’

1 Like

Thank you so much! It was my first time using arrays but now It has worked for this and I think I undestand It!

2 Likes