# Sketchy Traingle to Square

By: Matthen on Nov 8th, 2011  |  syntax: None  |  size: 1.56 KB  |  hits: 1,936  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. Clear[line];
2. width = 50;
3. yskip = 1;
4. contract = 0.99;
5. line[k_]["line"][0] = Table[{i, 0}, {i, width}];
6. line[k_]["square"][0] = Map[# - {width/2, width/2} &, Reverse@Join[
7.      Table[{i, 0}, {i, width}],
8.      Table[{width, i}, {i, width}],
9.      Table[{width - i, width}, {i, width}],
10.      Table[{0, width - i}, {i, width}]
11.      ]];
12. line[k_]["triangle"][0] = Map[# - {width/2, Sqrt[3] width/4} &, Join[
13.     Table[{i/2, i Sqrt[3]/2}, {i, 0, width, 3/4}],
14.     Table[{width/2, width Sqrt[3]/2} + {i/2, -i Sqrt[3]/2}, {i, 0,
15.       width, 3/4}],
16.     Table[{width - i, 0}, {i, 1, width, 3/4}]
17.     ]];
18.
19. WavyCopy[line_, transform_] :=
20.   Map[transform,
21.    line + RandomReal[NormalDistribution[0, 0.1], Dimensions[line]]];
22. line[k_]["line"][n_] :=
23.   line[k]["line"][n] =
24.    WavyCopy[line[k]["line"][n - 1], # - {0, yskip} &];
25. line[k_]["square"][n_] :=
26.   line[k]["square"][n] =
27.    WavyCopy[line[k]["square"][n - 1], # contract &];
28. line[k_]["triangle"][n_] :=
29.   line[k]["triangle"][n] =
30.    WavyCopy[line[k]["triangle"][n - 1], # contract &];
31.
32. nlines = 25;
33. type1 = "square";
34. type2 = "triangle";
35. f[nlines_, n_, "line"][{x_, y_}] := {x, yskip nlines - y}
36. f[nlines_, n_, "square"][{x_, y_}] := ({x, y}) contract^(3 n - nlines)
37. frame[nmin_] := Graphics[{FaceForm[], EdgeForm[Black],
38.    Table[Polygon[
39.       MovingAverage[((nlines - n)/nlines) line[0][type1][
40.          n] + (n/nlines) Map[f[nlines, n, "square"],
41.          line[1][type2][nlines - n]], 2]
42.      ]
43.     , {n, nmin, nlines}]
44.    }, PlotRange -> 25, ImageSize -> 300];
45.
46. Manipulate[frame[nmin],{nmin,0,nlines}]
47.