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 › Working with grids
Page Index Toggle Pages: 1
Working with grids (Read 1174 times)
Working with grids
Nov 19th, 2009, 6:13pm
 
Hello all!
In a nut shell... I have a rect and it's coordenates and size are equal to the cellsize of a grid I have. I have 4 conditional statements inside a for loop colouring the spaces of my grid to imitate tetris pieces.
My problem is that I only have 1 object, which is my rect, the 4 different coloured spaces aren't objects on their own, they're just the result of conditional statements. I need those colours to be 4 seperate objects because i want the white spaces to have noStroke(); and the rest to have a stroke.
As you can probably guess, by the simplicity of this problem, I am not a programmer, so any help I can get is very much appreciated.
Thank you for any help you can give me.
Sincerely Samantha

for (int i = 0; i < cols; i ++ ) { //columns of my grid
   for (int j = 0; j < rows; j ++ ) { //rows of my grid
     stroke(0);
     strokeWeight(2);
     if (grid[i][j] == 0) fill(cor,cor,cor); //white spaces being filled
     if (grid[i][j] == 1) fill(0,0,255); //blue spaces being filled
     if (grid[i][j] == 2) fill(255,0,0); //red spaces being filled
     if (grid[i][j] == 3) fill(255,255,0); //yellow spaces being filled
     rect(i*cellsize, j*cellsize,cellsize,cellsize);

   }
 }
Re: Working with grids
Reply #1 - Nov 19th, 2009, 6:15pm
 
For anyone who would like to see the result of my applet I will display my code here:


int[][] grid;

int cols = 30;
int rows = 30;
int cellsize = 35;
int cor = 255;
 
void setup() {
 size(700, 700);
 smooth();
 grid = new int[cols][rows];
 for (int i = 0; i < cols; i ++ ) {
   for (int j = 0; j < rows; j ++ ) {
     grid[i][j] = 0;
   }
 }
 noLoop();
 
 if (cor == 255) {
    noStroke();
 }


}

void draw(){
 background(255);
 updategrid();
 for (int i = 0; i < cols; i ++ ) {
   for (int j = 0; j < rows; j ++ ) {
     noStroke();
     stroke(0);
     strokeWeight(2);
     if (grid[i][j] == 0) fill(cor,cor,cor);
     if (grid[i][j] == 1) fill(0,0,255);
     if (grid[i][j] == 2) fill(255,0,0);
     if (grid[i][j] == 3) fill(255,255,0);
     rect(i*cellsize, j*cellsize,cellsize,cellsize);

   }
 }
 
 
}



void  updategrid(){
 for (int i = 0; i < cols; i ++ ) {
   for (int j = 0; j < rows; j ++ ) {

     grid[i][j] = int(random(0,4));
   }
 }

}
Re: Working with grids
Reply #2 - Nov 22nd, 2009, 7:46am
 
Hi
I just removed and added a couple of lines to your loop:

Code:

for (int i = 0; i < cols; i ++ ) {
  for (int j = 0; j < rows; j ++ ) {
    stroke(0);
    strokeWeight(2);
    if (grid[i][j] == 0)
       {  
         noStroke();
         fill(cor,cor,cor);
       }
    else stroke(0);  
    if (grid[i][j] == 1) fill(0,0,255);
    if (grid[i][j] == 2) fill(255,0,0);
    if (grid[i][j] == 3) fill(255,255,0);

    rect(i*cellsize, j*cellsize,cellsize,cellsize);
  }
}


When I ran it, I realised why you'd called the colour "cor"!
Anyway, hope this is what you meant
string
Re: Working with grids
Reply #3 - Nov 22nd, 2009, 3:08pm
 
When you've got lots of repeated conditions on the same int variable a switch can be useful:

Code:
 for (int i = 0; i < cols; i ++ ) {
  for (int j = 0; j < rows; j ++ ) {
   
    stroke(0);
    strokeWeight(2);
   
    switch(grid[i][j]) {
      case 0:
        fill(cor,cor,cor);
        noStroke();      
      break;
      case 1:
        fill(0,0,255);      
      break;
      case 2:
        fill(255,0,0);
      break;
      case 3:
        fill(255,255,0);      
      break;
    }
    rect(i*cellsize, j*cellsize,cellsize,cellsize);
  }
}


That way you only call the value from the array once.  Failing that, if the conditions are mutually exclusive, then using else if is also going to be more efficient.

The result is rather Shocked though also reminds me of some of Mondrian's stuff.
Page Index Toggle Pages: 1