(*********************************************************************

	Copyright 1989 by Roman E. Maeder
	
	Adapted from
	Roman E. Maeder: Programming in Mathematica, Addison-Wesley, 1989.

	Permission is hereby granted to make copies of this file for
	any purpose other than direct profit, or as part of a
	commercial product, provided this copyright notice is left
	intact.  Sale, other than for the cost of media, is prohibited.
	
	Permission is hereby granted to reproduce part or all of
	this file, provided that the source is acknowledged.

 *********************************************************************)


Needs["RMPackages`ComplexMap`"]
Needs["RMPackages`NewParametricPlot3D`"]
Needs["Graphics`Shapes`"]
Needs["Graphics`Polyhedra`"]
Needs["RMPackages`RungeKutta`"]
Needs["RMPackages`RandomWalk`"]

(* fix bug with FaceForm[GrayLevel[...]] in V1.2 *)
Unprotect[GrayLevel];
GrayLevel[lev_] := RGBColor[lev, lev, lev];
Protect[GrayLevel];


(* Moebius transform *)

chapter1 := PolarMap[ (2#-I)/(#-1)&, {0.001, 5.001, 0.25}, {0, 2Pi, Pi/15},
                      Framed->True ]

(* Minimal surface *)

chapter2 :=
	ParametricPlot3D[{r*Cos[phi] - (r^2*Cos[2*phi])/2,
		-(r*Sin[phi]) - (r^2*Sin[2*phi])/2, (4*r^(3/2)*Cos[(3*phi)/2])/3},
		{r, 0.0001, 1, 0.9999/8}, {phi, 0, 4Pi, Pi/12}]

(* rotaionally symmetric parametric surface *)

chapter3 :=
    ParametricPlot3D[
	{r Cos[Cos[r]] Cos[psi], r Cos[Cos[r]] Sin[psi], r Sin[Cos[r]]},
	{r, 0.001, 9Pi/2 + 0.001, Pi/16}, {psi, 0, 3Pi/2, Pi/16}]

(* Fractal tile *)

iterations = 5;
om7 = N[-1+Sqrt[-3]]/2; l7=om7-2
r7 = {0, 1,-1,om7,-om7,om7+1,-om7-1}
g7[x_] := Flatten[Outer[Plus, r7 , l7 x]]

chapter4 := (
	points = Point[{Re[#],Im[#]}]& /@ Nest[g7, {0.}, iterations];
	graph4 = Graphics[Prepend[points, PointSize[0.003]]];
	Show[graph4, AspectRatio->1, Axes->None, Framed->True]
	)

(* Sphere with random holes *)

chapter5 := Show[ Graphics3D[Select[Sphere[][[1]], Random[]>0.5&]] ]

(* Saddle surface *)

chapter6 := CylindricalPlot3D[r^2 Cos[2 phi],
		{r, 0, 1/2, 1/16}, {phi, 0, 2Pi, 2Pi/24}]

(* Van-der-Pol equation *)

chapter7 :=
	Block[{vdp, eps = 1.5, x, xdot},
		vdp = RungeKutta[{xdot, eps (1 - x^2) xdot - x}, {x, xdot},
		                 {2, 0}, {4Pi, 0.05}];
		ListPlot[vdp, PlotJoined->True, AspectRatio -> Automatic]
	]


(* Fourier approximations of saw-tooth *)

l5 = Table[ Sum[Sin[i x]/i, {i, n}], {n, 5} ];

chapter9 := Plot[ Release[l5], {x, -0.3, 2Pi+0.3}, Framed->True ]

(* spiral with varying radius *)

chapter10 :=
	ParametricPlot3D[{r (1 + phi/2) Cos[phi], r (1 + phi/2) Sin[phi], -phi/2},
		{r, 0.1, 1.1, 0.125}, {phi, 0, 11Pi/2, Pi/12}]

(* diagonally shaded surface *)

chapter11 :=
	SphericalPlot3D[ {Sin[theta],
		FaceForm[GrayLevel[0.05 + 0.9 Sin[2theta + phi]^2],
		         GrayLevel[0.05 + 0.9 Sin[2theta - phi]^2]]},
		{theta, 0, Pi, Pi/24}, {phi, 0, 3Pi/2, Pi/12},
		Lighting->False ]

(* Random walk *)

appendixA := RandomWalk[5000]

(* Great icosahedron, vertices computed from icosahedron *)

AdjacentTo[face_, flist_] := Select[flist, Length[Intersection[face, #]] == 2&]

Opposite[face_, flist_] :=
	Block[ {adjacent, next},
		adjacent = AdjacentTo[ face, flist ];
		next = AdjacentTo[#, flist]& /@ adjacent;
		next = Complement[#, {face}]& /@ next;
		Flatten[ Intersection @@ #& /@ next ]
	]

AppendTo[Polyhedra, GreatIcosahedron]

GreatIcosahedron/: Vertices[GreatIcosahedron] = Vertices[Icosahedron];

GreatIcosahedron/: Faces[GreatIcosahedron] =
	Opposite[#, Faces[Icosahedron]]& /@ Faces[Icosahedron];

appendixB := Show[ Polyhedron[GreatIcosahedron] ]
