Conditional Statements from Albert Schueller on Vimeo.
We finish the mouse circle interaction example using a Boolean expression derived from the distance function. We continue the example using the mouseClicked() function and introduce the other mouse-specific functions.
I'm having some trouble with my code here. I'm not quite hitting the mark yet as the mouse activates the fill action when above, beside, and under the rectangle, in other words - it's not accurate. I'm sure the error is either somewhere in defining the integers or in the conditional statement defining mouse coordinates. The logic is starting to confuse me.
ReplyDeleteHere's my code!
int rectx;
int recty;
int rectsX, rectsY;
void setup()
{
size(500,500);
rectx = 250;
recty = 250;
rectsX = 200;
rectsY = 80;
}
void draw() {
background(125);
noStroke();
if (mouseX > rectx-rectsX && mouseX < recty+rectsY &&
mouseY > rectx-rectsX && mouseY < recty+rectsY) {
fill(random(255),random(255),random(255));
}
else {
fill(255);
}
rectMode(CENTER);
rect(rectx,recty,rectsX,rectsY);
}
This comment has been removed by the author.
DeleteYou're pretty close. You just have to be careful with the if-statement. Since you're using "CENTER" mode, you want to use quantities like rectx-rectsX/2.0 since the reference point is at the center of the rectangle. Also, you're mixing your x and y values, given all that, the new if-statement would be:
Deleteif (mouseX > rectx-rectsX/2.0 &&
mouseX < rectx+rectsX/2.0 &&
mouseY > recty-rectsY/2.0 &&
mouseY < recty+rectsY/2.0)