Skip to content

Commit b286771

Browse files
author
James Brundage
committed
docs: Turtle.LSystem docs ( Fixes #20 )
Also forcing scriptblocks to be locally recreated
1 parent c20f9f7 commit b286771

File tree

1 file changed

+118
-3
lines changed

1 file changed

+118
-3
lines changed

Types/Turtle/LSystem.ps1

Lines changed: 118 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,108 @@
1+
<#
2+
.SYNOPSIS
3+
Draws a L-system pattern.
4+
.DESCRIPTION
5+
Generates a pattern using a L-system.
6+
7+
The initial string (Axiom) is transformed according to the rules provided for a specified number of iterations.
8+
.LINK
9+
https://en.wikipedia.org/wiki/L-system
10+
.EXAMPLE
11+
# Box Fractal L-System
12+
$Box = 'F-F-F-F'
13+
$Fractal = 'F-F+F+F-F'
14+
15+
$turtle.Clear().LSystem(
16+
$Box,
17+
[Ordered]@{ F = $Fractal },
18+
3,
19+
@{
20+
F = { $this.Forward(10) }
21+
J = { $this.Jump(10) }
22+
'\+' = { $this.Rotate(90) }
23+
'-' = { $this.Rotate(-90) }
24+
}
25+
).Pattern.Save("$pwd/BoxFractalLSystem.svg")
26+
.EXAMPLE
27+
# Fractal L-System
28+
$Box = 'FFFF-FFFF-FFFF-FFFF'
29+
$Fractal = 'F-F+F+F-F'
30+
31+
$turtle.Clear().LSystem(
32+
$Box,
33+
[Ordered]@{ F = $Fractal },
34+
4,
35+
@{
36+
F = { $this.Forward(10) }
37+
J = { $this.Jump(10) }
38+
'\+' = { $this.Rotate(90) }
39+
'-' = { $this.Rotate(-90) }
40+
}
41+
).Symbol.Save("$pwd/FractalLSystem.svg")
42+
.EXAMPLE
43+
# Arrowhead Fractal L-System
44+
$Box = 'FF-FF-FF'
45+
$Fractal = 'F-F+F+F-F'
46+
47+
48+
$turtle.Clear().LSystem(
49+
$Box,
50+
[Ordered]@{ F = $Fractal },
51+
4,
52+
@{
53+
F = { $this.Forward(10) }
54+
J = { $this.Jump(10) }
55+
'\+' = { $this.Rotate(90) }
56+
'-' = { $this.Rotate(-90) }
57+
}
58+
).Pattern.Save("$pwd/ArrowheadFractalLSystem.svg")
59+
.EXAMPLE
60+
# Tetroid LSystem
61+
$turtle.Clear().LSystem(
62+
'F',
63+
[Ordered]@{ F = 'F+F+F+F' +
64+
'+JJJJ+' +
65+
'F+F+F+F' +
66+
'++JJJJ' +
67+
'F+F+F+F' +
68+
'++JJJJ' +
69+
'F+F+F+F' +
70+
'++JJJJ' +
71+
'-JJJJ'
72+
},
73+
3,
74+
@{
75+
F = { $this.Forward(10) }
76+
J = { $this.Jump(10) }
77+
'\+' = { $this.Rotate(90) }
78+
'-' = { $this.Rotate(-90) }
79+
}
80+
).Pattern.Save("$pwd/TetroidLSystem.svg")
81+
82+
.EXAMPLE
83+
$turtle.Clear().LSystem(
84+
'F',
85+
[Ordered]@{ F = '
86+
F+F+F+F +JJJJ+ F+F+F+F ++ JJJJ' },
87+
3,
88+
@{
89+
F = { $this.Forward(10) }
90+
J = { $this.Jump(10) }
91+
'\+' = { $this.Rotate(90) }
92+
'-' = { $this.Rotate(-90) }
93+
}
94+
).Pattern.Save("$pwd/LSystemCool1.svg")
95+
.EXAMPLE
96+
Move-Turtle LSystem F-F-F-F ([Ordered]@{F='F-F+F+F-F'}) 3 (
97+
[Ordered]@{
98+
F = { $this.Forward(10) }
99+
J = { $this.Jump(10) }
100+
'\+' = { $this.Rotate(90) }
101+
'-' = { $this.Rotate(-90) }
102+
}
103+
)
104+
105+
#>
1106
param(
2107
[Alias('Start', 'StartString', 'Initiator')]
3108
[string]
@@ -15,27 +120,37 @@ $N = 2,
15120
$Variable = @{}
16121
)
17122

18-
if ($n -le 1) { return $Axiom}
123+
if ($n -lt 1) { return $Axiom}
19124

20125
$currentState = "$Axiom"
21126
$combinedPattern = "(?>$($Rule.Keys -join '|'))"
22127
foreach ($iteration in 1..$n) {
23128
$currentState = $currentState -replace $combinedPattern, {
24129
$match = $_
25130
$matchingRule = $rule["$match"]
26-
if ($matchingRule -is [ScriptBlock]) {
131+
if ($matchingRule -is [ScriptBlock]) {
27132
return "$(& $matchingRule $match)"
28133
} else {
29134
return $matchingRule
30135
}
31136
}
32137
}
33138

139+
$localReplacement = [Ordered]@{}
140+
foreach ($key in $variable.Keys) {
141+
$localReplacement[$key] =
142+
if ($variable[$key] -is [ScriptBlock]) {
143+
[ScriptBlock]::Create($variable[$key])
144+
} else {
145+
$variable[$key]
146+
}
147+
}
148+
34149
$finalState = $currentState
35150
$null = foreach ($character in $finalState.ToCharArray()) {
36151
foreach ($key in $Variable.Keys) {
37152
if ($character -match $key) {
38-
$action = $Variable[$key]
153+
$action = $localReplacement[$key]
39154
if ($action -is [ScriptBlock]) {
40155
. $action $character
41156
} else {

0 commit comments

Comments
 (0)