Fractal canopy
November 16, 2022
(ns fractals.core
(:gen-class))
(def tree-fractal-file "tree.svg")
(defn append-to-file
"Uses spit to append to a file specified with its name as a string, or
anything else that writer can take as an argument. s is the string to
append."
[file-name s]
(spit file-name s :append true))
(def header-xml "<?xml version=\"1.0\"?>")
(def header-svg "<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">")
(def footer "\" stroke=\"black\" stroke-width=\"1\"/>\n</svg>")
(defn map-y-coordinate [y]
(- 600 y))
(defn draw-line [x y x1 y1 file]
(append-to-file file (str " M " x " " (map-y-coordinate y)))
(append-to-file file (str " L " x1 " " (map-y-coordinate y1)))
)
(defn draw-tree [x y angle depth len fork-angle]
(if (> depth 0)
(let [x2 (- x (* (Math/sin (Math/toRadians angle)) depth len))
y2 (- y (* (Math/cos (Math/toRadians angle)) depth len))]
(draw-line x y x2 y2 tree-fractal-file)
(draw-tree x2 y2 (- angle fork-angle) (- depth 1) len fork-angle)
(draw-tree x2 y2 (+ angle fork-angle) (- depth 1) len fork-angle)
)))
(defn run-tree []
(let [content "<path d=\""
]
(append-to-file tree-fractal-file header-xml)
(append-to-file tree-fractal-file header-svg)
(append-to-file tree-fractal-file content)
(draw-tree 600 50 180 10 10 20)
(append-to-file tree-fractal-file footer)
))
(defn -main
[& args]
;; (run 0.0 0.0 729.0 )
(run-tree)
)
Continue reading →
Vicsek fractal
November 10, 2022
(def header-xml "<?xml version=\"1.0\"?>")
(def header-svg "<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">")
(def footer "\"/>\n</svg>")
(defn append-to-file
[file-name s]
(spit file-name s :append true))
(defn print-line [x y len file]
(append-to-file file (str "M " x " " y))
(append-to-file file (str "h " len))
(append-to-file file (str "v " len))
(append-to-file file (str "h " (- 0 len)))
(append-to-file file (str "v -" len))
)
(defn fractal-cross [x y len file]
(if (< len 3)
(print-line x y len file)
(let [l3 (/ len 3.0) l23 (* l3 2)]
(fractal-cross x y l3 file)
(fractal-cross (+ x l23) y l3 file)
(fractal-cross (+ x l3) (+ y l3) l3 file)
(fractal-cross x (+ y l23) l3 file)
(fractal-cross (+ x l23) (+ y l23) l3 file))))
(defn run [x,y,len file]
(let [content "<path d=\""]
(append-to-file file header-xml)
(append-to-file file header-svg)
(append-to-file file content)
(fractal-cross x y len file)
(append-to-file file footer)))
(run 0.0 0.0 729.0 "test.svg")
Continue reading →