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 › newbie question - multidimensional arrays
Page Index Toggle Pages: 1
newbie question - multidimensional arrays (Read 1849 times)
newbie question - multidimensional arrays
Jun 2nd, 2005, 7:13pm
 
Hi guys,

Like a few people here, I'm coming from an actionscript background, so I'm wondering if this is possible in processing. I want to create a multidimensional array, so I can access a grid-like structure like so:

grid[x][y] = someVal;

In actionscript you'd normally do:

xSquares = ySquares = 10;
var grid:Array = new Array();
for (var i=0;i<xSquares;i++){
  grid[i] = new Array;
  for (var j=0;j<ySquares;j++){
     grid[i][j] = 0;
  }
}

Then access each element as grid[x][y]...

Does this approach work in processing? I can't seem to create an array inside another one. Does the language support this? Is there a correct syntax or workaround to do so?

Thanks in advance,
Alias
Re: newbie question - multidimensional arrays
Reply #1 - Jun 2nd, 2005, 7:21pm
 
yes, it is very possible:

Code:

int [][] coords;
void setup()
{
size(200,200);
coords = new int[width][height];
for(int x = 0; x < width; x++)
for(int y = 0; y < height; y++)
coords[x][y] = (int)random(0,255);
noLoop();
}
void draw()
{
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
stroke(coords[x][y]);
point(x,y);
}
}
}



-seltar
Page Index Toggle Pages: 1