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


chaosGame := proc(numPoints)
	local points, i;
	randomize();
	use geometry in
	RegularPolygon(triSideways, 3, point(cent, [0, 0]), 1);
	rotation(tri, triSideways, Pi/2, counterclockwise);
	randpoint(currentP, -1/2*sqrt(3)..1/2*sqrt(3), -1/2..1/2);
	points := [coordinates(currentP)];
	for i to numPoints do
		midpoint(mid, currentP, parse(cat("rotate_triSideways_", rand(1..3)(), "_tri")));
		points := [op(points), coordinates(mid)];
		point(currentP, coordinates(mid));
	end do:
	end use;
	use plottools in
		plots:-display( seq([plots:-display([seq(point(points[i]), i = 1..j)])], j = 1..numelems(points) ), insequence=true);
	end use;
end proc: