Week 5: Fun­c­t­i­o­n­s

Random Stars

• Show Sketch
``````/** @peep sketchcode */
size(200, 200);
background(204);
strokeWeight(4);
int r = random(10, 250);
int g = random(10, 250);
int b = random(10, 250);
fill(r, g, b);
star(random(200), random(200), 20, 40);
star(random(200), random(200), 20, 30);
star(random(200), random(200), 15, 40);

void star(float x, float y, float inner, float outer) {
// Define some variables to use to draw a star
int points = 5;
// Draw the star shape...
beginShape();
for (int i = 0; i < points; i++) {
// Draw the outer point
float ox = x + outer * cos(i*delta);
float oy = y + outer * sin(i*delta);
vertex(ox, oy);
// Draw the inner point
float ix = x + inner * cos(i*delta + delta/2);
float iy = y + inner * sin(i*delta + delta/2);
vertex(ix, iy);
}
endShape(CLOSE);
}``````

Random Face

• Show Sketch
``````/** @peep sketchcode */
size(200, 200);
smooth();
fill(0);
face(random(20), random(80), random(26));

void face(int x, int y, int gap) {
line(x, 0, x, y);               // Nose Bridge
line(x, y, x+gap, y);           // Nose
line(x+gap, y, x+gap, height);
int mouthY = (height+y)/2;
line(x, mouthY, x+gap, mouthY); // Mouth
ellipse(x-gap/2, y/2, 5, 5);    // Left eye
ellipse(x+gap, y/2, 5, 5);      // Right eye
}``````

Star Loop

• Show Sketch
``````/** @peep sketchcode */
size(200, 200);
background(204);
strokeWeight(1);
//star(100, 100, 20, 40);
//star(30, 80, 20, 30);
//star(150, 50, 15, 40);
for (int i = 8; i < width; i = i+20) {
for (int j = 8; j < height; j = j+20) {
star(i, j, 4, 8);
}
}
void star(float x, float y, float inner, float outer) {
// Define some variables to use to draw a star
int points = 5;
// Draw the star shape...
beginShape();
for (int i = 0; i < points; i++) {
// Draw the outer point
float ox = x + outer * cos(i*delta);
float oy = y + outer * sin(i*delta);
vertex(ox, oy);
// Draw the inner point
float ix = x + inner * cos(i*delta + delta/2);
float iy = y + inner * sin(i*delta + delta/2);
vertex(ix, iy);
}
endShape(CLOSE);
}``````

Translation (Rectangle)

• Show Sketch
``````/** @peep sketchcode */
size(200, 200);
background(204);
rect(0, 0, 30, 40);
translate(30,40);
rect(0, 0, 30, 40);``````

Repeating Transformations

• Show Sketch
``````/** @peep sketchcode */
size(200, 200);
background(204);
translate(100,100);
for (int i = 0; i < 10; i++) {
fill(random(100), random(110), random(200));
ellipse(20, 10, 30, 40);