-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
LSystem.scala
123 lines (104 loc) · 2.84 KB
/
LSystem.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright 2015 Creative Scala
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package doodle
package turtle
package examples
import doodle.core._
import doodle.image.Image
import doodle.syntax.all._
import doodle.turtle._
object LSystem {
import Instruction._
def iterate(
steps: Int,
seed: List[Instruction],
rule: Instruction => List[Instruction]
): List[Instruction] = {
def rewrite(instructions: List[Instruction]): List[Instruction] =
instructions.flatMap {
case Branch(i) =>
List(branch(rewrite(i): _*))
case other =>
rule(other)
}
steps match {
case 0 =>
seed
case _ =>
val rewritten = rewrite(seed)
iterate(steps - 1, rewritten, rule)
}
}
object tree {
val left = turn(25.degrees)
val right = turn(-25.degrees)
val f = forward(5)
val rule = (i: Instruction) => {
i match {
case NoOp => // F−[+X]+F[+FX]−X
List(
f,
left,
branch(branch(noop), right, noop),
right,
f,
branch(right, f, noop),
left,
noop
)
case Forward(_) =>
List(f, f)
case other =>
List(other)
}
}
val image = Turtle
.draw(iterate(6, List(noop), rule), 50.degrees)
.strokeColor(Color.forestGreen)
}
object flowers {
val f = forward(5)
val spin = turn(51.degrees)
val spur = List(spin, branch(f, noop))
val rule = (i: Instruction) => {
i match {
case Forward(_) => List(f, f, f)
case NoOp => spur ++ spur ++ spur ++ spur ++ spur ++ spur ++ spur
case other => List(other)
}
}
val image = Turtle.draw(iterate(5, List(noop), rule))
}
object kochCurve {
val f = forward(5)
val tP = turn(45.degrees)
val tM = turn(-90.degrees)
val spacer = Image.rectangle(5, 5).noFill.noStroke
def rule(i: Instruction): List[Instruction] =
i match {
case Forward(_) => List(f, tP, f, tM, f, tP, f)
case other => List(other)
}
val seed = List(f)
val iterations = List.tabulate(5) { n =>
Turtle.draw(iterate(n, seed, rule))
}
val image =
iterations.fold(Image.empty) { (img, i) =>
img above spacer above i
}
}
}