# Coding With Processing, Part 3

I just learned about for loops in Processing, and it occurred to me that they are perfect for illustrating iterated function systems. The classic example of an iterated function system is where you start with three vertices of a triangle, labeled 1, 2, and 3. Then perform the following steps:

1. Plot a point (x0, y0) anywhere inside the triangle.
2. Use a random number generator to generate 1, 2, or 3 randomly.
3. If 1 comes up, plot the midpoint between vertex #1 and (x0, y0).
4. If 2 comes up, plot the midpoint between vertex #2 and (x0, y0).
5. If 3 comes up, plot the midpoint between vertex #3 and (x0, y0).
6. Make your new point (x0, y0).
7. Repeat the process.

Even though the locations of the points are being generated randomly, a very interesting picture emerges. Here is after 100 iterations:

And here is the result after 1000 iterations:

And here it is after 10,000. It’s pretty clear that we’re getting the Sierpinski Triangle!

Here’s the code:

float x1 = width/2;
float y1 = 0;
float x2 = 0;
float y2 = height;
float x3 = width;
float y3 = height;

float x0 = width/2;
float y0 = height/2;

size(600, 600);
background(0);
stroke(255);
frameRate(5);

for (int i = 1; i < 10000; i = i + 1) {
int roll = int (random(0, 4));
if (roll == 1) {
x0 = (x0 + x1)/2;
y0 = (y0 + y1)/2;
point(x0, y0);
} else if (roll == 2) {
x0 = (x0 + x2)/2;
y0 = (y0 + y2)/2;
point(x0, y0);
} else {
x0 = (x0 + x3)/2;
y0 = (y0 + y3)/2;
point(x0, y0);
}
}

# Coding With Processing, Part 2

I’m continuing to work through Daniel Shiffman’s book, Learning Processing, and I have just finished the chapter on conditionals. I wrote a program that animates a rectangle and ellipse, and depending on their positions in the window their colors and shapes change. You can click here to see it run in openprocessing.org.

Here’s a gif of 3 seconds of it in action:

# Computer Coding With Processing

Coding is the hot topic in education these days, so I decided to teach myself a computer language. I chose Processing, because it creates nice computer graphics, which I think will appeal to my students. I’m using Daniel Shiffman’s book, Learning Processing, which is very entertaining and understandable. He assumes the reader knows nothing about coding, and that’s a good thing in my case!

Processing uses simple commands like ellipse(centerX, centerY, width, height), or line(X1, Y1, X2, Y2). If you’re familiar with coordinate geometry, then coding in Processing is a piece of cake. And did I mention it’s open-source, which means it’s completely free? The Processing website (linked above) has a fantastic reference that covers all of the commands and their parameters.

Here is my first project, which is the result of working through the first three chapters. I think I’ll call him Hypno-Dog, because I made his eyes change color.

# Light, Math, and Color – 2017 Projects

I just finished teaching another Light, Math, and Color minicourse. Twenty students researched a math topic and illustrated it with a stained glass window. Their projects this time around are really spectacular, especially considering these are first tries. (Note: If you hover over a picture, the math topic it illustrates will show up.)