← Home

About

KNOW XEREX

likes LANGUAGES AND FOSS

mail: proton

Strives to make things I LIKE :

MANDELBROT SET IN CLOJURE :

(ns xelog.mandelbrot)

(def palette " .:*+x$#%")

(defn escape-iteration [cx cy max-iters]
  (loop [x 0.0
         y 0.0
         iteration 0]
    (cond
      (>= iteration max-iters) iteration
      (> (+ (* x x) (* y y)) 4.0) iteration
      :else (recur (+ (- (* x x) (* y y)) cx)
                   (+ (* 2.0 x y) cy)
                   (inc iteration)))))

(defn pixel->character [iteration max-iters]
  (let [index (min (dec (count palette))
                   (quot (* iteration (count palette)) max-iters))]
    (nth palette index)))

(defn render-mandelbrot [width height max-iters]
  (doseq [py (range height)]
    (println
     (apply str
            (for [px (range width)
                  :let [cx (+ -2.0 (* 3.0 (/ px (double width))))
                        cy (+ -1.0 (* 2.0 (/ py (double height))))
                        iteration (escape-iteration cx cy max-iters)]]
              (pixel->character iteration max-iters))))))

(defn -main []
  (render-mandelbrot 100 50 200))

Bye, college sucks! 🦀