Spaxe / elm-lsystem / LSystem.Turtle

Turtle graphics consists of 4 states:

type State
    = D -- | Draw forward
    | S -- | Skip foward without drawing
    | L -- | Turn left by some degrees
    | R -- | Turn right by some degrees

(In formal literature, it is known as F, f, -, and + respectively.)

Given a starting point and an angle, you can iterate through a list of States and create a generative drawing.

For example, the following ruleset applied 3 times will generate this:

rule : LSystem.Rule State
rule state =
    case state of
        D ->
            [ D, D, R, D, L, D, R, D, R, D, D ]

        S ->
            [ S ]

        L ->
            [ L ]

        R ->
            [ R ]

If you want a complete example of how this can be used in the Elm Architecture, see https://github.com/creative/elm-generative/blob/master/example/Turtle.elm

Module functions


type State
    = D
    | S
    | L
    | R
    | A
    | B
    | C

Turtle consists of 4 primary states.

We also supply additional states A, B, and C, which are ignored during drawing. They are useful for constructing rulesets only.

turtle : List State -> Basics.Float -> List Svg.PathD.Segment

Transforms a list of States into Svg.Path d-compatible property strings. This requires a starting point and a starting angle.

You can use the Svg.PathD library's d_ to draw segments as a SVG attribute:

import Svg.PathD as PathD exposing (pathD)

...


Svg.path
    [ d <| pathD <| turtle [ D, R, D, R, D, R, D ] 90
    ]
    []