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 › Problem with grid...
Page Index Toggle Pages: 1
Problem with grid... (Read 337 times)
Problem with grid...
Oct 22nd, 2009, 6:55am
 
Hi!
I'm an almost complete beginner with Processing and programming at all, but I'm interested in learning. However i've got really stuck at a tiny program i've created. It's just a grid board which you can click in to fill in the squares. The problem I'm having is that if I first click gridX[0] gridY[0] then click gridX[0] gridY[2] then gridX[0] gridY[1] gets filled in too! I really can't get my head around this one, so if anyone wants to help i would be really grateful. Thanks!

Code:

boolean gridX[] = new boolean[200];
boolean gridY[] = new boolean[200];

void setup() {
 frameRate(10);
 size(200,200);
 background(255);
 for (int x = 0; x < width; x++) {
   for (int y = 0; y < height; y++) {
     line(x * 10, 0, x * 10, height);    
     line(0, y* 10, width, y * 10);
     }
   }
}
void draw() {

 for (int x = 0; x < width; x++) {
   for (int y = 0; y < height; y++) {
     if( ( gridX[x] && gridY[y] ) == true) {
       fill(0);
       rect(x * 10, y * 10, 10, 10);
     }
   }
 }
}

void mousePressed() {
     if (!(gridX[mouseX/10] && gridY[mouseY/10])) {
     gridX[mouseX/10] = true;
     gridY[mouseY/10] = true;
   }
}
Re: Problem with grid...
Reply #1 - Oct 22nd, 2009, 7:13am
 
You should use a two dimensional array, not 2 one dimensional arrays.

Like so:

boolean grid[][] = new boolean[20][20];
Re: Problem with grid...
Reply #2 - Oct 22nd, 2009, 7:19am
 
Your data (gridX and gridY) don't represent a two-dimensional grid but two one-dimensional vectors.

You need something like
boolean grid[][] = new boolean[20][20];
to represent a grid of 20x20 cells.

If you click on a square, you currently do not activate a single grid cell,
grid[x][y] = true;
but one column of your grid,
gridX[x] = true;
and one row of your grid,
gridY[y] = true;

In your draw() function, you don't ask whether a certain grid cell is activated, but whether the row and the column which define the position of this cell are activated.

This modification of your draw() visualizes this:
Code:
void draw() {

 for (int x = 0; x < width; x++) {
   for (int y = 0; y < height; y++) {
     if( ( gridX[x] || gridY[y] ) == true) {
       fill(200);
       rect(x * 10, y * 10, 10, 10);
     }
     if( ( gridX[x] && gridY[y] ) == true) {
       fill(0);
       rect(x * 10, y * 10, 10, 10);
     }
   }
 }
}
Re: Problem with grid...
Reply #3 - Oct 22nd, 2009, 7:21am
 
Of course! I should've thought of that! Thanks for your help and quick answers. This is what i wound up with:
Code:

boolean grid[][] = new boolean[200][200];

void setup() {
 frameRate(10);
 size(200,200);
 background(255);
 for (int x = 0; x < width; x++) {
   for (int y = 0; y < height; y++) {
line(x * 10, 0, x * 10, height);    
line(0, y* 10, width, y * 10);
}
   }
}
void draw() {

 for (int x = 0; x < width; x++) {
   for (int y = 0; y < height; y++) {
if( grid[x][y] ) {
 fill(0);
 rect(x * 10, y * 10, 10, 10);
}
   }
 }
}

void mousePressed() {
if (!(grid[mouseX/10][mouseY/10])) {
 grid[mouseX/10][mouseY/10] = true;
   }
}


thanks again!
Page Index Toggle Pages: 1