Code

float rx;
float ry;
float s = 100;
int clicker;


void setup(){

  size(400, 400);
  clicker = 0;

  rectMode(CENTER_DIAMETER);
}

void loop(){
  rx = width/2.0;
  ry = height/2.0;
  
  if (clicker == 0){
  background(0);
  fill(255);
    rect(rx, ry, s, s);
    
    }
   
    else if (clicker == 1) {
      float m= millis();
      background(m % 255);
      fill(0);
      rect(rx, ry, s, s);
      ;
      }
    if (clicker == 2){
    clicker = 0;
    }
    

  }
  
  void mousePressed(){
  if (mouseX > rx-s && mouseX < rx+s &&
  mouseY > ry-s && mouseY < ry+s) {
   clicker ++;
  }
}

020 -- Flipping Square I: State Machines

Statement:Assignments numbered 02x will be due February 1.

This exercise tests your ability to maintain system state in some sort of variable, and also asks you to devise a way of testing the containment of a point (the cursor) within a special region (a reactive square).

In a 400x400 canvas, create a 100x100 pixel reactive square, such that each click in the square flips its color from white to black (if it is white), or from black to white (if it is black). Mouse clicks outside the square should have no effect! This example may be helpful to you.

hide statement