Code Sketch


LSystem line pattern
By: Anay Narendra Kamat
Category: Art
cleari()

setSpeed(superFast)

setBackground(cm.white)

def steps = 3

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

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

        previous + result
    }

}

def carpetPattern = Picture {
    setPenColor(cm.black)

    def len = 20
    def angle = 90

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

    }
}

drawCentered(carpetPattern)