Cantor Set and Koch Snowflake

The Cantor Set

Deterministic construction.

f[1][{x_, y_}]:= {x/3, 0} //N
f[2][{x_, y_}]:= {(2 + x)/3, 0} //N
K[0] = {{0, 0}, {1, 0}};

K[1]=Table[Map[f[k], K[0]], {k, 1, 2}];
K[2]=Flatten[Table[Map[f[k], K[1][[j]]], {j, 1, 2}, {k, 1, 2}], 1];
K[3]=Flatten[Table[Map[f[k], K[2][[j]]], {j, 1, 2^2}, {k, 1, 2}], 1];
K[4]=Flatten[Table[Map[f[k], K[3][[j]]], {j, 1, 2^3}, {k, 1, 2}], 1];
K[5]=Flatten[Table[Map[f[k], K[4][[j]]], {j, 1, 2^4}, {k, 1, 2}], 1];
K[6]=Flatten[Table[Map[f[k], K[5][[j]]], {j, 1, 2^5}, {k, 1, 2}], 1];

Graphics[Line[K[6]], AspectRatio-> 1/12]

The Chaos Game.

MapRandom[{x_, y_}]:= f[RandomChoice[{1, 2}]][{x, y}]
ListIterates=NestList[MapRandom, {1, 0}, 30000];

Graphics[{PointSize[.001], Point[ListIterates]}, AspectRatio->1/12]

The Koch Snowflake

Deterministic construction.

ftn[1][{x_ , y_}]:={x/3, y/3}//N
ftn[2][{x_ , y_}]:={x/3+2/3, y/3}//N
ftn[3][{x_, y_}]:={{Cos[Pi/3]/3, -Sin[Pi/3]/3}, {Sin[Pi/3]/3, Cos[Pi/3]/3}}.{x, y}+{1/3, 0}//N
ftn[4][{x_, y_}]:={{Cos[Pi/3]/3, Sin[Pi/3]/3}, {-Sin[Pi/3]/3, Cos[Pi/3]/3}}.{x, y}+{1/2, Sqrt[3]/6}//N


K[0]={{0, 0}, {1, 0}};
K[1]=Table[Map[ftn[k], K[0]], {k, 1, 4}];

Graphics[Line[K[1]]]

K[2]=Flatten[Table[Map[ftn[k], K[1][[j]]], {j, 1, 4}, {k, 1, 4}], 1];
K[3]=Flatten[Table[Map[ftn[k], K[2][[j]]], {j, 1, 4^2}, {k, 1, 4}], 1];
K[4]=Flatten[Table[Map[ftn[k], K[3][[j]]], {j, 1, 4^3}, {k, 1, 4}], 1];
K[5]=Flatten[Table[Map[ftn[k], K[4][[j]]], {j, 1, 4^4}, {k, 1, 4}], 1];
K[6]=Flatten[Table[Map[ftn[k], K[5][[j]]], {j, 1, 4^5}, {k, 1, 4}], 1];
K[7]=Flatten[Table[Map[ftn[k], K[6][[j]]], {j, 1, 4^6}, {k, 1, 4}], 1];

Graphics[Line[K[7]]]

The Chaos Game.

MapRandom[{x_, y_}]:= ftn[RandomChoice[{1, 2, 3, 4}]][{x, y}]
ListIterates=NestList[MapRandom, {1, 0}, 300000];
Graphics[{PointSize[.001], Point[ListIterates]}]

Pythagoras Tree
The squares are scaling down by a factor of cos(a) at each iteration.

a = Pi/4;

f[1][{x1_, y1_}]:={Cos[a]^2 x1 - Cos[a]Sin[a]y1, Cos[a]Sin[a]*x1 + Cos[a]^2*y1 + 1}//N (* Scale down by Cos[a] and rotate by ``a" radians, then shift up to allow the square to sit in the correct location *) 
f[2][{x1_, y1_}]:={Sin[a]^2 x1 + Cos[a]Sin[a]y1+ Cos[a]^2, -Cos[a]Sin[a]*x1 + Sin[a]^2 y1 + 1 + Cos[a]Sin[a]}//N 
f[3][{x1_, y1_}] := {x1, y1}//N    (*This final map is the identity, to allow us to see the entire tree, not just the leaves created at the final iteration*)

L[0]={{0, 0}, {0, 1}, {1, 1}, {1, 0}};
L[1]=Table[Map[f[k], L[0]], {k, 1, 3}];
L[2]=Flatten[Table[Map[f[k], L[1][[j]]], {j, 1, 3}, {k, 1, 3}], 1];
L[3]=Flatten[Table[Map[f[k], L[2][[j]]], {j, 1, 9}, {k, 1, 3}], 1];
L[4]=Flatten[Table[Map[f[k], L[3][[j]]], {j, 1, 27}, {k, 1, 3}], 1];
L[5]=Flatten[Table[Map[f[k], L[4][[j]]], {j, 1, 3^4}, {k, 1, 3}], 1];
L[6]=Flatten[Table[Map[f[k], L[5][[j]]], {j, 1, 3^5}, {k, 1, 3}], 1];
L[7]=Flatten[Table[Map[f[k], L[6][[j]]], {j, 1, 3^6}, {k, 1, 3}], 1];

Graphics[Polygon[L[1]]]