Apr 26, 2010

Discovering Fractals: Brownian Trees

After I got back into generating fractals yesterday, I decided to do up another interesting one. This one is called the Brownian Tree. The difference between this one and all the other ones that I've done is that this one is stochastically generated, where the other ones are all deterministic. This means that each image is random, so I can't tell you how to generate each one exactly the same - although technically since computers use pseudorandom numbers and not real random numbers, I could just give you the seed and you're set. However I didn't record the seeds.

The way this one works is based on something in reality. You start with a world. Within this world, you fix a seed particle. Then you repeatedly add new particles to the world and have them float around. When the new particle bumps into the seed it becomes part of the seed.
In pseudo-C, it would look something like this:
bool world[SIZE][SIZE]; // assume it is pre-set to false
world[rand() % SIZE][rand() % SIZE] = true;

for (int i = 0; i < NUM_PARTICLES; i++){
particle = [rand() % SIZE, rand() % SIZE];

while (true){
projection = particle + random direction
if (projection out of bounds){
// do something
}if (world[projection] == true){
world[particle] = true;
break;
}
particle = projection;
}
}
plot(world);
What this ends up doing is generating structures that look really organic. Here's an example:It looks like a shrubbery!

So two things to note about the algorithm. When the particle goes out of bounds, I just put it in some random other spot in the world. Also when the particle collides with the structure, I keep track of how long it took to get there (it resets to 0 when it goes out of bounds). Based on how long it takes, I give it a different colour. This leads to the nice layering effects that you see there.

You can change this algorithm in quite a number of ways. Here's a modification where when it bumps into the side of the window, it just sticks there:Or one with multiple seeds:Or one where instead of using a point for the seed, you use a collection of points in a ring (I stole this idea from the Wikipedia page):One thing I read briefly in my searching is that you can also generate music using fractals. This would be quite interesting, and would force me to learn how audio files work! Maybe I will post something on it at some point.

1 comment:

Michael Mol said...

Cool stuff. I don't suppose you'd be interested in creating it as a task over at Rosetta Code, would you? I'd be curious to see how different languages tackle the problem of the particles' motion. (Especially where they might use implicit or explicit parallelism.)