Skip to content

Instantly share code, notes, and snippets.

@gvangool
Last active November 23, 2017 09:00
Show Gist options
  • Save gvangool/9deaa19a9cb259b3e53e to your computer and use it in GitHub Desktop.
Save gvangool/9deaa19a9cb259b3e53e to your computer and use it in GitHub Desktop.
Generates an SVG Sierpinski triangle in ELM, demo: http://codepen.io/gvangool/full/vGGzjb/
import Svg exposing (Svg, svg, g, use, defs)
import Svg.Attributes exposing (..)
import Html
triangle_path = "M0 0,2 0,1 1.732 z"
matrix1 = "matrix(0.5 0 0 0.5 0 0)"
matrix2 = "matrix(0.5 0 0 0.5 1 0)"
matrix3 = "matrix(0.5 0 0 0.5 0.5 0.866)"
getLevels : Int -> List (Svg n)
getLevels n =
case n of
0 ->
[Svg.path [id "lev0", fill "#039", d triangle_path] []]
_ ->
let
levp = "#lev" ++ toString (n - 1)
levn = "lev" ++ toString n
in
getLevels (n - 1) ++ [g [id levn]
[ use [xlinkHref levp, transform matrix1] []
, use [xlinkHref levp, transform matrix2] []
, use [xlinkHref levp, transform matrix3] []
]]
main =
svg [ version "1.1", x "0", y "0", width "600", height "400" ]
[ defs [] (getLevels 5)
, use [xlinkHref "#lev5", transform "translate(50,50) scale(200)"] []
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment