Need to create random fill by only one color

dddddd
edited January 2015 in Questions about Code

Hi I have this code where I create random rectangles every frame, I need that at least one random rect turns, one per color, red, yellow, blue & black (the ideal would be to limit them from at least 1 to max. 3). The rest should be white.

void setup() { size (600, 600); strokeWeight (10); strokeCap (SQUARE); rectMode(CORNERS); frameRate(1); } float x; float larg; float alt; float y; int i; int j; int o=20; //no. of vertical rects int v=30; //no. of horizontal rects

void draw () {

background(255); alt=0; stroke(20);

for (i=0; i<=5; i++) {

y=random(alt, height);
larg=0;

if (i==o)
  y=600;

for (j=1; j<=v; j++) {
  x=random(larg, width);

  if (j==v)
    x=600;

  rect(larg, alt, x, y);
  larg=x;
}

alt=y;

} }

Comments

  • here

    void setup() { 
      size (600, 600); 
      strokeWeight (10); 
      strokeCap (SQUARE); 
      rectMode(CORNERS); 
      frameRate(1);
    } 
    float x; 
    float larg; 
    float alt; 
    float y; 
    int i; 
    int j;
    
    int o=20; //no. of vertical rects 
    int v=30; //no. of horizontal rects
    
    void draw () {
    
      background(255); 
      alt=0; 
      stroke(20);
    
      for (i=0; i<=5; i++) {
    
        y=random(alt, height);
        larg=0;
    
        if (i==o)
          y=600;
    
        for (j=1; j<=v; j++) {
          x=random(larg, width);
    
          if (j==v)
            x=600;
    
          fill(255);
    
          if (random(100)>90) 
            fill(255, 2, 2);
          else if (random(100)>90) 
            fill(0, 245, 2);
          else if (random(100)>90) 
            fill(0, 2, 255);
    
          rect(larg, alt, x, y);
          larg=x;
        }
    
        alt=y;
      }
    }
    
  • Man, awesome, thanks a lot! Can I control the number of rects to be filled?

  • edited January 2015

    at the moment it's random

    but you just can increase a counter every time you use a color and only invoke the color lines in the code when counter < 5 or so

    if (counter<5) {
        ///...................
    }
    
Sign In or Register to comment.