Lecture 06b

  • modulo (%) arithmetic
  • case, switch
  • if/mouse: make a button
  • click, set a boolean

The Trinary (or Ternary) Comparison Operator (see http://en.wikipedia.org/wiki/Ternary_operation):

void setup() {
  size(400, 200);
}
 
void draw() {
  int g = (mouseX < 200) ? 0 : 255;
  background (g);
}

Modulo Arithmetic: the % operator

size(400, 100);
 
background(255);
for (int i=0; i

Trigger an event every N frames, using modulo arithmetic:

void setup() {
  size(400, 100);
}
 
void draw() {
  fill(0);
 
  int x = frameCount % 100;
  if (x == 0){
    background(255,0,0);
    text("bang!!", 200,50);
 
  } else {
    background(129);
    text(x, 200,50);
  }
}

Periodic movement (animation) using Modulo Arithmetic:

float x = 0; 
 
void setup() {
  size(400, 100);
}
 
void draw() {
  background(255);
  fill(0);
 
  x = x + 2.7;
  float pos = x % width;
  ellipse (pos, 50, 20,20);
}

Periodic color animations with Modulo Arithmetic:

void setup() {
  size(400, 150);
}
 
void draw() {
 
  int moddedCounter = frameCount % 100;
  float grayLevel = map(moddedCounter, 0,100, 0,255);
  background(grayLevel); 
 
  fill (255-grayLevel); 
  text(moddedCounter, width/2, height/2); 
}

Switch{} statement as alternative to a large number of if/else{}:

char theKey;
 
//=======================
void setup() {
  size(400, 400);
}
 
//=======================
void keyPressed() {
  theKey = key;
  println("At " + millis() + " milliseconds, you pressed " + theKey);
}
 
//=======================
void draw() { 
  background(200);
 
  switch (theKey) {
 
  case 'e':
  case 'E':
    if (theKey == 'e') {
      strokeWeight(1);
    } else {
      strokeWeight(5);
    }
    ellipse(200, 200, 100, 100);
    break;
 
  case 'r':
  case 'R':
    rect (100, 150, 200, 100); 
    break;
 
  case 's':
    rect (150, 150, 100, 100);
    break;
 
  default:
    break;
  }
 
}

Comparison: Random position, non-drunk walk:

void setup() {
  size (300, 300);
}
 
void draw() {
  background(200);
 
  float rx;
  float ry; 
  rx = width/2;
  ry = height/2;
 
  rx = rx + random(-10, 10);
  ry = ry + random(-10, 10); 
  ellipse(rx, ry, 40, 40);
}

Comparison: Random position, drunk walk — achieved through proper scoping of (persistent) variables:

float rx;
float ry; 
 
void setup() {
  size (300, 300);
  rx = width/2;
  ry = height/2;
}
 
void draw() {
  background(200);
  rx = rx + random(-10, 10);
  ry = ry + random(-10, 10); 
  ellipse(rx, ry, 40, 40);
}

A simple toggle, achieved through negation of a boolean state variable:

boolean isTheBackgroundBlack; 
 
void setup(){
  size(200,200);
  isTheBackgroundBlack = true;
}
 
void draw(){
  if (isTheBackgroundBlack){
    background(0,0,0); 
  } else {
    background(255,200,200);
  }
}
 
void mousePressed(){
   isTheBackgroundBlack = ! isTheBackgroundBlack;
}

Back and Forth Linear Motion:

// back-and-forth motion
float xPos;
float myDirection  = 1;
void setup() {
  size(300,300); 
  xPos = 0; 
}
 
void draw() {
  background(255); 
  fill(255,0,0);
 
  float mySpeed = mouseX / 100.0;
  xPos = xPos + myDirection * mySpeed;
  if (xPos > width){
    myDirection = -1;
  }
  if (xPos < 0){
    myDirection =  1;
  }
  ellipse(xPos, 150, 40,40);
}
Posted in