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);
}
}

Advertisements

Fractals and Kindles

I just can’t help myself. Whenever I get a new gadget, I have to customize it. When my wife and daughters gave me a Kindle four years ago, I was thrilled. It’s a Kindle 3, and it opened to me the amazing world of ebooks. My library now includes collections of G. K. Chesterton, Charles Dickens, Fyodor Dostoyevsky, and Shakespeare. I like the fact that the screen isn’t backlit, so there is no eye strain. It’s a wonderful device that has completely changed the way I purchase and read books.

However, I thought the screensavers that Amazon preloaded on the Kindle were really unattractive, so I tried to replace them with images more to my liking. Easier said than done! I assumed that all I had to do was locate the folder containing the screensaver files and dump my own in there. It turns out Amazon does not want you poking around in there, so that folder is hidden.

Fortunately, after a little research online, I was able to hack into my Kindle and change the screensavers. There are thousands of great images online to choose from (just Google “Kindle screensavers”), and I had a blast exploring them. Then it occurred to me that I could create my own fractal screensavers – all that is necessary is to make sure they are gray-scale images that are 600 by 800 in size.

I decided to use Chaoscope to create my screensavers. (I posted a tutorial on how to use Chaoscope here.) Make sure you render them in either Gas or Liquid mode. My first batch is posted below. They are already correctly sized – just click on a thumbnail to access the full-size image, and then save it to your computer. Enjoy!

Make Your Own Fractals for Fun and Profit!

Well, maybe not for profit, but they are a lot of fun, and it isn’t that difficult to create some beautiful images that will impress your friends.

There are lots of programs that generate fractals, many of which are open source. Fractint is the granddaddy of them all; Apophysis is incredibly powerful but overwhelming in its complexity; Fractal Explorer is very versatile. However, I’m going to focus on just one: Chaoscope, because it’s one of the easiest to use, and beginners can get impressive results. After a few minutes you will be up and running, making beautiful fractal art.

I use it to create desktop wallpapers for my computer, and my students asked where they came from. When I told them that I made them, they begged to learn how. When my students ask me to teach them something, I’m not going to turn them down! Even if you know nothing about the mathematics behind fractals, this can be a really rewarding project for your students. (If you are interested in the math, here are the equations.)

This post is merely a quick introduction to Chaoscope. For a more detailed manual, go here.

Okay, let’s make a fractal!

1. Download the installer file here.

2. After you’ve installed Chaoscope, open it. You will see a very boring blank gray window. Click on “File”, then “New” (or hit Ctrl-N):

File New

 

3. The “New” window should pop up. There are lots of things you can do in this window. First, choose what type of fractal you want to work on (Chaotic Flow, Julia, IFS, Icon, Lorenz, etc.). For your first attempt, I suggest Chaotic Flow:

New

 

In this window, you can also set the dimensions of your fractal. I recommend keeping it relatively small, since the smaller they are, the faster they render. Once you get your fractal exactly the way you like, you can change the size to whatever final dimensions you desire.

4. You can also set the render style in this window. Different styles look, well, different. Gas and Liquid are grayscale, while the others are in color. Solid makes your fractal look like a solid object, and you can also change the background color. I recommend you choose Plasma for this first project.

Render

5. We’re still not seeing any fractal, so let’s have Chaoscope generate one for us. Click the “OK” button in the “New” window. Click on the “Attractor” menu, then choose “Search” (or hit F3):

Attractor

 

You should now see a preview window, a “View” window, and an “Attractor” window. You may have something interesting in your preview window, or you may not. If not, just hit F3 until you get a fractal that looks promising. You can click on the fractal and rotate it in any direction. Often, a fractal that initially seems boring can turn into a spectacular one just by changing the viewing perspective. Here are two different views of the same fractal:

Perspective2

 

 

6. Try tweaking the sliders in the “Attractor” window. You will see that a very small adjustment can have a huge effect. Hitting Ctrl-R will put a random color gradient in place. Right-clicking on the color gradients in the “View” window accesses different gradient maps.

7. If you prefer fractals with lots of symmetry, play with Icon attractors. Lorenz attractors have the famous “butterfly” pattern. Julia attractors have a knobby, organic look.

8. When your fractal is perfect, save the parameters. Then set the size to the desired width and height in the “View” window. Hit the F4 button to have Chaoscope create a high-quality rendering. When it’s done, go to “File” and choose “Save Image As…” to save a bmp file of your masterpiece.

I run Windows 8.1 on my tablet, which allows me to set up a slide show to display a collection of desktop wallpapers. Simply right-click in an open area of your desktop, choose “Personalize”, and create your own theme.

Congratulations! You are now a fractal artist!