Jupyter at Bryn Mawr College |
|||
Public notebooks: /services/public/dblank / CS110 Intro to Computing / 2017-Fall / Notebooks |
New Processing functions:
New ideas:
New Jargon:
call
a functionTYPE NAME;
TYPE NAME = INITIAL_VALUE;
int weight = 120;
int age = 19;
float height = 152.4;
float GRAVITY = 9.8;
int theDogThatBarkedInTheNight = 3;
int the_dog_that_barked_in_the_night = 3;
Let's see an example!
Let's draw a stick person at location (200, 100):
NOTE: separated the numbers that indicate where (x, y) from size
size(400, 300);
// Shadow:
fill(180);
noStroke();
ellipse(200, 100 + 100, 30, 10);
// body and the rest
fill(255);
stroke(0);
// body:
line(200, 100, 200, 100 + 100); // x, y,
// head:
ellipse(200, 100, 100, 100); // (x, y, w, h)
// arms:
line(200 - 50, 100 + 60,
200 + 50, 100 + 60);
Now, consider what you would need to do to:
size(400, 300);
fill(255);
stroke(0);
ellipse(200, 100, 0.5 * 100, 0.5 * 100);
// body:
line(200, 100, 200, 100 + 0.5 * 100);
// arms:
line(200 - 0.5 * 50, 100 + 0.5 * 60,
200 + 0.5 * 50, 100 + 0.5 * 60);
// Shadow:
fill(180);
noStroke();
ellipse(200, 100 + 0.5 * 100, 0.5 * 30, 0.5 * 10);
Consider the problem of drawing a stick person at some location x,y at a particular scale.
Let's say, at location (200, 100) at scale 1.0.
size(400, 300);
// scale is a value indicating the size of stick person
float scale = 0.75;
// x,y are values indicating the location of stick person
int x = 300;
int y = 100;
int bodyLength = 100;
fill(255);
stroke(0);
ellipse(x, y, scale * 100, scale * bodyLength);
// base of body:
float baseX = x;
float baseY = y + scale * bodyLength;
// body:
line(x, y, baseX, baseY);
// arms:
line(x - scale * 50, y + scale * 60,
x + scale * 50, y + scale * 60);
// Shadow:
fill(180);
noStroke();
ellipse(baseX, baseY, scale * 30, scale * 10);
size(400, 300);
// scale is a value indicating the size of stick person
float scale = 0.5;
// x,y are values indicating the location of stick person
int x = 200;
int y = 100;
fill(255);
stroke(0);
ellipse(x, y, scale * 100, scale * 100);
// base of body:
float baseX = x;
float baseY = y + scale * 100;
// body:
line(x, y, baseX, baseY);
// arms:
line(x - scale * 50, y + scale * 60,
x + scale * 50, y + scale * 60);
// Shadow:
fill(180);
noStroke();
ellipse(baseX, baseY, scale * 30, scale * 10);
NOTE: in copy, don't declare the variables again, just assign them
size(400, 300);
// DRAW ONE!
// scale is a value indicating the size of stick person
float scale = 0.25;
// x,y are values indicating the location of stick person
int x = 100;
int y = 50;
fill(255);
stroke(0);
ellipse(x, y, scale * 100, scale * 100);
// base of body:
float baseX = x;
float baseY = y + scale * 100;
// body:
line(x, y, baseX, baseY);
// arms:
line(x - scale * 50, y + scale * 60,
x + scale * 50, y + scale * 60);
// Shadow:
fill(180);
noStroke();
ellipse(baseX, baseY, scale * 30, scale * 10);
// REPEAT!
// scale is a value indicating the size of stick person
scale = 1.0;
// x,y are values indicating the location of stick person
x = 200;
y = 100;
fill(255);
stroke(0);
ellipse(x, y, scale * 100, scale * 100);
// base of body:
baseX = x;
baseY = y + scale * 100;
// body:
line(x, y, baseX, baseY);
// arms:
line(x - scale * 50, y + scale * 60,
x + scale * 50, y + scale * 60);
// Shadow:
fill(180);
noStroke();
ellipse(baseX, baseY, scale * 30, scale * 10);
New way of creating sketches.
Break code up into two functions:
void setup() {
}
void draw() {
}
void setup() {
size(400, 100);
}
void draw() {
// scale is a value indicating the size of stick person
float scale = 0.25;
// x,y are values indicating the location of stick person
int x = 100;
int y = 50;
fill(255);
stroke(0);
ellipse(x, y, scale * 100, scale * 100);
// base of body:
float baseX = x;
float baseY = y + scale * 100;
// body:
line(x, y, baseX, baseY);
// arms:
line(x - scale * 50, y + scale * 60,
x + scale * 50, y + scale * 60);
// Shadow:
fill(180);
noStroke();
ellipse(baseX, baseY, scale * 30, scale * 10);
}
In class, we will transform the following to create a new function, stickPerson(x, y, scale).
void setup() {
size(400, 100);
}
void drawStickPerson(int x, int y, float scale) {
// scale is a value indicating the size of stick person
//float scale = 0.25;
// x,y are values indicating the location of stick person
//int x = 100;
//int y = 50;
fill(255);
stroke(0);
ellipse(x, y, scale * 100, scale * 100);
// base of body:
float baseX = x;
float baseY = y + scale * 100;
// body:
line(x, y, baseX, baseY);
// arms:
line(x - scale * 50, y + scale * 60,
x + scale * 50, y + scale * 60);
// Shadow:
fill(180);
noStroke();
ellipse(baseX, baseY, scale * 30, scale * 10);
}
void draw() {
fill(200, 229, 255);
rect(0, 0, 400, 50); // sky
fill(0, 255, 0);
rect(0, 50, 400, 50); // ground
drawStickPerson(100, 50, 0.25);
drawStickPerson(200, 55, 0.5);
}