Simple nested loop problem... how to reuse random numbers.. ?

edited October 2013 in How To...

Hi, i got this sketch

http://studio.sketchpad.cc/sp/pad/view/ro.9N7z1RsE2GxhQ/rev.4

and I want :

  • that the first row gets copied to the second row...
  • the second row gets passed to the 3rd row...
  • the 3rd row gets passed to the 4rth and so on...

Now every frame, all the rows get new random values every time... I want that the upper row passes the values to the lower one..

so it looks like if the circles are falling down...

i think i must use an array but i dont know how to continue,... any ideas ?

Thanks...

is like an led screen...

Inspiration came from here :

image alt text

http://media-cache-ak0.pinimg.com/736x/58/69/50/58695008b75b11569b07cf3e24bffea9.jpg

Tagged:

Answers

  • edited October 2013 Answer ✓

    here - the array you describe is a 2D-Array, a field or grid like a chess board.

    It consists of cells. Each of them has a position and a size for the circle. When the lines drop, they keep the position but take the circle size of the line above. Since the 1st line (at the top) is then the same as the 2nd, we initialize the 1st line with new random sizes for the circles.

    The cells are implemented as a class Cell, so we got all its properties and methods in one place. The 2D-array consists of the cells of that class.

    The Code is commented.

    int rows = 40;  
    int cols = 30;
    int rectSize; 
    
    
    Cell [] [] field;  // grid of class Cell (2D Array)
    
    void setup() {
      size (400, 600);
      smooth();
      field = new Cell [cols][rows];
      rectSize = width/cols;
      background(#210303);
      frameRate(2);
      //
      int randomSize;
    
      // define grid 
      for (int x=0; x<field.length; x++) {
        for (int y=0; y<field[1].length; y++) {
          randomSize =   int (random (rectSize));  
          // define one cell
          field [x][y]= new Cell ( x*rectSize+rectSize/2+1, 
                                             y*height/rows+rectSize/2+2, 
                                             randomSize) ;
        }
      }
    
      // test (sets the 1st 22 circles to very small)
      //  int y = 0;
      //  for (int x=0; x<22; x++) {
      //    field [x][y]= new Cell ( x*width/cols+rectSize/2+1, y*height/rows+rectSize/2+2, 1 ) ;
      //  } // for
    } // func 
    
    void draw() {
    
      fill(#210303);
      rect(0, 0, width, height);
    
      fill(#D62923);
      // display grid   
      for (int x=0; x<field.length; x++) {
        for (int y=0; y<field[1].length; y++) {
          field [x][y].display();
        }
      }
      // drop lines 
      // go thru all rows but backwards (from bottom up)
      for (int y=field[1].length-2; y>=0; y--) { 
        // copy this line "y" call by cell: 
        for (int x=0; x<field.length; x++) {
          field [x][y+1].cellSize=field [x][y].cellSize;
        }
      }
      // the top line gets new values now
      for (int x=0; x<field.length; x++) {
        field [x][0].cellSize=  int (random (rectSize));
      } // for
      //
    } // func 
    
    // =======================================
    
    class Cell {
      // one cell in the grid 
      float x, y;
      float cellSize; 
    
      Cell(float x_, float y_, float s_) {
        x=x_;
        y=y_;
        cellSize=s_;
      } // constr 
    
      //
      void display() {
        fill (0, 0);
        stroke (255);
        ellipse (x, y, cellSize, cellSize);
      } // method 
      //
    } // class 
    //
    
  • Awesome, gonna study it, thanks !!! :)

Sign In or Register to comment.