CS 101
Variables in Processing
Lecture 6
Draw three circles to resemble this . . .
ICA
Draw three circles to resemble this . . .
size(400, 300);
ellipse(75, 60, 80, 80);
ellipse(175, 60, 80, 80);
ellipse(275, 60, 80, 80);
ICA
Variables
Storing Numbers
Variables
int name = 123;
int name = 123;
All variables begin with the type!
Sticking to only integers for now...
The name of the variable.
Whatever you want (with some restrictions)
Left of equals: the type and name
Right of equals: the value!
Every variable that you create should have the equals!
Choose a number to “store” inside of the variable
Every line ends with a semicolon (with exceptions)
int name = 123;
Using Variables
int xCoord = 57;
int yCoord = 92;
ellipse(xCoord, yCoord, 100, 200);
What values are needed?
size(300, 300);
int xCoord = ? ;
int yCoord = ? ;
int wVal = ? ;
int hVal = ? ;
rect(xCoord, yCoord, wVal, hVal);
ICA
What values are needed?
size(300, 300);
int xCoord = 50;
int yCoord = 150;
int wVal = 200;
int hVal = 75;
rect(xCoord, yCoord, wVal, hVal);
ICA
Back to the three circles
Resize the circles
Take your program from earlier, and change so that the circles are larger and barely touch each-other
ICA
Resize the circles
size(400, 300);
ellipse(75, 60, 100, 100);
ellipse(175, 60, 100, 100);
ellipse(275, 60, 100, 100);
ICA
Resize the circles
size(400, 300);
ellipse(75, 60, 100, 100);
ellipse(175, 60, 100, 100);
ellipse(275, 60, 100, 100);
How many arguments did you have to change to make it look like this?
ICA
Resize the circles
size(400, 300);
ellipse(75, 60, 80, 80);
ellipse(175, 60, 80, 80);
ellipse(275, 60, 80, 80);
size(400, 300);
ellipse(75, 60, 100, 100);
ellipse(175, 60, 100, 100);
ellipse(275, 60, 100, 100);
Resize the circles
size(400, 300);
ellipse(75, 60, 80, 80);
ellipse(175, 60, 80, 80);
ellipse(275, 60, 80, 80);
size(400, 300);
ellipse(75, 60, 100, 100);
ellipse(175, 60, 100, 100);
ellipse(275, 60, 100, 100);
OR . . .
Resize the circles
int diameter = 80;
size(400, 300);
ellipse(75, 60, diameter, diameter);
ellipse(175, 60, diameter, diameter);
ellipse(275, 60, diameter, diameter);
int diameter = 100;
size(400, 300);
ellipse(75, 60, diameter, diameter);
ellipse(175, 60, diameter, diameter);
ellipse(275, 60, diameter, diameter);
Other variables ?
int diameter = 100;
size(400, 300);
ellipse(75, 60, diameter, diameter);
ellipse(175, 60, diameter, diameter);
ellipse(275, 60, diameter, diameter);
How else could we use variables in this program?
ICA
Other variables ?
int diameter = 100;
int y = 60;
size(400, 300);
ellipse(75, y, diameter, diameter);
ellipse(175, y, diameter, diameter);
ellipse(275, y, diameter, diameter);
ICA
Other variables ?
int diameter = 100;
int y = 60;
size(400, 300);
ellipse(75, y, diameter, diameter);
ellipse(175, y, diameter, diameter);
ellipse(275, y, diameter, diameter);
How would one make the canvas look like the one to the right instead?
ICA
Other variables ?
int diameter = 100;
int y = 60;
size(400, 300);
ellipse(75, y, diameter, diameter);
y = 100;
ellipse(175, y, diameter, diameter);
y = 140;
ellipse(275, y, diameter, diameter);
ICA
What will this program produce?
size(400, 400);
int r = 50;
int g = 100;
int b = 150;
fill(r, g, b, 100);
rect(50, 50, 200, 200);
r = r + 100;
fill(r, g, b, 100);
rect(100, 100, 200, 200);
g = g + 100;
fill(r, g, b, 100);
rect(150, 150, 200, 200);
ICA
What will this program produce?
size(400, 400);
int r = 50;
int g = 100;
int b = 150;
fill(r, g, b, 100);
rect(50, 50, 200, 200);
r = r + 100;
fill(r, g, b, 100);
rect(100, 100, 200, 200);
g = g + 100;
fill(r, g, b, 100);
rect(150, 150, 200, 200);
ICA
Math
Math - part I
After each line runs, what value will be stored in each of these variables?
int number1 = 50 + 100 + 200;
int number2 = 500 / 100;
int number3 = 5 * 5 * 2;
int number4 = 100 - 70;
ICA
Math - Part II
After all of these lines run, what value will be stored in each of these variables?
int a = 200;
int b = 10;
int c = 2;
int valueX = a / b + 2;
int valueY = b + a + c - 3 - a;
int valueZ = a * c - 2;
ICA
Order of Operations
Order of Operations
After all of these lines run, what value will be stored in each of these variables?
int valueA = 100 + 200 * 2 / 4;
int valueB = (5 + 5) * 3 - 2;
int valueC = 5 - 100 / (7 + 3) - 10;
ICA
Order of Operations
After all of these lines run, what value will be stored in each of these variables?
int valueA = (5 + 5) * 3 - 2;
int valueB = (5 + 5) * (3 - 2);
int valueC = 5 + (5 * 3) - 2;
ICA
Order of Operations
After all of these lines run, what value will be stored in each of these variables?
int valueA = (5 + 5) * 3 - 2;
int valueB = (5 + 5) * (3 - 2);
int valueC = 5 + (5 * 3) - 2;
28, 10, and 18 !
ICA
Operators and Operands
int name = a + b + 100;
Variable name
Data type
Operators
Operands
Variable assignment
Draw a face
ICA
Draw a face
size(400, 400);
fill(140, 100, 80);
rect(100, 100, 200, 200);
fill(0, 255, 0);
ellipse(160, 150, 20, 20);
ellipse(240, 150, 20, 20);
fill(100, 200, 255);
rect(150, 220, 100, 30);
ICA
Built-In Variables
width and height
size(200, 200);
int d = 100;
ellipse(0, 0, d, d);
ellipse(width, 0, d, d);
ellipse(0, height, d, d);
ellipse(width, height, d, d);
ICA
width and height
size(200, 200);
int d = 100;
ellipse(0, 0, d, d);
ellipse(width, 0, d, d);
ellipse(0, height, d, d);
ellipse(width, height, d, d);
ICA
width and height
size(200, 200);
strokeWeight(10);
line(0, 0, width, height);
line(width, 0, 0, height);
ellipse(100, 100, 70, 70);
ICA
width and height
size(200, 200);
strokeWeight(10);
line(0, 0, width, height);
line(width, 0, 0, height);
ellipse(100, 100, 70, 70);
ICA
Setup and Draw
Setup and Draw
Setup and Draw
Setup and draw
What will this program produce?
void setup() {
size(500, 500);
background(200, 230, 255);
}
void draw() {
strokeWeight(2);
fill(100, 255, 200);
rect(100, 100, 50, 50);
}
ICA
Setup and draw
OK, how about this program?
What have we changed?
int number = 50;
void setup() {
size(500, 500);
background(200, 230, 255);
}
void draw() {
strokeWeight(2);
fill(100, 255, 200);
rect(number, number, 50, 50);
number = number + 1;
}
ICA
Setup and draw
What will this program produce?
void setup() {
size(500, 500);
background(200, 230, 255);
stroke(0, 200, 255);
}
void draw() {
strokeWeight(5);
line(0, 0, mouseX, mouseY);
}
ICA
Setup and draw
ICA
Setup and draw
How do I not leave a trail of lines?
ICA
Setup and draw
void setup() {
size(500, 500);
stroke(0, 200, 255);
}
void draw() {
background(200, 230, 255);
strokeWeight(5);
line(0, 0, mouseX, mouseY);
}
ICA
Setup and draw
ICA
Following the mouse
ICA
Following the mouse
void setup() {
size(500, 500);
}
void draw() {
strokeWeight(2);
fill(100, 200, 200);
rect(mouseX, mouseY, 50, 50);
}
ICA
Move the face
ICA
Draw a face
void setup() {
size(400, 400);
}
void draw() {
background(200);
fill(140, 100, 80);
rect(mouseX, mouseY, 200, 200);
fill(0, 255, 0);
ellipse(mouseX + 60, mouseY + 50, 20, 20);
ellipse(mouseX + 140, mouseY + 50, 20, 20);
fill(100, 200, 255);
rect(mouseX + 50, mouseY + 120, 100, 30);
}
ICA
Checking the value stored in a variable
Checking the value stored in a variable
Printing a variable
ICA
Floats
float height = 6.1;
float weight = 177.45;
float speed = 45.302;
Processing - Materials