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 › 2D Array Question (simple)
Page Index Toggle Pages: 1
2D Array Question (simple) (Read 712 times)
2D Array Question (simple)
Oct 22nd, 2009, 7:32pm
 
I am trying to teach myself 2D arrays but have got stumped upon an issue that I cannot figure out.

I want to draw a 10x10 grid, with one 2D array holding the x,y values for each square in the grid and the other holding the color value.

I can get the color value one working fine, but the x and y is really problematic.

Cannot convert from int to int[].

Am I thinking about this the wrong way?

Thanks,
-TB

Code:

int [][] cells;
int [][] cellColor;
int cellSize = 10;

void setup(){
 size(100,100);
 colorMode(HSB, 100,100,100);
 int cols = width/cellSize;
 int rows = height/cellSize;
 cells = new int[cols][rows];
 cellColor = new int[cols][rows];
}

void draw(){
 int hueVal = 0;
 for(int i = 0; i < cells.length; i++){
   for(int j = 0; j < cells.length; j++){
     cells[i] = i*cellSize;
     cells[j] = j*cellSize;
     cellColor[i][j] = hueVal ++;
     fill(cellColor[i][j], 100, 100);
     rect(cells[i], cells[j], cellSize, cellSize);
     
    //the below line works, but wont allow me to refer back to each
   // square by array position
    //rect(i*cellSize,j*cellSize, cellSize, cellSize);

   }
 }

}
Re: 2D Array Question (simple)
Reply #1 - Oct 22nd, 2009, 11:21pm
 
You can't store both x and y values of a grid in a two-dimensional int array.

A way to do what you want is the following:
It uses a three-dimensional array to store x and y values of the grid.
Code:

int [][][] cells;
int [][] cellColor;
int cellSize = 10;

void setup(){
 size(100,100);
 colorMode(HSB, 100,100,100);
 int cols = width/cellSize;
 int rows = height/cellSize;
 cells = new int[cols][rows][2];
 cellColor = new int[cols][rows];
}

void draw(){
 int hueVal = 0;
 for(int i = 0; i < cells.length; i++){
   for(int j = 0; j < cells.length; j++){
     cells[i][j][0] = i*cellSize;
     cells[i][j][1] = j*cellSize;
     cellColor[i][j] = hueVal ++;
     fill(cellColor[i][j], 100, 100);
     rect(cells[i][j][0], cells[i][j][1], cellSize, cellSize);
   }
 }
}


But in this particular case I would not use an array to store the x and y values, because calculating them is so easy:
Code:

int [][] cellColor;
int cellSize = 10;

void setup(){
 size(100,100);
 colorMode(HSB, 100,100,100);
 int cols = width/cellSize;
 int rows = height/cellSize;
 cellColor = new int[cols][rows];
}

void draw(){
 int hueVal = 0;
 for(int i = 0; i < cellColor.length; i++){
   for(int j = 0; j < cellColor[i].length; j++){
     cellColor[i][j] = hueVal ++;
     fill(cellColor[i][j], 100, 100);
     rect(getX(i), getY(j), cellSize, cellSize);
   }
 }
}

int getX(int i)
{
 return i*cellSize;
}

int getY(int j)
{
 return j*cellSize;
}
Re: 2D Array Question (simple)
Reply #2 - Oct 23rd, 2009, 12:06am
 
just another thing. if you want to make your sketch more flexible. like rescaling it. you have to adjust the color change. By now, it turns red and stays red after you counted up to 100. If you wanna expand it over the whole sketch you can remap it like this

    hueVal = (int)map(i*cols+j,0,cols*rows,0,100);
Re: 2D Array Question (simple)
Reply #3 - Oct 23rd, 2009, 10:09pm
 
Thank you both!
Page Index Toggle Pages: 1