Function are huge! They are cool! They are powerful! They are the software equivolent of a robot! Build them, and thy keep working! They can do 'things' automatically, forever. Build it once, the you can use it to build other things (like robots). They take the following general form (in psuedo code):
Make A Function
Let's draw an Owl in a function using the above components:
PROCESSING
Example 9-3: Draw The Owl
void setup () {
size (480, 120);
}
void draw () {
background (176, 204, 226);
translate (110, 110);
stroke (138, 138, 125);
strokeWeight (70);
line (0, -35, 0, -65); // Body
noStroke ();
fill (255);
ellipse (-17.5, -65, 35, 35); // Left eye dome
ellipse (17.5, -65, 35, 35); // Right eye dome
arc (0, -65, 70, 70, 0, PI ); // Chin
fill (51, 51, 30);
ellipse (-14, -65, 8, 8); // Left eye
ellipse (14, -65, 8, 8); // Right eye
quad (0, -58, 4, -51, 0, -44, -4, -51); // Beak
}
Result
PROCESSING
Example 9-4: Two's Company
void setup () {
size (480, 120);
}
void draw () {
background (176, 204, 226);
// Left Owl
translate (110, 110);
stroke (138, 138, 125);
strokeWeight (70);
line (0, -35, 0, -65); // Body
noStroke ();
fill (255);
ellipse (-17.5, -65, 35, 35); // Left eye dome
ellipse (17.5, -65, 35, 35); // Right eye dome
arc (0, -65, 70, 70, 0, PI ); // Chin
fill (51, 51, 30);
ellipse (-14, -65, 8, 8); // Left eye
ellipse (14, -65, 8, 8); // Right eye
quad (0, -58, 4, -51, 0, -44, -4, -51); // Beak
// Right Owl
translate (70, 0);
stroke (138, 138, 125);
strokeWeight (70);
line (0, -35, 0, -65); // Body
noStroke ();
fill (255);
ellipse (-17.5, -65, 35, 35); // Left eye dome
ellipse (17.5, -65, 35, 35); // Right eye dome
arc (0, -65, 70, 70, 0, PI ); // Chin
fill (51, 51, 30);
ellipse (-14, -65, 8, 8); // Left eye
ellipse (14, -65, 8, 8); // Right eye
quad (0, -58, 4, -51, 0, -44, -4, -51); // Beak
}
Result
PROCESSING
Example 9-5: An Owl Function
void setup () {
size (480, 120);
}
void draw () {
background (176, 204, 226);
drawOwl(110, 110);
drawOwl(180, 110);
}
void drawOwl(int xPosition, int yPosition) {
pushMatrix ();
translate (xPosition, yPosition);
stroke (138, 138, 125);
strokeWeight (70);
line (0, -35, 0, -65); // Body
noStroke ();
fill (255);
ellipse (-17.5, -65, 35, 35); // Left eye dome
ellipse (17.5, -65, 35, 35); // Right eye dome
arc (0, -65, 70, 70, 0, PI ); // Chin
fill (51, 51, 30);
ellipse (-14, -65, 8, 8); // Left eye
ellipse (14, -65, 8, 8); // Right eye
quad (0, -58, 4, -51, 0, -44, -4, -51); // Beak
popMatrix ();
}
Result
PROCESSING
Example 9-6: Increasing the Surplus Population
void setup () {
size (480, 120);
}
void draw () {
background (176, 204, 226);
for (int xVal = 35; xVal < width + 70; xVal += 70) {
drawOwl(xVal, 110);
}
}
void drawOwl(int xPosition, int yPosition) {
pushMatrix ();
translate (xPosition, yPosition);
stroke (138, 138, 125);
strokeWeight (70);
line (0, -35, 0, -65); // Body
noStroke ();
fill (255);
ellipse (-17.5, -65, 35, 35); // Left eye dome
ellipse (17.5, -65, 35, 35); // Right eye dome
arc (0, -65, 70, 70, 0, PI ); // Chin
fill (51, 51, 30);
ellipse (-14, -65, 8, 8); // Left eye
ellipse (14, -65, 8, 8); // Right eye
quad (0, -58, 4, -51, 0, -44, -4, -51); // Beak
popMatrix ();
}
Result
PROCESSING
Example 9-7: Owls of Different Sizes
void setup () {
size (480, 120);
}
void draw () {
background (176, 204, 226);
randomSeed (0);
for (int i = 35; i < width + 40; i += 40) {
int randomGray = int (random (0, 102));
float randomScale = random (0.25, 1.0);
drawOwl(i, 110, randomGray, randomScale);
}
}
void drawOwl(int xPosition, int yPosition, int grayVal, float scaleVal) {
pushMatrix ();
translate (xPosition, yPosition);
scale (scaleVal); // Set the size
stroke (138-grayVal, 138-grayVal, 125-grayVal); // Set the color value
strokeWeight (70);
line (0, -35, 0, -65); // Body
noStroke ();
fill (255);
ellipse (-17.5, -65, 35, 35); // Left eye dome
ellipse (17.5, -65, 35, 35); // Right eye dome
arc (0, -65, 70, 70, 0, PI ); // Chin
fill (51, 51, 30);
ellipse (-14, -65, 8, 8); // Left eye
ellipse (14, -65, 8, 8); // Right eye
quad (0, -58, 4, -51, 0, -44, -4, -51); // Beak
popMatrix ();
}
Result