Last active
November 23, 2017 09:00
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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