Week 3

This week’s highlights:

Covered more variables and data types, boolean logic, and introduction to control flow.

Examples

Circle-drawing stuff:

// Variables defining circle size and color
int diameter = 20;
int colorR = 0;
int colorG = 100;
int colorB = 100;

// Diameter is increased or decreased by this much each keypress
int stepSize = 5;

// Called at start of program
void setup() {
  size(500, 500);
}

// Function called each time any key is pressed
void keyPressed() {
  println("You pressed " + key);
  // Keys: 
  // q: increase diam
  // a: decrease diam
  // r,g,b: switch color to red, green, or blue
  if (key == 'q') {
    diameter = diameter + stepSize;
  }
  if (key == 'a') {
    diameter = diameter - stepSize;
  }
  if (key == 'r') {
    colorR = 255;
    colorG = 0;
    colorB = 0;
  }
  if (key == 'g') {
    colorR = 0;
    colorG = 255;
    colorB = 0;
  }
  if (key == 'b') {
    colorR = 0;
    colorG = 0;
    colorB = 255;
  }
  println("New value of diameter: " + diameter);
}

// Draw a circle wherever the mouse is clicked
void mouseClicked() {
  fill(colorR, colorG, colorB); 
  ellipse(mouseX, mouseY, diameter, diameter);
}

void draw() {
  // This function is called repeatedly
  // for every frame.  You must define this function
  // in order for Processing to know that it should 
  // run a loop to check for new events like mouse clicks
  // and key presses.
}

Box-check homework hints:

// Variables defining location and size of square:
int squareX = 100;
int squareY = 100;
int squareWidth = 100;

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

void mouseClicked() {
    // Check if (mouseX, mouseY) is inside square
    boolean xInSquare = mouseX >= squareX && mouseX <= squareX + squareWidth;
    boolean yInSquare = mouseY >= squareY && mouseY <= squareY + squareWidth;
    if (xInSquare && yInSquare) {
      println("In square");
    }
}

void draw() {
  rect(squareX, squareY, squareWidth, squareWidth);
}

Reading

Chapters 4 and 5 in Getting Started with Processing.
Make sure to run the examples as you go through. Chapter 5 might help with some of the homework.

Homework

Programs are due by midnight on the night of week 4’s class (along with the problem listed for week 2). You can turn in the other exercises on paper at week 4’s class if you don’t want to type them.

Anything listed as optional is not graded, but you should do it anyway.

  1. For each of the expressions below, write out the truth table. Simplify the expression before or after creating the truth table, if possible.

    • (a || b) || (!a || b)
    • (a && b) || (!a || !b)
    • (a && b) || (a && c)
    • (a || b) && (a || c)
  2. Create a Processing program that draws a box, and when you click in the box it prints a message that shows the number of times the box has been clicked.

  3. Create a Processing program that keeps track of the last letter you pressed, and displays it. Make sure you don’t have a bunch of overlapping letters floating around (hint: use the text() and background() functions appropriately).

    Optional: display the last 3 letters you pressed; choose a pair of keys that you can use to increase/decrease the font size (for extra fun: try to keep the letter centered on your screen as you change the size – the textWidth() and textAscent() functions might help); change the color of the background and/or letter each time you press a new key.

  4. Create a Processing program that you can use to draw a picture with selectable colors. There are several ways to go about this. One is to draw boxes that you can click on to select the color you want to draw. Another could use the keyboard (or both). Make sure to provide instructions (in comments, or in a help message).

    Some more hints: make use of the special Processing variables mouseX, mouseY, and mousePressed. Start out simple (like with only one color), and add as you go along.

    Optional: also provide a way to select brush size and/or shape. Some more ideas: create special brush types, like the airbrush tool you see in some paint programs; create an eraser; provide a way to enter text to be drawn on the screen.