Sierpinski Gasket Generator
Each point's color is based on the last couple of vertices choosen. The number of vertices of "memory" will determine how many colors get used.
zoom = 200
numpoints = 40000
What is Sierpinski's Gasket?
It is a fractal. It has been related to Pascal's triangle and Cantor Dust and many other things. Rather than regenerate all the details here, I suggest you check out this site at The University of Western Australia. Also, this Google search has a long list of sites.
I like that there is an easy random process that generates the figure. In simple terms, it works as follows. Take a triangle with three equal sides (i.e. equalateral). Pick some point in the triangle and mark it. Pick one of the three vertices at random, go half way from your point towards that vertex, and call this your new point. Draw it. Repeat: pick a vertex, go half way towards it, call that your new point, draw it. What ends up emerging after a few thousand points is Sierpinski's Gasket. See the section of the University of Western Australia link above entitled "Chaos game" for a mathematical description of this process.
(Computer geek comments) The only thing I have added is some memory. Color your vertices red, green and blue. If you go towards the blue vertex, color your dot blue. If you go towards the green vertex, then color your dot green. Likewise for red. This approach yields a three color Sierpinski Gasket. Instead, I take the color of the current dot, divide its red, green and blue components by 2, then add half the value of the vertex I have choosen for the next point. Thus, choosing the red vertex several times in a row yields a dot with very little blue or green, but lots of red. If you follow that with a single blue vertex, the dot will still have lots of red, but now also have lots of blue - a strong purple. The pattern in the colors comes about because each point is determined by the previous vertex choices. The only way to get close to a given point is to have the same previous choices of vertices and thus a similiar color. In fact, due to the dots on the computer picture being "big" mathematically, we only need to keep track of the previous 7 or 8 vertices. Conviently, this is how many I can distinguish easily with 8 bits of red, green and blue.
Personal History
When you learn a new computer language your first program almost always does nothing more than print "Hello, World!" A long time ago, when I was in graduate school, I took to writing Sierpinski's gasket generation programs as my first graphics program in each new computer language I learned. I did this on what was at that time a wicked cool graphics display hooked up to a VAX. I wrote it in Postscript and printed on now ancient LaserWriters (very small printout would take 30 minutes - very fun to irritate my fellow students). I recall something about a C version. I implemented it in Perl. I suspect I did some other versions that I have forgotten --- there is some memory left of a version in Awk that generated a LaTeX file with embedded PicTeX commands which falls into the list of versions I would like to forget. Now I have added PHP!
One interesting version was not a program. When my wife taught 7th grade mathematics, we put up a bulletin board covered with paper - 44 inches square. We drew the triangle. The kids would walk up, pick a vertex. They had to measure from the push pin halfway to their choosen vertex and move the push pin. Then they marked the old spot with a black marker. After a week, Sierpinski's Gasket started to appear --- despite the one or two kids trying to "mess things up" and the mistakes in measuring. The process is self correcting enough for those errors not to matter.