Skip to content

Commit 3092f76

Browse files
docs: Describing L Systems in Turtle
1 parent 96d083f commit 3092f76

File tree

5 files changed

+152
-5
lines changed

5 files changed

+152
-5
lines changed

Examples/BoxFractal1.svg

Lines changed: 6 additions & 0 deletions
Loading

Examples/BoxFractal2.svg

Lines changed: 6 additions & 0 deletions
Loading

Examples/BoxFractal3.svg

Lines changed: 6 additions & 0 deletions
Loading

README.md

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,66 @@ Because this Turtle generates SVG, we can also use it to create patterns.
130130
<div align='center'>
131131
<img src='./Examples/HexagonPattern.svg' alt='Hexagon Pattern' width='50%' />
132132
</div>
133-
Speaking of patterns, Turtle is often used to draw fractals.
133+
Turtle is often used to draw fractals.
134134

135+
Many fractals can be described in something called a [L-System](https://en.wikipedia.org/wiki/L-system) (short for Lindenmayer system)
136+
137+
L-Systems describe:
138+
139+
* An initial state (called an Axiom)
140+
* A series of rewriting rules
141+
* The way each variable should be interpreted.
142+
143+
For example, let's show how we contruct the [Box Fractal](https://en.wikipedia.org/wiki/Vicsek_fractal)
144+
145+
Our Axiom is F-F-F-F.
146+
147+
This should look familiar: it's a shorthand for the squares we drew earlier.
148+
149+
It basically reads "go forward, then left, four times"
150+
151+
Our Rule is F = 'F-F+F+F-F'.
152+
153+
This means every time we encounter F, we want to replace it with F-F+F+F-F.
154+
155+
This will turn our one box into 6 new boxes. If we repeat it again, we'll get 36 boxes. Once more and we're at 216 boxes.
156+
157+
Lets show the first three generations of the box fractal:
158+
159+
~~~PowerShell
160+
161+
Turtle BoxFractal 5 1 |
162+
Set-Turtle Stroke '#4488ff' |
163+
Save-Turtle ./Examples/BoxFractal1.svg
164+
165+
166+
167+
Turtle BoxFractal 5 2 |
168+
Set-Turtle Stroke '#4488ff' |
169+
Save-Turtle ./Examples/BoxFractal2.svg
170+
171+
172+
173+
Turtle BoxFractal 5 3 |
174+
Set-Turtle Stroke '#4488ff' |
175+
Save-Turtle ./Examples/BoxFractal3.svg
176+
177+
~~~
178+
179+
180+
@"
181+
<div align='center'>
182+
<img src='./Examples/BoxFractal1.svg' alt='Box Fractal 1' width='50%' />
183+
<img src='./Examples/BoxFractal2.svg' alt='Box Fractal 2' width='50%' />
184+
<img src='./Examples/BoxFractal3.svg' alt='Box Fractal 3' width='50%' />
185+
</div>
135186
This implementation of Turtle has quite a few built-in fractals.
136187

137188
For example, here is an example of a pattern comprised of Koch Snowflakes:
138189

139190
~~~PowerShell
140191
141-
turtle KochSnowflake 2.5 6 |
142-
Set-Turtle -Property Stroke '#4488ff' |
192+
turtle KochSnowflake 2.5 4 |
143193
Set-Turtle -Property StrokeWidth '0.1%' |
144194
Set-Turtle -Property Stroke '#4488ff' |
145195
Set-Turtle -Property PatternTransform -Value @{scale = 0.5 } |

README.md.ps1

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#requires -Module Turtle
88
param()
99

10+
#region Introduction
1011

1112
@"
1213
# Turtle
@@ -43,6 +44,10 @@ We can implement Turtle in any language.
4344
This module implements Turtle in PowerShell.
4445
"@
4546

47+
#endregion Introduction
48+
49+
#region Installation
50+
4651
@"
4752
### Installing and Importing
4853
@@ -71,6 +76,9 @@ Import-Module ./ -Force -PassThru
7176
~~~
7277
"@
7378

79+
#endregion Installation
80+
81+
#region Getting Started
7482
@"
7583
### Getting Started
7684
@@ -138,6 +146,8 @@ New-Turtle | Move-Turtle Square 10 | Save-Turtle ./Square.svg
138146
~~~
139147
'@
140148

149+
150+
141151
@'
142152
We can use loops:
143153
@@ -192,9 +202,78 @@ $HexPattern = . $MakeHexagonPattern
192202
"@
193203

194204

205+
#region LSystems
206+
207+
$box1 = {
208+
Turtle BoxFractal 5 1 |
209+
Set-Turtle Stroke '#4488ff' |
210+
Save-Turtle ./Examples/BoxFractal1.svg
211+
}
212+
213+
$box2 = {
214+
Turtle BoxFractal 5 2 |
215+
Set-Turtle Stroke '#4488ff' |
216+
Save-Turtle ./Examples/BoxFractal2.svg
217+
}
218+
219+
$box3 = {
220+
Turtle BoxFractal 5 3 |
221+
Set-Turtle Stroke '#4488ff' |
222+
Save-Turtle ./Examples/BoxFractal3.svg
223+
}
224+
195225
@"
196-
Speaking of patterns, Turtle is often used to draw fractals.
226+
Turtle is often used to draw fractals.
227+
228+
Many fractals can be described in something called a [L-System](https://en.wikipedia.org/wiki/L-system) (short for Lindenmayer system)
229+
230+
L-Systems describe:
231+
232+
* An initial state (called an Axiom)
233+
* A series of rewriting rules
234+
* The way each variable should be interpreted.
235+
236+
For example, let's show how we contruct the [Box Fractal](https://en.wikipedia.org/wiki/Vicsek_fractal)
237+
238+
Our Axiom is `F-F-F-F`.
239+
240+
This should look familiar: it's a shorthand for the squares we drew earlier.
241+
242+
It basically reads "go forward, then left, four times"
243+
244+
Our Rule is `F = 'F-F+F+F-F'`.
197245
246+
This means every time we encounter `F`, we want to replace it with `F-F+F+F-F`.
247+
248+
This will turn our one box into 6 new boxes. If we repeat it again, we'll get 36 boxes. Once more and we're at 216 boxes.
249+
250+
Lets show the first three generations of the box fractal:
251+
252+
~~~PowerShell
253+
$box1
254+
255+
$box2
256+
257+
$box3
258+
~~~
259+
$(
260+
$null = @(
261+
. $box1
262+
. $box2
263+
. $box3
264+
)
265+
)
266+
267+
@"
268+
<div align='center'>
269+
<img src='./Examples/BoxFractal1.svg' alt='Box Fractal 1' width='50%' />
270+
<img src='./Examples/BoxFractal2.svg' alt='Box Fractal 2' width='50%' />
271+
<img src='./Examples/BoxFractal3.svg' alt='Box Fractal 3' width='50%' />
272+
</div>
273+
"@
274+
275+
276+
@"
198277
This implementation of Turtle has quite a few built-in fractals.
199278
200279
For example, here is an example of a pattern comprised of Koch Snowflakes:
@@ -225,7 +304,7 @@ $SnowFlakePattern = . $MakeSnowflakePattern
225304
"@
226305

227306

228-
307+
#endregion LSystems
229308

230309

231310

0 commit comments

Comments
 (0)