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 falseWhat this ends up doing is generating structures that look really organic. Here's an example:It looks like a shrubbery!
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);
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:
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.)
Post a Comment