Have a problem with "for" comand

i’m trying to create a code that display a grid with squares and when you put the mouse over a square it disappears, how ever in my code when I put the mouse over a square the row and the column disappear…
here is my code


int objX;
int objY;
int x, y, w, h;
int lastEnteredX = 100000000;
int lastEnteredY = 100000000;
int posX;
int posY; 
void setup() {
  size(1000, 1000);
  x = 20;
  y = 20;
  h = 85;
  w = 85;
}
void draw() {
  background(#000000);
  name();
  objX = mouseX;
  objY = mouseY;
}

  void name() {
    for (int i = 0; i < (10*w +10); i = i + w + 10) {
      for (int j = 0; j < 5*h + 10; j += h + 10) { 
        posX = i;
        posY = j; 

        if (objX < x + w + i && objX > x + i && objY < y + h + j && objY > y + j) {
          println("entered, I: " + i + " J: "+ j + " lastEnteredX: " + lastEnteredX + ", lastEnteredY: " + lastEnteredY );
          lastEnteredX = i;
          lastEnteredY = j;
        }
        if (posX != lastEnteredX  && posY != lastEnteredY) {
          rect(x + i, y + j, w, h);
        }
      }
    }
  }

and also if you know a way to make them disappear for ever it would be great!

1 Like

Please read the tutorial about two-dimensional arrays

Basically you store whether a cell in the grid was clicked

If so, don’t show it or show it differently

2 Likes

Oh thanks, sorry if it seams like a dumb question, I’m just learning

Oh no, it was an excellent question

The tutorial is very good

3 Likes

well I started reading what you said, but I could not stop thinking why it didn’t work the way I expected, so I did a little test… I change this lines

if (posX != lastEnteredX  && posY != lastEnteredY) {

to

if (posX == lastEnteredX  && posY == lastEnteredY) {

and it work as I expected, just the other way around do you know why?
btw will keep reading what you told me it seams that fixes the disappears forever problem

1 Like

Could you explain what you mean by

I doubt that‘s what you were refering to, but if you meant why it was the other way around, that is because the opposite statement of == (equal) is !=(not equal).

1 Like

try this code


int objX;
int objY;
int x, y, w, h;
int lastEnteredX = 100000000;
int lastEnteredY = 100000000;
int posX;
int posY; 
void setup() {
  size(1000, 1000);
  x = 20;
  y = 20;
  h = 85;
  w = 85;
}
void draw() {
  background(#000000);
  name();
  objX = mouseX;
  objY = mouseY;
}

  void name() {
    for (int i = 0; i < (10*w +10); i = i + w + 10) {
      for (int j = 0; j < 5*h + 10; j += h + 10) { 
        posX = i;
        posY = j; 

        if (objX < x + w + i && objX > x + i && objY < y + h + j && objY > y + j) {
          println("entered, I: " + i + " J: "+ j + " lastEnteredX: " + lastEnteredX + ", lastEnteredY: " + lastEnteredY );
          lastEnteredX = i;
          lastEnteredY = j;
        }
        if (posX != lastEnteredX  && posY != lastEnteredY) {
          rect(x + i, y + j, w, h);
        }
      }
    }
  }

and then try this code


int objX;
int objY;
int x, y, w, h;
int lastEnteredX = 100000000;
int lastEnteredY = 100000000;
int posX;
int posY; 
void setup() {
  size(1000, 1000);
  x = 20;
  y = 20;
  h = 85;
  w = 85;
}
void draw() {
  background(#000000);
  name();
  objX = mouseX;
  objY = mouseY;
}

  void name() {
    for (int i = 0; i < (10*w +10); i = i + w + 10) {
      for (int j = 0; j < 5*h + 10; j += h + 10) { 
        posX = i;
        posY = j; 

        if (objX < x + w + i && objX > x + i && objY < y + h + j && objY > y + j) {
          println("entered, I: " + i + " J: "+ j + " lastEnteredX: " + lastEnteredX + ", lastEnteredY: " + lastEnteredY );
          lastEnteredX = i;
          lastEnteredY = j;
        }
        if (posX == lastEnteredX  && posY == lastEnteredY) {
          rect(x + i, y + j, w, h);
        }
      }
    }
  }
1 Like

Ah, i see. So the first Code is what you don‘t want. The second one is what you want, just the other way around.

Just put the whole condition into a bracket and negate it, like this :

if (!(posX == lastX && posY == lastY))
2 Likes

first thank you… second do you know why do this happen?

Yes, this happens, because there is a difference between ( !true && !true ) and ( !(true && true)). To demonstrate this, take a 3*3 grid for example.

if ( x != 1 && y != 1) print(x, y); //where 1 would be the middle (0, 1, 2).length is 3

//this would print : 
// (0,0),       ,(2,0)
//        ,       ,
// (0,2),       ,(2,2)

//while 
if (x == 1 && y == 1) 

//
//         (1,1)
// 

//and then
if ( ! (x == 1 && y == 1)) //this just negates the result of the previously shown one. 
//Because ! is the sign for negation ( !true would be false 
//and this !showRect would not show a rect 
//and !notShowRect would show a rect)

//printed result would be 
//(0,0),(1,0),(2,0)
//(0,1),       ,(2,1)
//(0,2),(1,2),(2,2)

And this probably doesn‘t explain it too well, but i only noticed that halfway through… Anyway, to explain it better, the && means that if one is false, the whole statement is regarded as false, because (true&&false) == false cause && means „are they both“, which they aren’t, while || on the other Hand would mean „is either of them“.

So if you had x = 1 and y = 0 and if ( x != 1 && y != 1 ) it’s the same as if ( false && true ), which (&& basically meaning „are both true?“) results in if (false) and thus not getting triggered even though you only wanted (1,1) to not trigger the if statement…

Now this also happens when using ==, just on a larger scale (on all except the one where both return true, thus triggering the if statement only with that one.

But what we wanted was the exact opposite, so just negating this all gives is what we were looking for.

3 Likes

ohhhhh… thanks good to know, will apply it on further codes

1 Like