Some exciting new features have been added to Kojo over the past couple of months:
Please give them a try, and let me know what you think...
Please give them a try, and let me know what you think...
Lalit's technical blog; thoughts and ideas on the structure and flow of computer programs, and related matters.
| F | Go forward by the base length |
| f | Same as F (sometimes you need two different drawing letters) |
| G | Go forward by the base length - with the pen up |
| | | Go forward by the base length scaled down for the current generation |
| [ | Save position and heading |
| ] | Restore position and heading |
| + | Turn right |
| - | Turn left |
case class LSystem(axiom: String, angle: Double, len: Int = 100, sf: Double = 0.6)(rules: PartialFunction[Char, String]) {
var currVal = axiom
var currGen = 0
def evolve() {
currGen += 1
currVal = currVal.map { c =>
if (rules.isDefinedAt(c)) rules(c) else c
}.mkString.replaceAll("""\|""" , currGen.toString)
}
def draw() {
def isDigit(c: Char) = Character.isDigit(c)
val genNum = new StringBuilder
def maybeDrawBar() {
if (genNum.size != 0) {
val n = genNum.toString.toInt
genNum.clear()
forward(len * math.pow(sf, n))
}
}
currVal.foreach { c =>
if (!isDigit(c)) {
maybeDrawBar()
}
c match {
case 'F' => forward(len)
case 'f' => forward(len)
case 'G' => penUp(); forward(len); penDown()
case '[' => savePosHe()
case ']' => restorePosHe()
case '+' => right(angle)
case '-' => left(angle)
case n if isDigit(n) => genNum.append(n)
case _ =>
}
}
maybeDrawBar()
}
}
val sierp_wp6 = LSystem("F", 60, 2) {
case 'F' => "f+F+f"
case 'f' => "F-f-F"
}
val dragon_wp7 = LSystem("FX", 90, 10) {
case 'X' => "X+YF"
case 'Y' => "FX-Y"
}
val fplant_wp8 = LSystem("X", 25, 4) {
case 'X'=> "F-[[X]+X]+F[+FX]-X"
case 'F' => "FF"
}
val tree2 = LSystem("G", 8, 100, 0.35) {
case 'G' => "|[+++++G][-------G]-|[++++G][------G]-|[+++G][-----G]-|G"
}
val carpet = LSystem("F-F-F-F", 90, 1) {
case 'F'=> "F[F]-F+F[--F]+F-F"
}