Week 03: Conditionals

Topics:

  • Type: boolean

  • Conditional expressions

  • Relational operators: <, ⇐, >=, >, ==, !=

  • Logical operators: &&, ||, !

  • Conditional statements: if, if/else, multi-way if/else

1. Resources:

1.1. Circle intersection test

/*
 * Aline Normoyle
 * 9/25/2020
 * Print text if mouse is inside a circle
 * This example uses a variable
 */
float radius = 100;
float cx = 250;
float cy = 250;
boolean showText = false;

void setup() {
  size(500,500);
  textSize(32);
  textAlign(CENTER);
}

void mousePressed() {
  if (dist(mouseX, mouseY, cx, cy) < radius) {
    showText = true;
  }
  else {
    showText = false;
  }
}

void draw() {
  background(128);

  fill(255,0,0);
  ellipse(cx, cy, radius*2, radius*2);

  if (showText) {
     fill(0);
     stroke(0);
     text("Inside circle", 250, 250);
  }
}
/*
 * Aline N
 * 9/25/2020
 * Print text if mouse is inside a circle
 * This example checks in draw
 */
float radius = 100;
float cx = 250;
float cy = 250;

void setup() {
  size(500,500);
  textSize(32);
  textAlign(CENTER);
}

void draw() {
  background(128);

  fill(255,0,0);
  ellipse(cx, cy, radius*2, radius*2);

  if (mousePressed) {
    if (dist(mouseX, mouseY, cx, cy) < radius) {
       fill(0);
       stroke(0);
       text("Inside circle", 250, 250);
    }
  }
}

1.2. Height example

/*
 * Aline N
 * 9/25/2020
 * Height multi-way if statement example
 */
int h = 48;
void setup() {
  if (h > 60) {
    println("Taller than 5 ft");
  }
  else if (h > 48) {
    println("Taller than 4 ft");
  }
  else if (h > 36) {
    println("Taller than 3 ft");
  }
  else {
    println("Too small for this ride");
  }
}

1.3. Keyboard input

/*
 * Aline N
 * 9/25/2020
 * Change fill color when any key is pressed
 */
void setup() {
  size(500,500);
  rectMode(CENTER);
}

void draw() {
  background(128);

  if (keyPressed) {
    fill(255,0,0);
  }
  else {
    fill(0,0,255);
  }
  rect(250,250,400,400);
}
/*
 * Aline N
 * 9/25/2020
 * Draw different shapes depending on last key press
 * This example checks in draw()
 */
void setup() {
  size(500,500);
  rectMode(CENTER);
}

void draw() {
  background(128);
  if (key == '1') {
    rect(250, 250, 400, 400);
  }
  else if (key == '2') {
    ellipse(250, 250, 400, 400);
  }
}
/*
 * Aline N
 * 9/25/2020
 * Draw different shapes depending on last key press
 * This example uses a variable
 */
String shapeType = "None";

void setup() {
  size(500,500);
  rectMode(CENTER);
}

void keyPressed() {
  if (key == '1') {
    shapeType = "Rectangle";
  }
  else if (key == '2') {
    shapeType = "Ellipse";
  }
  else {
    shapeType = "None";
  }
}

void draw() {
  background(128);
  if (shapeType == "Rectangle") {
    rect(250, 250, 400, 400);
  }
  else if (shapeType == "Ellipse") {
    ellipse(250, 250, 400, 400);
  }
}

1.4. Bouncing particle

/*
 * Aline N
 * 9/25/2020
 * Particle changes direction when hitting a wall
 */
int posX = 500;
int posY = 0;
int velY = 10;
int velX = 10;

void setup() {
  size(1000,1000);
}

void draw() {
  background(128);

  posY = posY + velY;
  posX = posX + velX;
  if (posY > height) {
    velY = -10;
  }
  if (posY < 0) {
    velY = 10;
  }
  if (posX > width) {
    velX = -10;
  }
  if (posX < 0) {
    velX = 10;
  }

  ellipse(posX, posY, 100, 100);
}

1.5. Is primary color?

/*
 * Aline N
 * 9/25/2020
 * Print out whether a color is primary
 * This solution doesn't work correctly. It prints "X is not primary" too many times.
 */
String colorName = "blue";

void setup() {

  if (colorName == "red") {
    println(colorName + " is primary");
  }
  else {
    println(colorName + " is not primary");
  }

  if (colorName == "blue") {
    println(colorName + " is primary");
  }
  else {
    println(colorName + " is not primary");
  }

  if (colorName == "yellow") {
    println(colorName + " is primary");
  }
  else {
    println(colorName + " is not primary");
  }
/*
 * Aline N
 * 9/25/2020
 * Check if a color is primary -- one of several good approaches
 */
String colorName = "red";

void setup() {
  if (colorName != "red" && colorName != "blue" && colorName != "yellow") {
    println(colorName + " is not primary");
  }
  else {
    println(colorName + " is primary");
  }
}
/*
 * Aline N
 * 9/25/2020
 * Check is a color is primary -- one of several good approaches
 */
String colorName = "purple";

void setup() {
  if (colorName == "red") {
    println(colorName + " is primary");
  }
  else if (colorName == "blue") {
    println(colorName + " is primary");
  }
  else if (colorName == "yellow") {
    println(colorName + " is primary");
  }
  else {
    println(colorName + " is not primary");
  }
}
/*
 * Aline N
 * 9/25/2020
 * Check is a color is primary -- one of several good approaches
 */
void setup() {

  if (colorName == "red" || colorName == "blue" || colorName == "yellow") {
    String message = colorName + " is primary";
    println(message);
  }
  else {
    println(colorName + " is not primary");
  }
}

1.6. Interactive heart demo

/*
 * Aline N
 * 9/25/2020
 * Bezier curve heart with interactive handles
 * Use the mouse to drag the left, top or left, bottom control points
 * Press 'd' to toggle whether the handles are shown
 */
boolean showControls = true;
String selected = "None"; // what control point is selected?
int pointRadius = 25;  // radius for visualizing control points
int cbotX = 50;   // bottom control point
int cbotY = 350;
int ctopX = 75;   // top control point
int ctopY = 40;
int botX = 250;   // end points of bezier curve
int botY = 400;
int topX = 250;
int topY = 150;

void setup() {
  size(500,500);
  textSize(32);
}

void mousePressed() {
  if (dist(mouseX, mouseY, ctopX, ctopY) < pointRadius) {
    selected = "Top";
  }
  else if (dist(mouseX, mouseY, cbotX, cbotY) < pointRadius) {
    selected = "Bottom";
  }
  else {
    selected = "None";
  }
}

void keyPressed() {
  if (key == 'd') {
     showControls = !showControls;
  }
}


void draw() {
  background(255);

  if (mousePressed && showControls) {
    if (selected == "Top") {
      ctopX = mouseX;
      ctopY = mouseY;
    }
    else if (selected == "Bottom") {
      cbotX = mouseX;
      cbotY = mouseY;
    }
  }

  fill(200,0,0);
  bezier(topX,topY, ctopX,ctopY, cbotX,cbotY, botX,botY);
  bezier(topX,topY, width-ctopX,ctopY, width-cbotX,cbotY, botX,botY);

  if (showControls) {
    circle(ctopX, ctopY, pointRadius);
    circle(cbotX, cbotY, pointRadius);
    circle(width-ctopX, ctopY, pointRadius);
    circle(width-cbotX, cbotY, pointRadius);

    line(topX, topY, ctopX, ctopY);
    line(botX, botY, cbotX, cbotY);

    fill(0);
    text("TOP   : "+ctopX+" "+ctopY, 10, height-50);
    text("BOTTOM: "+cbotX+" "+cbotY, 10, height-15);
  }
}