# L-System in Mathematica

Here is some Maple code for drawing an L-System:

  with(Fractals:-LSystem)
cons := ["A" = "draw:1", "+" = "turn:-90", "B" = "turn:90"]
state, rules := "A", ["A" = "AB+BA+B", "B" = "B+AAB"]
newstate1 := Iterate(state, rules, 7)

LSystemPlot(newstate1, cons)


How can I make the same graphic using Mathematica?

I tried the first few steps:

SubstitutionSystem[{"A" -> "AB+BA+B", "B" -> "B+AAB"}, "A", {3}]

{"AB+BA+BB+AAB+B+AABAB+BA+B+B+AAB"}


str = First@
SubstitutionSystem[{"A" -> "AB+BA+B", "B" -> "B+AAB"}, "A", {7}];

asc = <|"A" -> {1, 0}, "B" -> {0, Pi/2}, "+" -> {0, -Pi/2}|>;


Here {1,0} means go forward 1 step and turn 0 radians. The turtle graphics substitute in Mathematica is AnglePath.

Graphics[
Line@AnglePath@Lookup[asc, Characters[str]]
]


Thanks to @Pillsy, a shorter and faster way is

StringCases[str, {"A" -> {1, 0}, "B" -> {0, Pi/2}, "+" -> {0, -Pi/2}}]


• Nice. Cool knowledge of recent functions. Commented Apr 9, 2017 at 14:01
• Sweet. You can also generate the angle path steps in a single step using StringCases. Commented Apr 9, 2017 at 14:10
• @Vitaliy Actually I did not know SubstitutionSystem. I am happy to have learned something new. Commented Apr 9, 2017 at 14:13
• @Pillsy Indeed. It's faster too. Somehow I always thought of StringCases as a strictly string related function, and it simply did not occur to me to use it this way. Commented Apr 9, 2017 at 14:14