Random LoadShape array

If there's any advice about loading an array of PShapes and controlling random colour changing on the svg's I'd be very grateful:

PShape[] he = new PShape[4];
PShape[] m = new PShape[16];

void setup() {
  size(900, 1000, P2D);
  smooth(2);

  /* for ( int i = 4; i< he.length; i++ ){
   he[i] = loadShape("he" + nf(1, 4) + ".svg"); // svgs in data folder
   } */

  // load all shapes at the same time ?


  he[0] = loadShape("he1.svg");
  he[1] = loadShape("he2.svg");
  he[2] = loadShape("he3.svg");
  he[3] = loadShape("he4.svg");

  m[0] = loadShape("m1.svg");
  m[1] = loadShape("m2.svg");
  m[2] = loadShape("m3.svg");
  m[3] = loadShape("m4.svg");
  m[4] = loadShape("m5.svg");
  m[5] = loadShape("m6.svg");
  m[6] = loadShape("m7.svg");
  m[7] = loadShape("m8.svg");
  m[8] = loadShape("m9.svg");
  m[9] = loadShape("m10.svg");
  m[10] = loadShape("m11.svg");
  m[11] = loadShape("m12.svg");
  m[12] = loadShape("m13.svg");
  m[13] = loadShape("m14.svg");
  m[14] = loadShape("m15.svg");
  m[15] = loadShape("m16.svg");

  frameRate(0.2);
}

void draw() { //random color working locally ?

  background(random(0, 255), random(0, 255), random(0, 255)); //colour behind shapes

  shape(ha[(int)random(ha.length)], 20, 40, 800, 800); // coordinates X/Y and size W/H
  ha[(int)random(he.length)].disableStyle(); // changes svg colour
  fill(random (0, 255), random (0, 255), random (0, 255));
  noStroke();

  shape(m[(int)random(m.length)], 20, 40, 800, 800); 
  m[(int)random(m.length)].disableStyle(); 
  fill(random (0, 255), random (0, 255), random (0, 255));
  noStroke();
}


void mousePressed() { //stops program ( bit slow )
  noLoop();
}

void mouseReleased() {
  loop();
}
Tagged:

Answers

  • edited August 2014

    The code loooks... a bit random!

    You have a he array, but sometime you refer to it as ha. If you show code, you should try making it to compile...

    You draw a random shape, then you disable the style of another (probably) random shape, after the drawing. Not sure what you try to do there... If it is intended to be the same shape, you probably want:

    1. To put the random number in a variable, to use several time without having its value to change on each usage!
    2. To disable the style before drawing the shape.

    Actually, you got the order wrong for fill() and noStroke(): everything must be done before drawing the shape!

    The business about loop() and noLoop() is a bit suspicious, too. But if it works...

    Oh, and instead of writing 16 lines of code for loading shapes, let the program work for you: do a loop.

    BTW, to format code, select it and hit the C button or Ctrl+K.

  • edited August 2014

    Thank you for your comments - it's a first post on a first program - apart from the he/ha mistake it does compile. I want to load arrays of svg's, draw random PShapes to the screen, disable and fill the same shape randomly, but locally, changing them all every 3/4 seconds... I can't seem to get the disableStyle to work on the randomly selected shape ...

        PShape[] he = new PShape[4];
        PShape[] m = new PShape[16];
    
        void setup() {
          size(900, 1000, P2D);
          smooth(2);
          noStroke();
          frameRate(0.2);
    
          /*for (int i = 4; i < he.length; i++) {  
           he[i] = loadShape("he" + nf(1, 4) + ".svg");
           }*/
    
          // want to load all shapes at the same time ( for loop ) ?
    
    
    
          he[0] = loadShape("he1.svg");
          he[1] = loadShape("he2.svg");
          he[2] = loadShape("he3.svg");
          he[3] = loadShape("he4.svg");
    
          m[0] = loadShape("m1.svg");
          m[1] = loadShape("m2.svg");
          m[2] = loadShape("m3.svg");
          m[3] = loadShape("m4.svg");
          m[4] = loadShape("m5.svg");
          m[5] = loadShape("m6.svg");
          m[6] = loadShape("m7.svg");
          m[7] = loadShape("m8.svg");
          m[8] = loadShape("m9.svg");
          m[9] = loadShape("m10.svg");
          m[10] = loadShape("m11.svg");
          m[11] = loadShape("m12.svg");
          m[12] = loadShape("m13.svg");
          m[13] = loadShape("m14.svg");
          m[14] = loadShape("m15.svg");
          m[15] = loadShape("m16.svg");
        }
    
        void draw() { 
    
          background(random(0, 155), random(0, 155), random(0, 155)); 
    
          he[3].disableStyle(); 
          fill(random (0, 255), random (0, 255), random (0, 255)); 
          shape(he[(int)random(he.length)], 20, 40, 800, 800);
    
          m[15].disableStyle(); 
          fill(random (0, 255), random (0, 255), random (0, 255)); 
          shape(m[(int)random(m.length)], 200, 440, 300, 300);
        }
    
        void mousePressed() { //stops program ( but v. slow )
          noLoop();
        }
    
        void mouseReleased() {
          loop();
        }
    
  • Answer ✓

    As said previously, you have to disable the style of the shape you must display, not on an arbitrary shape while you display another.

    I suggest to do this in the setup() (in place of your 16 lines!):

    for (int i = 0; i < m.length; i++)
    {
      m[i] = loadShape("m" + (i + 1) + ".svg");
      m[i].disableStyle();
    }
    

    Thus, no need to call disableStyle() in draw() again.

  • Thank you so much - it works a treat !

Sign In or Register to comment.