/* Sketch that uses conditional to determine whether a mouse press lies within a rectangle. If so, the rectangle is recolored. Otherwise, the mouse press is ignored. Author: Michael Goldwasser */ float left, right, top, bottom; // parameters for rectangle boundaries void setup() { size(500, 400); left = 0.25*width; right = 0.75*width; top = 0.25*height; bottom = 0.75*height; drawRectangleRandomly(); } void drawRectangleRandomly() { fill(random(255), random(255), random(255)); rect(left, top, right-left, bottom-top); } void draw() { // nothing to do here } void mousePressed() { // We use "&&" operator to enforce "and" of all conditions if (left <= mouseX && mouseX <= right && top <= mouseY && mouseY <= bottom) { // click is within rectangle drawRectangleRandomly(); } }