# B4J Tutorial[B4XTurtle]L-Systems and The Money Tree

Inspired by @rabbitBUSH I followed the suggested Wiki link:

I found it very interesting, and thought it would be fun to make the B4XTurtle follow L-System Grammar instructions.
Not for young children, but ok for older children like us.

Here is the Money Tree.

#### Attachments

• LSystem.zip
4.4 KB · Views: 63

#### William Lancee

##### Well-Known Member
Longtime User
There is a wonderful book using L-Systems to help understand how plants grow.
"The Algorithmic Beauty of Plants "
by
Przemyslaw Prusinkiewicz and Aristid Lindenmayer
With James S. Hanan, F. David Fracchia, Deborah Fowler, Martin J. M. de Boer, Lynn Mercer
It has 150 Illustrations, 48 in Color.

http://algorithmicbotany.org/papers/abop/abop.pdf

I am going through the manuscript following the algorithmic journey.
The attached project shows how the L-System grammar is expanded and then used to drive B4XTurtle.

#### Attachments

• LPatterns2.zip
4.6 KB · Views: 44
Last edited:

#### William Lancee

##### Well-Known Member
Longtime User
From page 12 of Book in #2: figure 1.11 a, edge replacement grammar two symbols: Fr = A and Fl = B
This curve belongs to the class of FASS construction curves (an acronym for space-Filling, self-Avoiding, Simple and self-Similar)

B4X:
``````    'Use smart string to describe spec
Dim spec As String = \$"
n=4, δ=60◦
Fl
Fl→Fl+Fr++Fr-Fl--FlFl-Fr+
Fr→-Fl+FrFr++Fr+Fl--Fl-Fr
"\$``````

#### Attachments

• edgeReplacement.zip
4.8 KB · Views: 39

#### William Lancee

##### Well-Known Member
Longtime User
The Maple Leaf is Canada's national symbol. The L-System definition for the leaf could be:
B4X:
``````    STurtle.Instructions(delta, 2, 45, "FF.[[A][B]|[A][B]]F-B", CreateMap( _
"A": "[+++FFF*]++FFFF[+FF*][-FF*][FFF*]", _
"B": "+FFF[[[+FFF*][-FFF*]FF[+FFF*][-FFF*]FF[+FF*][FFF*][-FF*]]]"))``````

I said "could be" because there is no single definition.
This definition does not highlight the growth sequence of a leaf - I haven't figured out what that would be.

Leaves are sun-catchers and look like upside-down parasols.
This parasol also has growth patterns that I haven't discovered yet.
However, it can be simulated by connecting the terminals of the leaf's structure with "scallops" created by quadratic Bezier curves.

One benefit of Turtle drawings is that scale, angle and position are easy to set, since the steps of turtle are all relative to an initial state.

Attached is the code that produced the above images.
Most of the work is done by a Class called SuperTurtle.
SuperTurtle uses the Main turtle to draw standard things like circles, rectangles, curves and L-System instructions.
There is also an important small class that makes all tasks simpler: "Point"

My experience with Turtle drawings is that it can make some tasks easier and some tasks harder.
The B4XTurtle template is not the only way to use Turtle. Since Turtle is a custom view, it is also a Class.
It can be used in any standard project, if the benefits of its drawing methods are advantageous and/or animation is needed.

Above all, this project challenged my problem solving skills in new ways, and that was fun.

#### Attachments

• MapLeaves.zip
8.3 KB · Views: 39
Last edited:

#### William Lancee

##### Well-Known Member
Longtime User

From Wiki: https://en.wikipedia.org/wiki/L-system#Example_7:_fractal_plant
variables : X F (X has pen up, F has pen down)
constants : + − [ ]
start : X
rules : (X → F+[[X]-X]-F[-FX]+X), (F → FF)
angle : 25°
iterations = 8

B4X:
``````Sub Turtle_Start
STurtle.Initialize(Me, Turtle)
Turtle.Home'
Turtle.RabbitMode.SetTurtleVisible(False)
MakeFern(p.XY(400, 800), 2, -90, xui.Color_RGB(79, 121, 66))
End Sub

Private Sub MakeFern(position As Point, delta As Int, angle As Int, clr As Int)
STurtle.Position(position, angle)
Turtle.SetPenColor(clr)
STurtle.Instructions(delta, 8, 25, "x", CreateMap("x": "F+[[x]-x]-F[-Fx]+x", "F": "FF"))
End Sub``````

#### Attachments

• Fern.zip
8.5 KB · Views: 46

Replies
4
Views
1K
B4J Question Is my program running
Replies
2
Views
658
Replies
2
Views
741
Replies
1
Views
6K
Replies
0
Views
2K