Lesson Notes 30

— golan @ 4:45 am

Notes for 9/19

  • 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;
}

Detecting presence of the cursor in a rectangular region:

int buttL, buttR, buttW;
int buttT, buttB, buttH;

void setup(){
  size(200,200);
  buttT = 50;
  buttL = 50;
  buttH = 100;
  buttW = 100;
  buttR = buttL + buttW;
  buttB = buttT + buttH;
}

void draw(){
  background(128);
  
  if ((mouseX > buttL) && (mouseX < buttR)  && 
      (mouseY > buttT) && (mouseY < buttB)){
     fill (0,250,250);
  } else {
     fill (255,255,255);
  }

  rect (buttL,buttT, buttW,buttH); 
}

A toggling region ("on-screen button"):

boolean buttOff;
int buttL, buttR, buttW;
int buttT, buttB, buttH;

//------------------------------------
void setup(){
  size(200,200);
  buttOff = true;
  buttT = buttL = 50;
  buttH = buttW = 100;
  buttR = buttL + buttW;
  buttB = buttT + buttH;
}

//------------------------------------
void mousePressed(){
  if ((mouseX > buttL) && (mouseX < buttR)  && 
      (mouseY > buttT) && (mouseY < buttB)){
     buttOff = !buttOff;
  } 
}

//------------------------------------
void draw(){
  background(128);
  color onColor   = color(0,255,255);  // cyan
  color offColor  = color(0,0,0);      // black
  color whatColorToUse = (buttOff) ? offColor : onColor; // trinary operator!
  fill ( whatColorToUse ); 
  rect(buttL,buttT, buttW,buttH); 
}

0 Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2019 CMU Electronic Media Studio II, Fall 2011, Section A | powered by WordPress with Barecity