Code Sketch


L-System Carpet
By: Anay Narendra Kamat
cleari()

setSpeed(superFast)

setBackground(cm.lightGrey)

zoom(0.7)

def steps = 5

val outputAfterGenerations = (0 until steps).foldLeft("F-F-F-F") { (generationOutput, _) =>

    generationOutput.foldLeft("") { (previous, input) =>
        val result = input match {
            case 'F'=> "F[F]-F+F[--F]+F-F"
            case _   => input.toString
        }

        previous + result
    }

}

def tree = Picture {

    setPenColor(cm.maroon)

    def len = 2 
    def angle = 90

    outputAfterGenerations.foreach {
        case 'F' => forward(len)
        case 'G' =>
            penUp(); forward(len); penDown()
        case '[' => savePosHe()
        case ']' => restorePosHe()
        case '+' => right(angle)
        case '-' => left(angle)
        case _   =>
    }

}

drawCentered(tree)