(************** Content-type: application/mathematica **************) Notebook[{ Cell[BoxData[{ RowBox[{ StyleBox[\( (*\ carry\ out\ the\ forward\ and\ backward\ moves\ and\ the\ various\ \ rotations\ by\ updating\ the\ global\ location\ ' Lpos'\ and\ direction\ angle\ ' \(\(Ltheta'\)\(.\)\)\ *) \), FormatType->StandardForm], StyleBox[" ", FormatType->StandardForm], StyleBox[\(\(\(Lmove[\ z_String, \ Ldelta_\ ]\ := \ Switch[\ z, \ "\<+\>", \ \(Ltheta\ += \ Ldelta;\), \ "\<-\>", \ \(Ltheta\ -= \ Ldelta;\), \ "\", \ Lpos\ += \ {Cos[Ltheta], \ Sin[Ltheta]}, \ "\", \ Lpos\ -= \ {Cos[Ltheta], \ Sin[Ltheta]}, \ _\ , \ Lpos\ += \ 0. \ ]\) \); \ \(\(LSystem::usage\ = \ "\"\) \); \ (*\ make\ the\ \(string : \ starting\ with\ ' axiom'\), \ use\ StringReplace\ the\ specified\ number\ of\ times\ *) \ \ \(\(LSystem[\ axiom_, \ rules_List, \ n_Integer, \ Ldelta_: N[90\ Degree]]\ := \ Nest[\ StringReplace[#, \ rules] &, \ axiom, \ n\ ]\) \); \ \(\(Off[General::spell1]\) \); \ (*\ initialize\ the\ position\ ' Lpos'\ and\ the\ direction\ angle\ ' Ltheta'; \ create\ the\ Line\ graphics\ primitive\ represented\ by\ the\ L - system\ by\ mapping\ ' Lmove'\ over\ the\ characters\ in\ the\ L - string, \ deleting\ all\ the\ Nulls; \ then\ show\ the\ Graphics\ object\ *) \ \(\(LShow[lstring_String, \ Ldelta_: N[90\ Degree]]\ := \ \((Lpos = {0. , 0. }; \ Ltheta = 0. ; \ Show[\ Graphics[ Line[\ Prepend[\ DeleteCases[\ Map[Lmove[#, \ Ldelta] &, \ Characters[lstring]], \ Null\ ], \ {0, 0}]]], \ AspectRatio\ -> \ Automatic])\)\) \); \ (*\ same\ as\ above, \ plus\ a\ list\ of\ colors\ for\ each\ segment\ contained\ in\ ' \ \(\(templist'\)\ --\)\ unfortunately, \ \(\('\) \(templist'\)\(\ \)\(isn'\) \ \(t\)\(\ \)\(really\ '\) \(temp'\)\), \ but\ stays\ in\ memory\ as\ a\ global\ variable; \ so\ sue\ me\ *) \ \(\(LShowColor[lstring_String, \ Ldelta_: N[90\ Degree]]\ := \ \((Lpos\ = \ {0. , \ 0. }; \ Ltheta\ = \ 0. ; \ templist\ = \ Map[\ Line, \ Partition[\ Prepend[\ DeleteCases[\ Map[\ Lmove[#, \ Ldelta] &, \ Characters[lstring]\ ], \ Null], \ {0, 0}], \ 2, 1]]; \ ncol\ = \ N[Length[templist]]; \ huelist\ = \ Table[\ Hue[k/ncol], \ {k, \ 1. , \ ncol}\ ]; \ Show[\ Graphics[\ N[Flatten[Transpose[{huelist, \ templist}]]]], \ AspectRatio\ -> \ Automatic])\)\) \); \ \(\(On[ General::spell1]\) \); \ \(\(LShowColor[\ (*\ Koch\ curve\ *) \ LSystem["\", \ {"\"\ -> \ "\"}, \ 4], \ N[60\ Degree]]\) \); \ \(\(LShowColor[\ (*\ Peano\ curve\ *) \ LSystem["\", \ {"\"\ -> \ "\"}, \ 4]]\) \); \ \(\(LShowColor[\ (*\ Quadratic\ Koch\ island\ *) \ LSystem["\", \ {"\"\ -> \ "\"}, \ \ 3]]\) \); \ \(\(LShowColor[\ (*\ 32 - segment\ curve\ *) \ LSystem["\", \ {"\"\ -> \ \ "\<-F+F-F-F+F+FF-F+F+FF+F-F-FF+FF-FF+F+F-FF-F-F+FF-F-F+F+F-F+\>"}, \ 2]]\) \); \ \(\(LShowColor[\ LSystem["\", \ (*\ Sierpinski\ arrowhead\ *) \ {"\"\ -> \ "\", \ \ "\"\ -> \ "\"}, \ 7], \ N[60\ Degree]]\) \); \ \(\(LShowColor[\ (*\ Peano - Gosper\ curve\ *) \ LSystem["\", \ {"\"\ -> \ "\", \ \ "\"\ -> \ "\<-FX+YFYF++YF+FX--FX-Y\>"}, \ 4], \ N[60\ Degree]]\) \); \ \(\(LShowColor[\ (*\ Sierpinski\ triangle\ *) \ LSystem["\", \ {"\"\ -> \ "\", \ "\" -> \ "\<--FXF++FXF++FXF--\>"}, \ 6], \ N[60\ Degree]]\) \); \ \(\(LShowColor@ LSystem["\", \ (*\ Square\ curve\ *) \ {"\" -> "\"}, \ \ 4]\) \); \ \(\(LShowColor[\ (*\ Dragon\ curve\ *) \ LSystem["\", \ {"\" -> "\", \ "\" -> \ "\<-FX-Y\>"}, \ 12]]\) \); \ \(\(LShowColor@ LSystem["\", \ (*\ Hilbert\ curve\ *) \ {"\"\ -> \ "\<+RF-LFL-FR+\>", \ \ "\"\ -> \ "\<-LF+RFR+FL-\>"}, \ 6]\) \); \ LShowColor@ LSystem["\", \ (*\ Hilbert\ curve\ II\ *) \ {"\"\ -> \ \ "\", \ "\"\ -> \ "\"}, \ 3];\), FormatType->StandardForm]}], "\[IndentingNewLine]", RowBox[{\(LShowColor[\ \ LSystem["\", \ {"\"\ -> \ \ "\"}, \ 4], \ N[86\ Degree]];\), " "}]}], "Input"] } ]