The Chaos Game is a method of generating the attractor of an iterated function system (IFS).

One of the best-known and simplest examples creates a fractal, using a polygon and an initial point selected at random.



Task
Play the Chaos Game using the corners of an equilateral triangle as the reference points.   Add a starting point at random (preferably inside the triangle).   Then add the next point halfway between the starting point and one of the reference points.   This reference point is chosen at random.

After a sufficient number of iterations, the image of a Sierpinski Triangle should emerge.



See also
 The Game of Chaos


To run:
Start up.
Initialize our reference points.
Clear the screen to the lightest gray color.
Play the chaos game.
Refresh the screen.
Wait for the escape key.
Shut down.
 
To play the chaos game:
Pick a spot within 2 inches of the screen's center.
Loop.
Draw the spot.
If a counter is past 20000, exit.
Pick a reference spot.
Find a middle spot of the spot and the reference spot.
Put the middle spot into the spot.
Repeat.
 
To find a middle spot of a spot and another spot:
Put the spot's x coord plus the other spot's x coord divided by 2 into the middle spot's x coord.
Put the spot's y coord plus the other spot's y coord divided by 2 into the middle spot's y coord.
 
The top spot is a spot.
The left spot is a spot.
The right spot is a spot.
 
To initialize our reference points:
Move up 2-1/2 inches.
Put the context's spot into the top spot.
Turn right. Turn 1/6 of the way around.
Move 5 inches.
Put the context's spot into the right spot.
Turn 1/3 of the way around.
Move 5 inches.
Put the context's spot into the left spot.
 
To pick a reference spot:
Pick a number between 1 and 3.
If the number is 1, put the top spot into the reference spot.
If the number is 2, put the right spot into the reference spot.
If the number is 3, put the left spot into the reference spot.