Inside a box

This week we started looking at variables and the if statement

We were looking at detecting if the cursor is inside a box. Very useful for things like detecting if the user clicks on a button

This bit of code uses both variables and the if

Lets say we have a box. We know the top left point and the bottom right point. We will call these (p1x,p1y) and (p2x,p2y)

  // Variables here
  int p1x=10
  int p1y=10;
  int p2x=100;
  int p2y=100;

  if (mouseX > p1x  && mouseX < p2x && mouseY > p1y &&  mouseY < p2y ) {
       // Do this code
       fill(255,0,0);
       rect(p1x,p1y,100,100);

  }

  else {

       // dark background here....
       fill(0,0,0);

  }

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s