Coding With Processing, Part 5

Now we’re getting somewhere! In Chapter 7 of Daniel Shiffman’s Learning Processing, I learned how to define a function, which makes coding repeated objects so much easier. For this exercise, I had to define a function (named drawFlower) that drew a flower. I defined variables for the placement of each flower and the color of its petals. Here’s the result:

Here’s the code:

void setup() {
size(600, 400);
}

void draw() {
//Sky-blue background
background(0, 191, 255);

//For the drawFlower function, enter the x-coordinate of the flower’s center,
//the y-coordinate, and the r, g, b values for the petals.
drawFlower(100, 85, color(255, 255, 0));
drawFlower(200, 100, color(255,69,0));
drawFlower(150, 200, color(218,112,214));
drawFlower(300, 225, color(230,230,250));
drawFlower(400, 80, color(250,250,210));
drawFlower(480, 150, color(255,99,71));
drawFlower(440, 230, color(138,43,226));
}

void drawFlower(int centerX, int centerY, color c) {

//Draw the stem
stroke(0, 255, 100);
strokeWeight(4);
line(centerX, centerY + 15, centerX, centerY + 115);

//Draw the leaves
fill(0, 255, 100);
ellipse(centerX-40, centerY+40, 80, 20);
ellipse(centerX+40, centerY+40, 80, 20);

//Draw the center of the flower
stroke(0);
fill(0);
ellipse(centerX, centerY, 30, 30);

//Draw the petals
stroke(c);
fill(c);
ellipse(centerX, centerY+40, 20, 40);
ellipse(centerX-40, centerY, 40, 20);
ellipse(centerX+40, centerY, 40, 20);
ellipse(centerX, centerY-40, 20, 40);

}

Advertisement

Algebra 2 Screencasts!

I’m teaching Algebra 2 for the first time in many years, so I am recording lots of screencasts for it. I’m putting them into a YouTube playlist, and you can access them all here. So far, I’ve covered completing the square, linear inequalities, radical and quadratic form equations, absolute value equations and inequalities, solving quadratic equations, basic tools of graphing, lines, circles, function basics, graphing functions, transformations, and functions operations and composition. (Whew!)

If you’re really bored and want to binge-watch them, here you are:

Transformations of Functions

Well, it’s time to start cranking up the online lectures for my Honors Precalculus classes, and the first one of the year is all about transforming graphs of functions. That means shifting left, right, up, and down, reflecting on x and y-axes, and scale changes. I hope you enjoy my explanation of this exciting topic!