(* packages from Computer Science with Mathematica *)

Needs["CSM`BinaryTree`"]
Needs["CSM`ComplexParametricPlot`"]
Needs["CSM`Aggregate`"]
Needs["CSM`Iterate`"]
Needs["CSM`TuringRecursive`"]
Get["CSM`Collatz2`"]

(* standard packages *)

Needs["Graphics`Animation`"]
Get["FlipBookAnimation`"]

(* not part of this book's packages: *)

Get["CellularAutomata`"]            (* from Mathematica 2.2 or from MathSource *)
Needs["MathProg`FractalExamples`"]  (* The Mathematica Programmer *)
Needs["MathProg`Icosahedra`"]       (* The Mathematica Programmer *)
Needs["MathProg`UniformPolyhedra`"] (* The Mathematica Programmer *)

(* more standard packages *)

Needs["Geometry`Polytopes`"]
Needs["Graphics`Polyhedra`"]

chapter1 :=
    Animate[ FunctionIteration[ 4#(1-#)&, {0.099, 0.1, 0.101}, 0, n, {0, 1},
                                 Frame->True, FrameTicks->None ],
             {n, 1, 9, 1} ];


grid := grid = Table[If[PrimeQ[x + I y], 0, 1], {x, 0, 255}, {y, 0, 255}]

chapter2 := Show[Graphics[Raster[grid]], AspectRatio -> Automatic]


chapter3 := ListPlot[Table[CollatzLength[i],{i,1,1000}]]


randCoeff := 2Random[Integer, {0, 1}] - 1
RandHorner[n_Integer, x_] := Nest[Function[p, p x + randCoeff], 1, n]

chapter4 := ComplexParametricPlot[ RandHorner[20,Exp[I t]]/Sqrt[21],
                {t, 0, 2Pi}, PlotStyle->Thickness[0.002],
                PlotPoints->100, AspectRatio->Automatic ]


Cuboids[n_, m_] := Graphics3D[ Cuboid /@ Table[Random[Integer, {0, m-1}], {n}, {3}] ]

chapter5 := Show[ Cuboids[500, 20] ]


RandomTree[n_] := Tree[ Table[Random[Integer, {0, 3n}], {n}], Identity ]
CSM`BinaryTree`Private`diskr = 0.5 (* make nodes larger   *)
CSM`BinaryTree`Private`ys = 2      (* more vertical space *)

chapter6 := plotTree[ RandomTree[30], DefaultFont->{"Times-Roman", 8},
                      Prolog->Thickness[0.001] ]


`fibx
fib[1] := {Point[{fibx++, 1}]}
fib[2] := {Point[{fibx++, 2}]}
fib[n_] :=
	Module[{n1 = n-1, n2 = n-2, xo = fibx++, a, b, c, d},
		a = Line[{ {xo, n}, {fibx, n1} }]; b = fib[n1];
		c = Line[{ {xo, n}, {fibx, n2} }]; d = fib[n2];
		{Point[{xo, n}], a, b, c, d} ]
FibonacciPlot[n_] :=
	Module[{lines},
		fibx = 0;
		lines = fib[n];
		lines = {PointSize[1.0/fibx], lines};
		Show[ Graphics[Flatten[lines]], Frame->True,
		      PlotRange -> {All, {0.5, n + 0.5}} ] ]

chapter7 := FibonacciPlot[9]


chapter8 := Show[gridGraphics[aggregate[initialGrid[50], 2000]]]


InsertMiddle[l_List] :=
	Flatten[Apply[{#1, (#1+#2)/2}&, Partition[l, 2, 1], {1}], 1] ~Join~ {l[[-1]]}
DoOrDie[ e_, d_, {i_, j_}/;OddQ[i+j] ] := e + Random[Real, {-d, d}]
DoOrDie[ e_, d_, {_, _} ] := e

Refine[ SurfaceGraphics[gr_, opts___], d_ ] :=
	Module[{net},
		net = Map[ InsertMiddle, gr, {0, 1} ];
		net = MapIndexed[ DoOrDie[#1, d, #2]&, net, {2} ];
		SurfaceGraphics[net, opts] ]
Refine[ s_SurfaceGraphics, d_, r_, 0 ] := s
Refine[ s_SurfaceGraphics, d_, r_, n_Integer?Positive ] :=
	Refine[ Refine[s, d], r d/2, r, n-1 ]

graph := graph = Plot3D[ Sin[x y], {x, 0, 3}, {y, 0, 3},
		 PlotPoints->8, DisplayFunction->Identity ]

chapter9 := Show[ Refine[graph, 0.4, 0.9, 3], Axes->None,
                  DisplayFunction->$DisplayFunction ]


automata[n_] :=
    Module[{init, pics},
        init = Table[ Random[Integer, {0, 1}], {n}];
        Block[{$DisplayFunction=Identity},
            pics = Table[ ShowCA[EvolveCA[init, NumberedRule[i], n-1]], {i, 0, 63} ]
        ];
        pics = GraphicsArray[ Partition[pics, 8] ];
        Show[pics, DisplayFunction->$DisplayFunction, AspectRatio->1] ]

chapter10 := automata[40]


SuperStellateFace[face_] :=
    Module[{apex, i, i1, r = Norm[face[[1]]], l = Length[face], nf},
        apex = r Unitize[Plus @@ face / l];
        nf = Table[ i1 = Mod[i, l] + 1;
                    nv = r Unitize[ face[[i]] + face[[i1]] ];
                    {Polygon[{face[[i]], apex, nv} ],
                     Polygon[{nv, apex, face[[i1]]}]},
                   {i, l}];
        Flatten[nf, 1] ]

Norm[v_] := Sqrt[Plus @@ (v^2)]
Unitize[v_] := v/Norm[v]

SuperStellate[thing_] := thing /. Polygon[vl_] :> SuperStellateFace[vl]

chapter11 := Show[ SuperStellate[Polyhedron[Dodecahedron]], Boxed->False ]


chapter12 := PlotTuring[ runList[ {b, m}, pr[0, zero, p[1, 2]] ],
                         Columns->5, DefaultFont->{"Times-Roman", 6.0} ]


chapter13 := Show[ Mandelbrot[4] ]


icosa = {{1, 3}, {36, 18}};

chapter14 := Show[GraphicsArray[Map[Icosahedra[#, Boxed->False]&, icosa, {2}]],
                  GraphicsSpacing -> 0]


polyopts = Sequence @@ {PlotRange->All, Boxed->False}
p1 := p1 = Graphics3D[MakeUniform[ w1[5, 2, 5/2] ], polyopts];
p2 := p2 = Graphics3D[MakeUniform[ w1[5/2, 2, 5] ], polyopts];
p3 := p3 = Graphics3D[MakeUniform[ w1[3, 2, 5/2] ], polyopts];
p4 := p4 = Graphics3D[MakeUniform[ w1[5/2, 2, 3] ], polyopts];

appendixA := Show[ GraphicsArray[{{p1, p2}, {p3, p4}}, GraphicsSpacing->0] ]


appendixB := ParametricPlot[
      (Exp[Cos[phi]] - 2Cos[4phi] + Sin[phi/12]^5)*{Cos[phi], Sin[phi]},
      {phi, 0, 12Pi}, PlotPoints->100, AspectRatio->Automatic, Axes->None ]


index := Show[ Graphics3D[MakeUniform[ w1[2, 5/2, 3] ]] ]
